Skip to content

Commit d733c9b

Browse files
committed
Move more bounds
changelog: skip
1 parent 4771a5f commit d733c9b

File tree

2 files changed

+54
-36
lines changed

2 files changed

+54
-36
lines changed

crates/ide_assists/src/handlers/move_bounds.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext
4040
let where_clause: ast::WhereClause = match_ast! {
4141
match parent {
4242
ast::Fn(it) => it.get_or_create_where_clause(),
43-
// ast::Trait(it) => it.get_or_create_where_clause(),
43+
ast::Trait(it) => it.get_or_create_where_clause(),
4444
ast::Impl(it) => it.get_or_create_where_clause(),
45-
// ast::Enum(it) => it.get_or_create_where_clause(),
45+
ast::Enum(it) => it.get_or_create_where_clause(),
4646
ast::Struct(it) => it.get_or_create_where_clause(),
4747
_ => return,
4848
}
@@ -82,51 +82,35 @@ mod tests {
8282
fn move_bounds_to_where_clause_fn() {
8383
check_assist(
8484
move_bounds_to_where_clause,
85-
r#"
86-
fn foo<T: u32, $0F: FnOnce(T) -> T>() {}
87-
"#,
88-
r#"
89-
fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
90-
"#,
85+
r#"fn foo<T: u32, $0F: FnOnce(T) -> T>() {}"#,
86+
r#"fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}"#,
9187
);
9288
}
9389

9490
#[test]
9591
fn move_bounds_to_where_clause_impl() {
9692
check_assist(
9793
move_bounds_to_where_clause,
98-
r#"
99-
impl<U: u32, $0T> A<U, T> {}
100-
"#,
101-
r#"
102-
impl<U, T> A<U, T> where U: u32 {}
103-
"#,
94+
r#"impl<U: u32, $0T> A<U, T> {}"#,
95+
r#"impl<U, T> A<U, T> where U: u32 {}"#,
10496
);
10597
}
10698

10799
#[test]
108100
fn move_bounds_to_where_clause_struct() {
109101
check_assist(
110102
move_bounds_to_where_clause,
111-
r#"
112-
struct A<$0T: Iterator<Item = u32>> {}
113-
"#,
114-
r#"
115-
struct A<T> where T: Iterator<Item = u32> {}
116-
"#,
103+
r#"struct A<$0T: Iterator<Item = u32>> {}"#,
104+
r#"struct A<T> where T: Iterator<Item = u32> {}"#,
117105
);
118106
}
119107

120108
#[test]
121109
fn move_bounds_to_where_clause_tuple_struct() {
122110
check_assist(
123111
move_bounds_to_where_clause,
124-
r#"
125-
struct Pair<$0T: u32>(T, T);
126-
"#,
127-
r#"
128-
struct Pair<T>(T, T) where T: u32;
129-
"#,
112+
r#"struct Pair<$0T: u32>(T, T);"#,
113+
r#"struct Pair<T>(T, T) where T: u32;"#,
130114
);
131115
}
132116
}

crates/syntax/src/ast/edit_in_place.rs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl GenericParamsOwnerEdit for ast::Fn {
2727
} else {
2828
Position::last_child_of(self.syntax().clone())
2929
};
30-
create_where_clause(position)
30+
create_where_clause(position, true)
3131
}
3232
self.where_clause().unwrap()
3333
}
@@ -36,16 +36,31 @@ impl GenericParamsOwnerEdit for ast::Fn {
3636
impl GenericParamsOwnerEdit for ast::Impl {
3737
fn get_or_create_where_clause(&self) -> WhereClause {
3838
if self.where_clause().is_none() {
39-
let position = if let Some(ty) = self.self_ty() {
40-
Position::after(ty.syntax().clone())
39+
let position = if let Some(items) = self.assoc_item_list() {
40+
Position::before(items.syntax().clone())
4141
} else {
4242
Position::last_child_of(self.syntax().clone())
4343
};
44-
create_where_clause(position)
44+
create_where_clause(position, false)
4545
}
4646
self.where_clause().unwrap()
4747
}
4848
}
49+
50+
impl GenericParamsOwnerEdit for ast::Trait {
51+
fn get_or_create_where_clause(&self) -> WhereClause {
52+
if self.where_clause().is_none() {
53+
let position = if let Some(items) = self.assoc_item_list() {
54+
Position::before(items.syntax().clone())
55+
} else {
56+
Position::last_child_of(self.syntax().clone())
57+
};
58+
create_where_clause(position, false)
59+
}
60+
self.where_clause().unwrap()
61+
}
62+
}
63+
4964
impl GenericParamsOwnerEdit for ast::Struct {
5065
fn get_or_create_where_clause(&self) -> WhereClause {
5166
if self.where_clause().is_none() {
@@ -62,17 +77,36 @@ impl GenericParamsOwnerEdit for ast::Struct {
6277
} else {
6378
Position::last_child_of(self.syntax().clone())
6479
};
65-
create_where_clause(position)
80+
create_where_clause(position, true)
81+
}
82+
self.where_clause().unwrap()
83+
}
84+
}
85+
86+
impl GenericParamsOwnerEdit for ast::Enum {
87+
fn get_or_create_where_clause(&self) -> WhereClause {
88+
if self.where_clause().is_none() {
89+
let position = if let Some(gpl) = self.generic_param_list() {
90+
Position::after(gpl.syntax().clone())
91+
} else if let Some(name) = self.name() {
92+
Position::after(name.syntax().clone())
93+
} else {
94+
Position::last_child_of(self.syntax().clone())
95+
};
96+
create_where_clause(position, true)
6697
}
6798
self.where_clause().unwrap()
6899
}
69100
}
70101

71-
fn create_where_clause(position: Position) {
72-
let elements = vec![
73-
make::tokens::single_space().into(),
74-
make::where_clause(empty()).clone_for_update().syntax().clone().into(),
75-
];
102+
fn create_where_clause(position: Position, after: bool) {
103+
let mut elements = vec![make::where_clause(empty()).clone_for_update().syntax().clone().into()];
104+
let ws = make::tokens::single_space().into();
105+
if after {
106+
elements.insert(0, ws)
107+
} else {
108+
elements.push(ws)
109+
}
76110
ted::insert_all(position, elements);
77111
}
78112

0 commit comments

Comments
 (0)