aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-03-16 19:28:47 +0000
committerGitHub <[email protected]>2021-03-16 19:28:47 +0000
commit9375207461dc69d622ecc46e924be70c08339d33 (patch)
tree2f1c13483133277b4bc79adac16559d4e2a58418 /crates
parent4771a5f1ca810a3f3697aea8da5af6dc8c03bbd1 (diff)
parentd733c9bdad81e23959b1a43421a9fa6ea92eda9f (diff)
Merge #8060
8060: Move more bounds r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ide_assists/src/handlers/move_bounds.rs36
-rw-r--r--crates/syntax/src/ast/edit_in_place.rs54
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
40 let where_clause: ast::WhereClause = match_ast! { 40 let where_clause: ast::WhereClause = match_ast! {
41 match parent { 41 match parent {
42 ast::Fn(it) => it.get_or_create_where_clause(), 42 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(),
44 ast::Impl(it) => it.get_or_create_where_clause(), 44 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(),
46 ast::Struct(it) => it.get_or_create_where_clause(), 46 ast::Struct(it) => it.get_or_create_where_clause(),
47 _ => return, 47 _ => return,
48 } 48 }
@@ -82,12 +82,8 @@ mod tests {
82 fn move_bounds_to_where_clause_fn() { 82 fn move_bounds_to_where_clause_fn() {
83 check_assist( 83 check_assist(
84 move_bounds_to_where_clause, 84 move_bounds_to_where_clause,
85 r#" 85 r#"fn foo<T: u32, $0F: FnOnce(T) -> T>() {}"#,
86 fn foo<T: u32, $0F: FnOnce(T) -> T>() {} 86 r#"fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}"#,
87 "#,
88 r#"
89 fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
90 "#,
91 ); 87 );
92 } 88 }
93 89
@@ -95,12 +91,8 @@ mod tests {
95 fn move_bounds_to_where_clause_impl() { 91 fn move_bounds_to_where_clause_impl() {
96 check_assist( 92 check_assist(
97 move_bounds_to_where_clause, 93 move_bounds_to_where_clause,
98 r#" 94 r#"impl<U: u32, $0T> A<U, T> {}"#,
99 impl<U: u32, $0T> A<U, T> {} 95 r#"impl<U, T> A<U, T> where U: u32 {}"#,
100 "#,
101 r#"
102 impl<U, T> A<U, T> where U: u32 {}
103 "#,
104 ); 96 );
105 } 97 }
106 98
@@ -108,12 +100,8 @@ mod tests {
108 fn move_bounds_to_where_clause_struct() { 100 fn move_bounds_to_where_clause_struct() {
109 check_assist( 101 check_assist(
110 move_bounds_to_where_clause, 102 move_bounds_to_where_clause,
111 r#" 103 r#"struct A<$0T: Iterator<Item = u32>> {}"#,
112 struct A<$0T: Iterator<Item = u32>> {} 104 r#"struct A<T> where T: Iterator<Item = u32> {}"#,
113 "#,
114 r#"
115 struct A<T> where T: Iterator<Item = u32> {}
116 "#,
117 ); 105 );
118 } 106 }
119 107
@@ -121,12 +109,8 @@ mod tests {
121 fn move_bounds_to_where_clause_tuple_struct() { 109 fn move_bounds_to_where_clause_tuple_struct() {
122 check_assist( 110 check_assist(
123 move_bounds_to_where_clause, 111 move_bounds_to_where_clause,
124 r#" 112 r#"struct Pair<$0T: u32>(T, T);"#,
125 struct Pair<$0T: u32>(T, T); 113 r#"struct Pair<T>(T, T) where T: u32;"#,
126 "#,
127 r#"
128 struct Pair<T>(T, T) where T: u32;
129 "#,
130 ); 114 );
131 } 115 }
132} 116}
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 {
27 } else { 27 } else {
28 Position::last_child_of(self.syntax().clone()) 28 Position::last_child_of(self.syntax().clone())
29 }; 29 };
30 create_where_clause(position) 30 create_where_clause(position, true)
31 } 31 }
32 self.where_clause().unwrap() 32 self.where_clause().unwrap()
33 } 33 }
@@ -36,16 +36,31 @@ impl GenericParamsOwnerEdit for ast::Fn {
36impl GenericParamsOwnerEdit for ast::Impl { 36impl GenericParamsOwnerEdit for ast::Impl {
37 fn get_or_create_where_clause(&self) -> WhereClause { 37 fn get_or_create_where_clause(&self) -> WhereClause {
38 if self.where_clause().is_none() { 38 if self.where_clause().is_none() {
39 let position = if let Some(ty) = self.self_ty() { 39 let position = if let Some(items) = self.assoc_item_list() {
40 Position::after(ty.syntax().clone()) 40 Position::before(items.syntax().clone())
41 } else { 41 } else {
42 Position::last_child_of(self.syntax().clone()) 42 Position::last_child_of(self.syntax().clone())
43 }; 43 };
44 create_where_clause(position) 44 create_where_clause(position, false)
45 } 45 }
46 self.where_clause().unwrap() 46 self.where_clause().unwrap()
47 } 47 }
48} 48}
49
50impl 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
49impl GenericParamsOwnerEdit for ast::Struct { 64impl GenericParamsOwnerEdit for ast::Struct {
50 fn get_or_create_where_clause(&self) -> WhereClause { 65 fn get_or_create_where_clause(&self) -> WhereClause {
51 if self.where_clause().is_none() { 66 if self.where_clause().is_none() {
@@ -62,17 +77,36 @@ impl GenericParamsOwnerEdit for ast::Struct {
62 } else { 77 } else {
63 Position::last_child_of(self.syntax().clone()) 78 Position::last_child_of(self.syntax().clone())
64 }; 79 };
65 create_where_clause(position) 80 create_where_clause(position, true)
81 }
82 self.where_clause().unwrap()
83 }
84}
85
86impl 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)
66 } 97 }
67 self.where_clause().unwrap() 98 self.where_clause().unwrap()
68 } 99 }
69} 100}
70 101
71fn create_where_clause(position: Position) { 102fn create_where_clause(position: Position, after: bool) {
72 let elements = vec![ 103 let mut elements = vec![make::where_clause(empty()).clone_for_update().syntax().clone().into()];
73 make::tokens::single_space().into(), 104 let ws = make::tokens::single_space().into();
74 make::where_clause(empty()).clone_for_update().syntax().clone().into(), 105 if after {
75 ]; 106 elements.insert(0, ws)
107 } else {
108 elements.push(ws)
109 }
76 ted::insert_all(position, elements); 110 ted::insert_all(position, elements);
77} 111}
78 112