8060: Move more bounds r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-03-16 19:28:47 +00:00 committed by GitHub
commit 9375207461
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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! {
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: u32, $0F: FnOnce(T) -> T>() {}
"#,
r#"
fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
"#,
r#"fn foo<T: u32, $0F: FnOnce(T) -> T>() {}"#,
r#"fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}"#,
);
}
@ -95,12 +91,8 @@ fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
fn move_bounds_to_where_clause_impl() {
check_assist(
move_bounds_to_where_clause,
r#"
impl<U: u32, $0T> A<U, T> {}
"#,
r#"
impl<U, T> A<U, T> where U: u32 {}
"#,
r#"impl<U: u32, $0T> A<U, T> {}"#,
r#"impl<U, T> A<U, T> where U: u32 {}"#,
);
}
@ -108,12 +100,8 @@ impl<U, T> A<U, T> where U: u32 {}
fn move_bounds_to_where_clause_struct() {
check_assist(
move_bounds_to_where_clause,
r#"
struct A<$0T: Iterator<Item = u32>> {}
"#,
r#"
struct A<T> where T: Iterator<Item = u32> {}
"#,
r#"struct A<$0T: Iterator<Item = u32>> {}"#,
r#"struct A<T> where T: Iterator<Item = u32> {}"#,
);
}
@ -121,12 +109,8 @@ struct A<T> where T: Iterator<Item = u32> {}
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, T) where T: u32;
"#,
r#"struct Pair<$0T: u32>(T, T);"#,
r#"struct Pair<T>(T, T) where T: u32;"#,
);
}
}

View File

@ -27,7 +27,7 @@ fn get_or_create_where_clause(&self) -> WhereClause {
} 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 @@ fn get_or_create_where_clause(&self) -> WhereClause {
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 @@ fn get_or_create_where_clause(&self) -> WhereClause {
} else {
Position::last_child_of(self.syntax().clone())
};
create_where_clause(position)
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(),
];
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, 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);
}