Move more bounds

changelog: skip
This commit is contained in:
Aleksey Kladov 2021-03-16 22:28:04 +03:00
parent 4771a5f1ca
commit d733c9bdad
2 changed files with 54 additions and 36 deletions

View file

@ -40,9 +40,9 @@ pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext
let where_clause: ast::WhereClause = match_ast! { let where_clause: ast::WhereClause = match_ast! {
match parent { match parent {
ast::Fn(it) => it.get_or_create_where_clause(), 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::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(), ast::Struct(it) => it.get_or_create_where_clause(),
_ => return, _ => return,
} }
@ -82,12 +82,8 @@ mod tests {
fn move_bounds_to_where_clause_fn() { fn move_bounds_to_where_clause_fn() {
check_assist( check_assist(
move_bounds_to_where_clause, move_bounds_to_where_clause,
r#" r#"fn foo<T: u32, $0F: FnOnce(T) -> T>() {}"#,
fn foo<T: u32, $0F: FnOnce(T) -> T>() {} r#"fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}"#,
"#,
r#"
fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
"#,
); );
} }
@ -95,12 +91,8 @@ mod tests {
fn move_bounds_to_where_clause_impl() { fn move_bounds_to_where_clause_impl() {
check_assist( check_assist(
move_bounds_to_where_clause, move_bounds_to_where_clause,
r#" r#"impl<U: u32, $0T> A<U, T> {}"#,
impl<U: u32, $0T> A<U, T> {} r#"impl<U, T> A<U, T> where U: u32 {}"#,
"#,
r#"
impl<U, T> A<U, T> where U: u32 {}
"#,
); );
} }
@ -108,12 +100,8 @@ mod tests {
fn move_bounds_to_where_clause_struct() { fn move_bounds_to_where_clause_struct() {
check_assist( check_assist(
move_bounds_to_where_clause, move_bounds_to_where_clause,
r#" r#"struct A<$0T: Iterator<Item = u32>> {}"#,
struct A<$0T: Iterator<Item = u32>> {} r#"struct A<T> where T: Iterator<Item = u32> {}"#,
"#,
r#"
struct A<T> where T: Iterator<Item = u32> {}
"#,
); );
} }
@ -121,12 +109,8 @@ mod tests {
fn move_bounds_to_where_clause_tuple_struct() { fn move_bounds_to_where_clause_tuple_struct() {
check_assist( check_assist(
move_bounds_to_where_clause, move_bounds_to_where_clause,
r#" r#"struct Pair<$0T: u32>(T, T);"#,
struct Pair<$0T: u32>(T, T); r#"struct Pair<T>(T, T) where T: u32;"#,
"#,
r#"
struct Pair<T>(T, T) where T: u32;
"#,
); );
} }
} }

View file

@ -27,7 +27,7 @@ impl GenericParamsOwnerEdit for ast::Fn {
} else { } else {
Position::last_child_of(self.syntax().clone()) Position::last_child_of(self.syntax().clone())
}; };
create_where_clause(position) create_where_clause(position, true)
} }
self.where_clause().unwrap() self.where_clause().unwrap()
} }
@ -36,16 +36,31 @@ impl GenericParamsOwnerEdit for ast::Fn {
impl GenericParamsOwnerEdit for ast::Impl { impl GenericParamsOwnerEdit for ast::Impl {
fn get_or_create_where_clause(&self) -> WhereClause { fn get_or_create_where_clause(&self) -> WhereClause {
if self.where_clause().is_none() { if self.where_clause().is_none() {
let position = if let Some(ty) = self.self_ty() { let position = if let Some(items) = self.assoc_item_list() {
Position::after(ty.syntax().clone()) Position::before(items.syntax().clone())
} else { } else {
Position::last_child_of(self.syntax().clone()) Position::last_child_of(self.syntax().clone())
}; };
create_where_clause(position) create_where_clause(position, false)
} }
self.where_clause().unwrap() 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 { impl GenericParamsOwnerEdit for ast::Struct {
fn get_or_create_where_clause(&self) -> WhereClause { fn get_or_create_where_clause(&self) -> WhereClause {
if self.where_clause().is_none() { if self.where_clause().is_none() {
@ -62,17 +77,36 @@ impl GenericParamsOwnerEdit for ast::Struct {
} else { } else {
Position::last_child_of(self.syntax().clone()) Position::last_child_of(self.syntax().clone())
}; };
create_where_clause(position) create_where_clause(position, true)
} }
self.where_clause().unwrap() self.where_clause().unwrap()
} }
} }
fn create_where_clause(position: Position) { impl GenericParamsOwnerEdit for ast::Enum {
let elements = vec![ fn get_or_create_where_clause(&self) -> WhereClause {
make::tokens::single_space().into(), if self.where_clause().is_none() {
make::where_clause(empty()).clone_for_update().syntax().clone().into(), 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, 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); ted::insert_all(position, elements);
} }