From d733c9bdad81e23959b1a43421a9fa6ea92eda9f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 16 Mar 2021 22:28:04 +0300 Subject: Move more bounds changelog: skip --- crates/ide_assists/src/handlers/move_bounds.rs | 36 +++++------------ crates/syntax/src/ast/edit_in_place.rs | 54 +++++++++++++++++++++----- 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/crates/ide_assists/src/handlers/move_bounds.rs b/crates/ide_assists/src/handlers/move_bounds.rs index 48efa67ed..9ad0c9816 100644 --- a/crates/ide_assists/src/handlers/move_bounds.rs +++ b/crates/ide_assists/src/handlers/move_bounds.rs @@ -40,9 +40,9 @@ pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext let where_clause: ast::WhereClause = match_ast! { match parent { ast::Fn(it) => it.get_or_create_where_clause(), - // ast::Trait(it) => it.get_or_create_where_clause(), + ast::Trait(it) => it.get_or_create_where_clause(), ast::Impl(it) => it.get_or_create_where_clause(), - // ast::Enum(it) => it.get_or_create_where_clause(), + ast::Enum(it) => it.get_or_create_where_clause(), ast::Struct(it) => it.get_or_create_where_clause(), _ => return, } @@ -82,12 +82,8 @@ mod tests { fn move_bounds_to_where_clause_fn() { check_assist( move_bounds_to_where_clause, - r#" - fn foo T>() {} - "#, - r#" - fn foo() where T: u32, F: FnOnce(T) -> T {} - "#, + r#"fn foo T>() {}"#, + r#"fn foo() where T: u32, F: FnOnce(T) -> T {}"#, ); } @@ -95,12 +91,8 @@ mod tests { fn move_bounds_to_where_clause_impl() { check_assist( move_bounds_to_where_clause, - r#" - impl A {} - "#, - r#" - impl A where U: u32 {} - "#, + r#"impl A {}"#, + r#"impl A where U: u32 {}"#, ); } @@ -108,12 +100,8 @@ mod tests { fn move_bounds_to_where_clause_struct() { check_assist( move_bounds_to_where_clause, - r#" - struct A<$0T: Iterator> {} - "#, - r#" - struct A where T: Iterator {} - "#, + r#"struct A<$0T: Iterator> {}"#, + r#"struct A where T: Iterator {}"#, ); } @@ -121,12 +109,8 @@ mod tests { fn move_bounds_to_where_clause_tuple_struct() { check_assist( move_bounds_to_where_clause, - r#" - struct Pair<$0T: u32>(T, T); - "#, - r#" - struct Pair(T, T) where T: u32; - "#, + r#"struct Pair<$0T: u32>(T, T);"#, + r#"struct Pair(T, T) where T: u32;"#, ); } } diff --git a/crates/syntax/src/ast/edit_in_place.rs b/crates/syntax/src/ast/edit_in_place.rs index 06cde591d..1788f2a40 100644 --- a/crates/syntax/src/ast/edit_in_place.rs +++ b/crates/syntax/src/ast/edit_in_place.rs @@ -27,7 +27,7 @@ impl GenericParamsOwnerEdit for ast::Fn { } else { Position::last_child_of(self.syntax().clone()) }; - create_where_clause(position) + create_where_clause(position, true) } self.where_clause().unwrap() } @@ -36,16 +36,31 @@ impl GenericParamsOwnerEdit for ast::Fn { impl GenericParamsOwnerEdit for ast::Impl { fn get_or_create_where_clause(&self) -> WhereClause { if self.where_clause().is_none() { - let position = if let Some(ty) = self.self_ty() { - Position::after(ty.syntax().clone()) + let position = if let Some(items) = self.assoc_item_list() { + Position::before(items.syntax().clone()) } else { Position::last_child_of(self.syntax().clone()) }; - create_where_clause(position) + create_where_clause(position, false) } self.where_clause().unwrap() } } + +impl GenericParamsOwnerEdit for ast::Trait { + fn get_or_create_where_clause(&self) -> WhereClause { + if self.where_clause().is_none() { + let position = if let Some(items) = self.assoc_item_list() { + Position::before(items.syntax().clone()) + } else { + Position::last_child_of(self.syntax().clone()) + }; + create_where_clause(position, false) + } + self.where_clause().unwrap() + } +} + impl GenericParamsOwnerEdit for ast::Struct { fn get_or_create_where_clause(&self) -> WhereClause { if self.where_clause().is_none() { @@ -62,17 +77,36 @@ impl GenericParamsOwnerEdit for ast::Struct { } else { Position::last_child_of(self.syntax().clone()) }; - create_where_clause(position) + create_where_clause(position, true) + } + self.where_clause().unwrap() + } +} + +impl GenericParamsOwnerEdit for ast::Enum { + fn get_or_create_where_clause(&self) -> WhereClause { + if self.where_clause().is_none() { + let position = if let Some(gpl) = self.generic_param_list() { + Position::after(gpl.syntax().clone()) + } else if let Some(name) = self.name() { + Position::after(name.syntax().clone()) + } else { + Position::last_child_of(self.syntax().clone()) + }; + create_where_clause(position, true) } self.where_clause().unwrap() } } -fn create_where_clause(position: Position) { - let elements = vec![ - make::tokens::single_space().into(), - make::where_clause(empty()).clone_for_update().syntax().clone().into(), - ]; +fn create_where_clause(position: Position, after: bool) { + let mut elements = vec![make::where_clause(empty()).clone_for_update().syntax().clone().into()]; + let ws = make::tokens::single_space().into(); + if after { + elements.insert(0, ws) + } else { + elements.push(ws) + } ted::insert_all(position, elements); } -- cgit v1.2.3