aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers/move_bounds.rs
diff options
context:
space:
mode:
authorZac Pullar-Strecker <[email protected]>2020-08-24 10:19:53 +0100
committerZac Pullar-Strecker <[email protected]>2020-08-24 10:20:13 +0100
commit7bbca7a1b3f9293d2f5cc5745199bc5f8396f2f0 (patch)
treebdb47765991cb973b2cd5481a088fac636bd326c /crates/assists/src/handlers/move_bounds.rs
parentca464650eeaca6195891199a93f4f76cf3e7e697 (diff)
parente65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff)
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Diffstat (limited to 'crates/assists/src/handlers/move_bounds.rs')
-rw-r--r--crates/assists/src/handlers/move_bounds.rs152
1 files changed, 152 insertions, 0 deletions
diff --git a/crates/assists/src/handlers/move_bounds.rs b/crates/assists/src/handlers/move_bounds.rs
new file mode 100644
index 000000000..e2e461520
--- /dev/null
+++ b/crates/assists/src/handlers/move_bounds.rs
@@ -0,0 +1,152 @@
1use syntax::{
2 ast::{self, edit::AstNodeEdit, make, AstNode, NameOwner, TypeBoundsOwner},
3 match_ast,
4 SyntaxKind::*,
5 T,
6};
7
8use crate::{AssistContext, AssistId, AssistKind, Assists};
9
10// Assist: move_bounds_to_where_clause
11//
12// Moves inline type bounds to a where clause.
13//
14// ```
15// fn apply<T, U, <|>F: FnOnce(T) -> U>(f: F, x: T) -> U {
16// f(x)
17// }
18// ```
19// ->
20// ```
21// fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
22// f(x)
23// }
24// ```
25pub(crate) fn move_bounds_to_where_clause(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
26 let type_param_list = ctx.find_node_at_offset::<ast::GenericParamList>()?;
27
28 let mut type_params = type_param_list.type_params();
29 if type_params.all(|p| p.type_bound_list().is_none()) {
30 return None;
31 }
32
33 let parent = type_param_list.syntax().parent()?;
34 if parent.children_with_tokens().any(|it| it.kind() == WHERE_CLAUSE) {
35 return None;
36 }
37
38 let anchor = match_ast! {
39 match parent {
40 ast::Fn(it) => it.body()?.syntax().clone().into(),
41 ast::Trait(it) => it.assoc_item_list()?.syntax().clone().into(),
42 ast::Impl(it) => it.assoc_item_list()?.syntax().clone().into(),
43 ast::Enum(it) => it.variant_list()?.syntax().clone().into(),
44 ast::Struct(it) => {
45 it.syntax().children_with_tokens()
46 .find(|it| it.kind() == RECORD_FIELD_LIST || it.kind() == T![;])?
47 },
48 _ => return None
49 }
50 };
51
52 let target = type_param_list.syntax().text_range();
53 acc.add(
54 AssistId("move_bounds_to_where_clause", AssistKind::RefactorRewrite),
55 "Move to where clause",
56 target,
57 |edit| {
58 let new_params = type_param_list
59 .type_params()
60 .filter(|it| it.type_bound_list().is_some())
61 .map(|type_param| {
62 let without_bounds = type_param.remove_bounds();
63 (type_param, without_bounds)
64 });
65
66 let new_type_param_list = type_param_list.replace_descendants(new_params);
67 edit.replace_ast(type_param_list.clone(), new_type_param_list);
68
69 let where_clause = {
70 let predicates = type_param_list.type_params().filter_map(build_predicate);
71 make::where_clause(predicates)
72 };
73
74 let to_insert = match anchor.prev_sibling_or_token() {
75 Some(ref elem) if elem.kind() == WHITESPACE => {
76 format!("{} ", where_clause.syntax())
77 }
78 _ => format!(" {}", where_clause.syntax()),
79 };
80 edit.insert(anchor.text_range().start(), to_insert);
81 },
82 )
83}
84
85fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> {
86 let path = {
87 let name_ref = make::name_ref(&param.name()?.syntax().to_string());
88 let segment = make::path_segment(name_ref);
89 make::path_unqualified(segment)
90 };
91 let predicate = make::where_pred(path, param.type_bound_list()?.bounds());
92 Some(predicate)
93}
94
95#[cfg(test)]
96mod tests {
97 use super::*;
98
99 use crate::tests::check_assist;
100
101 #[test]
102 fn move_bounds_to_where_clause_fn() {
103 check_assist(
104 move_bounds_to_where_clause,
105 r#"
106 fn foo<T: u32, <|>F: FnOnce(T) -> T>() {}
107 "#,
108 r#"
109 fn foo<T, F>() where T: u32, F: FnOnce(T) -> T {}
110 "#,
111 );
112 }
113
114 #[test]
115 fn move_bounds_to_where_clause_impl() {
116 check_assist(
117 move_bounds_to_where_clause,
118 r#"
119 impl<U: u32, <|>T> A<U, T> {}
120 "#,
121 r#"
122 impl<U, T> A<U, T> where U: u32 {}
123 "#,
124 );
125 }
126
127 #[test]
128 fn move_bounds_to_where_clause_struct() {
129 check_assist(
130 move_bounds_to_where_clause,
131 r#"
132 struct A<<|>T: Iterator<Item = u32>> {}
133 "#,
134 r#"
135 struct A<T> where T: Iterator<Item = u32> {}
136 "#,
137 );
138 }
139
140 #[test]
141 fn move_bounds_to_where_clause_tuple_struct() {
142 check_assist(
143 move_bounds_to_where_clause,
144 r#"
145 struct Pair<<|>T: u32>(T, T);
146 "#,
147 r#"
148 struct Pair<T>(T, T) where T: u32;
149 "#,
150 );
151 }
152}