diff options
Diffstat (limited to 'crates/assists')
-rw-r--r-- | crates/assists/src/handlers/add_lifetime_to_type.rs | 228 | ||||
-rw-r--r-- | crates/assists/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/assists/src/tests/generated.rs | 19 |
3 files changed, 249 insertions, 0 deletions
diff --git a/crates/assists/src/handlers/add_lifetime_to_type.rs b/crates/assists/src/handlers/add_lifetime_to_type.rs new file mode 100644 index 000000000..c1603e972 --- /dev/null +++ b/crates/assists/src/handlers/add_lifetime_to_type.rs | |||
@@ -0,0 +1,228 @@ | |||
1 | use ast::FieldList; | ||
2 | use syntax::ast::{self, AstNode, GenericParamsOwner, NameOwner, RefType, Type}; | ||
3 | |||
4 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | ||
5 | |||
6 | // Assist: add_lifetime_to_type | ||
7 | // | ||
8 | // Adds a new lifetime to a struct, enum or union. | ||
9 | // | ||
10 | // ``` | ||
11 | // struct Point { | ||
12 | // x: &$0u32, | ||
13 | // y: u32, | ||
14 | // } | ||
15 | // ``` | ||
16 | // -> | ||
17 | // ``` | ||
18 | // struct Point<'a> { | ||
19 | // x: &'a u32, | ||
20 | // y: u32, | ||
21 | // } | ||
22 | // ``` | ||
23 | pub(crate) fn add_lifetime_to_type(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
24 | let ref_type_focused = ctx.find_node_at_offset::<ast::RefType>()?; | ||
25 | if ref_type_focused.lifetime().is_some() { | ||
26 | return None; | ||
27 | } | ||
28 | |||
29 | let node = ctx.find_node_at_offset::<ast::AdtDef>()?; | ||
30 | let has_lifetime = node | ||
31 | .generic_param_list() | ||
32 | .map(|gen_list| gen_list.lifetime_params().count() > 0) | ||
33 | .unwrap_or_default(); | ||
34 | |||
35 | if has_lifetime { | ||
36 | return None; | ||
37 | } | ||
38 | |||
39 | let ref_types = fetch_borrowed_types(&node)?; | ||
40 | let target = node.syntax().text_range(); | ||
41 | |||
42 | acc.add( | ||
43 | AssistId("add_lifetime_to_type", AssistKind::Generate), | ||
44 | "Add lifetime`", | ||
45 | target, | ||
46 | |builder| { | ||
47 | match node.generic_param_list() { | ||
48 | Some(gen_param) => { | ||
49 | if let Some(left_angle) = gen_param.l_angle_token() { | ||
50 | builder.insert(left_angle.text_range().end(), "'a, "); | ||
51 | } | ||
52 | } | ||
53 | None => { | ||
54 | if let Some(name) = node.name() { | ||
55 | builder.insert(name.syntax().text_range().end(), "<'a>"); | ||
56 | } | ||
57 | } | ||
58 | } | ||
59 | |||
60 | for ref_type in ref_types { | ||
61 | if let Some(amp_token) = ref_type.amp_token() { | ||
62 | builder.insert(amp_token.text_range().end(), "'a "); | ||
63 | } | ||
64 | } | ||
65 | }, | ||
66 | ) | ||
67 | } | ||
68 | |||
69 | fn fetch_borrowed_types(node: &ast::AdtDef) -> Option<Vec<RefType>> { | ||
70 | let ref_types: Vec<RefType> = match node { | ||
71 | ast::AdtDef::Enum(enum_) => { | ||
72 | let variant_list = enum_.variant_list()?; | ||
73 | variant_list | ||
74 | .variants() | ||
75 | .filter_map(|variant| { | ||
76 | let field_list = variant.field_list()?; | ||
77 | |||
78 | find_ref_types_from_field_list(&field_list) | ||
79 | }) | ||
80 | .flatten() | ||
81 | .collect() | ||
82 | } | ||
83 | ast::AdtDef::Struct(strukt) => { | ||
84 | let field_list = strukt.field_list()?; | ||
85 | find_ref_types_from_field_list(&field_list)? | ||
86 | } | ||
87 | ast::AdtDef::Union(un) => { | ||
88 | let record_field_list = un.record_field_list()?; | ||
89 | record_field_list | ||
90 | .fields() | ||
91 | .filter_map(|r_field| { | ||
92 | if let Type::RefType(ref_type) = r_field.ty()? { | ||
93 | if ref_type.lifetime().is_none() { | ||
94 | return Some(ref_type); | ||
95 | } | ||
96 | } | ||
97 | |||
98 | None | ||
99 | }) | ||
100 | .collect() | ||
101 | } | ||
102 | }; | ||
103 | |||
104 | if ref_types.is_empty() { | ||
105 | None | ||
106 | } else { | ||
107 | Some(ref_types) | ||
108 | } | ||
109 | } | ||
110 | |||
111 | fn find_ref_types_from_field_list(field_list: &FieldList) -> Option<Vec<RefType>> { | ||
112 | let ref_types: Vec<RefType> = match field_list { | ||
113 | ast::FieldList::RecordFieldList(record_list) => record_list | ||
114 | .fields() | ||
115 | .filter_map(|f| { | ||
116 | if let Type::RefType(ref_type) = f.ty()? { | ||
117 | if ref_type.lifetime().is_none() { | ||
118 | return Some(ref_type); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | None | ||
123 | }) | ||
124 | .collect(), | ||
125 | ast::FieldList::TupleFieldList(tuple_field_list) => tuple_field_list | ||
126 | .fields() | ||
127 | .filter_map(|f| { | ||
128 | if let Type::RefType(ref_type) = f.ty()? { | ||
129 | if ref_type.lifetime().is_none() { | ||
130 | return Some(ref_type); | ||
131 | } | ||
132 | } | ||
133 | |||
134 | None | ||
135 | }) | ||
136 | .collect(), | ||
137 | }; | ||
138 | |||
139 | if ref_types.is_empty() { | ||
140 | None | ||
141 | } else { | ||
142 | Some(ref_types) | ||
143 | } | ||
144 | } | ||
145 | |||
146 | #[cfg(test)] | ||
147 | mod tests { | ||
148 | use crate::tests::{check_assist, check_assist_not_applicable}; | ||
149 | |||
150 | use super::*; | ||
151 | |||
152 | #[test] | ||
153 | fn add_lifetime_to_struct() { | ||
154 | check_assist( | ||
155 | add_lifetime_to_type, | ||
156 | "struct Foo { a: &$0i32 }", | ||
157 | "struct Foo<'a> { a: &'a i32 }", | ||
158 | ); | ||
159 | |||
160 | check_assist( | ||
161 | add_lifetime_to_type, | ||
162 | "struct Foo { a: &$0i32, b: &usize }", | ||
163 | "struct Foo<'a> { a: &'a i32, b: &'a usize }", | ||
164 | ); | ||
165 | |||
166 | check_assist( | ||
167 | add_lifetime_to_type, | ||
168 | "struct Foo { a: &$0i32, b: usize }", | ||
169 | "struct Foo<'a> { a: &'a i32, b: usize }", | ||
170 | ); | ||
171 | |||
172 | check_assist( | ||
173 | add_lifetime_to_type, | ||
174 | "struct Foo<T> { a: &$0T, b: usize }", | ||
175 | "struct Foo<'a, T> { a: &'a T, b: usize }", | ||
176 | ); | ||
177 | |||
178 | check_assist_not_applicable(add_lifetime_to_type, "struct Foo<'a> { a: &$0'a i32 }"); | ||
179 | check_assist_not_applicable(add_lifetime_to_type, "struct Foo { a: &'a$0 i32 }"); | ||
180 | } | ||
181 | |||
182 | #[test] | ||
183 | fn add_lifetime_to_enum() { | ||
184 | check_assist( | ||
185 | add_lifetime_to_type, | ||
186 | "enum Foo { Bar { a: i32 }, Other, Tuple(u32, &$0u32)}", | ||
187 | "enum Foo<'a> { Bar { a: i32 }, Other, Tuple(u32, &'a u32)}", | ||
188 | ); | ||
189 | |||
190 | check_assist( | ||
191 | add_lifetime_to_type, | ||
192 | "enum Foo { Bar { a: &$0i32 }}", | ||
193 | "enum Foo<'a> { Bar { a: &'a i32 }}", | ||
194 | ); | ||
195 | |||
196 | check_assist( | ||
197 | add_lifetime_to_type, | ||
198 | "enum Foo<T> { Bar { a: &$0i32, b: &T }}", | ||
199 | "enum Foo<'a, T> { Bar { a: &'a i32, b: &'a T }}", | ||
200 | ); | ||
201 | |||
202 | check_assist_not_applicable(add_lifetime_to_type, "enum Foo<'a> { Bar { a: &$0'a i32 }}"); | ||
203 | check_assist_not_applicable(add_lifetime_to_type, "enum Foo { Bar, $0Misc }"); | ||
204 | } | ||
205 | |||
206 | #[test] | ||
207 | fn add_lifetime_to_union() { | ||
208 | check_assist( | ||
209 | add_lifetime_to_type, | ||
210 | "union Foo { a: &$0i32 }", | ||
211 | "union Foo<'a> { a: &'a i32 }", | ||
212 | ); | ||
213 | |||
214 | check_assist( | ||
215 | add_lifetime_to_type, | ||
216 | "union Foo { a: &$0i32, b: &usize }", | ||
217 | "union Foo<'a> { a: &'a i32, b: &'a usize }", | ||
218 | ); | ||
219 | |||
220 | check_assist( | ||
221 | add_lifetime_to_type, | ||
222 | "union Foo<T> { a: &$0T, b: usize }", | ||
223 | "union Foo<'a, T> { a: &'a T, b: usize }", | ||
224 | ); | ||
225 | |||
226 | check_assist_not_applicable(add_lifetime_to_type, "struct Foo<'a> { a: &'a $0i32 }"); | ||
227 | } | ||
228 | } | ||
diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs index 14178a651..559b9651e 100644 --- a/crates/assists/src/lib.rs +++ b/crates/assists/src/lib.rs | |||
@@ -108,6 +108,7 @@ mod handlers { | |||
108 | pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>; | 108 | pub(crate) type Handler = fn(&mut Assists, &AssistContext) -> Option<()>; |
109 | 109 | ||
110 | mod add_explicit_type; | 110 | mod add_explicit_type; |
111 | mod add_lifetime_to_type; | ||
111 | mod add_missing_impl_members; | 112 | mod add_missing_impl_members; |
112 | mod add_turbo_fish; | 113 | mod add_turbo_fish; |
113 | mod apply_demorgan; | 114 | mod apply_demorgan; |
@@ -164,6 +165,7 @@ mod handlers { | |||
164 | &[ | 165 | &[ |
165 | // These are alphabetic for the foolish consistency | 166 | // These are alphabetic for the foolish consistency |
166 | add_explicit_type::add_explicit_type, | 167 | add_explicit_type::add_explicit_type, |
168 | add_lifetime_to_type::add_lifetime_to_type, | ||
167 | add_turbo_fish::add_turbo_fish, | 169 | add_turbo_fish::add_turbo_fish, |
168 | apply_demorgan::apply_demorgan, | 170 | apply_demorgan::apply_demorgan, |
169 | auto_import::auto_import, | 171 | auto_import::auto_import, |
diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs index d48d063b4..9aa807f10 100644 --- a/crates/assists/src/tests/generated.rs +++ b/crates/assists/src/tests/generated.rs | |||
@@ -104,6 +104,25 @@ impl Trait<u32> for () { | |||
104 | } | 104 | } |
105 | 105 | ||
106 | #[test] | 106 | #[test] |
107 | fn doctest_add_lifetime_to_type() { | ||
108 | check_doc_test( | ||
109 | "add_lifetime_to_type", | ||
110 | r#####" | ||
111 | struct Point { | ||
112 | x: &$0u32, | ||
113 | y: u32, | ||
114 | } | ||
115 | "#####, | ||
116 | r#####" | ||
117 | struct Point<'a> { | ||
118 | x: &'a u32, | ||
119 | y: u32, | ||
120 | } | ||
121 | "#####, | ||
122 | ) | ||
123 | } | ||
124 | |||
125 | #[test] | ||
107 | fn doctest_add_turbo_fish() { | 126 | fn doctest_add_turbo_fish() { |
108 | check_doc_test( | 127 | check_doc_test( |
109 | "add_turbo_fish", | 128 | "add_turbo_fish", |