diff options
author | Joshua Warner <[email protected]> | 2021-06-05 05:48:32 +0100 |
---|---|---|
committer | Joshua Warner <[email protected]> | 2021-06-05 05:48:32 +0100 |
commit | ca9ffba0473cb32b06c01bc5d387e538d379f19e (patch) | |
tree | fadd1b30a9f6eefddeb024ec6e3855e965dbdf92 /crates/ide_assists/src/handlers | |
parent | d647568db161db585f4f2ee3af9160e445338ac1 (diff) |
Add assist for converting a tuple enum variant to a named variant
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r-- | crates/ide_assists/src/handlers/convert_tuple_variant_to_named_variant.rs | 515 |
1 files changed, 515 insertions, 0 deletions
diff --git a/crates/ide_assists/src/handlers/convert_tuple_variant_to_named_variant.rs b/crates/ide_assists/src/handlers/convert_tuple_variant_to_named_variant.rs new file mode 100644 index 000000000..586ad9809 --- /dev/null +++ b/crates/ide_assists/src/handlers/convert_tuple_variant_to_named_variant.rs | |||
@@ -0,0 +1,515 @@ | |||
1 | use ide_db::defs::{Definition, NameRefClass}; | ||
2 | use syntax::{ | ||
3 | ast::{self, AstNode, VisibilityOwner}, | ||
4 | match_ast, SyntaxNode, | ||
5 | }; | ||
6 | |||
7 | use crate::{assist_context::AssistBuilder, AssistContext, AssistId, AssistKind, Assists}; | ||
8 | |||
9 | // Assist: convert_tuple_variant_to_named_variant | ||
10 | // | ||
11 | // Converts tuple variant to variant with named fields. | ||
12 | // | ||
13 | // ``` | ||
14 | // enum A { | ||
15 | // Variant(usize), | ||
16 | // } | ||
17 | // | ||
18 | // impl A { | ||
19 | // fn new(value: usize) -> A { | ||
20 | // A::Variant(value) | ||
21 | // } | ||
22 | // | ||
23 | // fn new_with_default() -> A { | ||
24 | // A::new(Default::default()) | ||
25 | // } | ||
26 | // | ||
27 | // fn value(self) -> usize { | ||
28 | // match self { | ||
29 | // A::Variant(value) => value, | ||
30 | // } | ||
31 | // } | ||
32 | // } | ||
33 | // ``` | ||
34 | // -> | ||
35 | // ``` | ||
36 | // enum A { | ||
37 | // Variant { | ||
38 | // field1: usize | ||
39 | // }, | ||
40 | // } | ||
41 | // | ||
42 | // impl A { | ||
43 | // fn new(value: usize) -> A { | ||
44 | // A::Variant { | ||
45 | // field1: value, | ||
46 | // } | ||
47 | // } | ||
48 | // | ||
49 | // fn new_with_default() -> A { | ||
50 | // A::new(Default::default()) | ||
51 | // } | ||
52 | // | ||
53 | // fn value(self) -> usize { | ||
54 | // match self { | ||
55 | // A::Variant { field1: value } => value, | ||
56 | // } | ||
57 | // } | ||
58 | // } | ||
59 | // ``` | ||
60 | pub(crate) fn convert_tuple_variant_to_named_variant( | ||
61 | acc: &mut Assists, | ||
62 | ctx: &AssistContext, | ||
63 | ) -> Option<()> { | ||
64 | let variant = ctx.find_node_at_offset::<ast::Variant>()?; | ||
65 | let tuple_fields = match variant.field_list()? { | ||
66 | ast::FieldList::TupleFieldList(it) => it, | ||
67 | ast::FieldList::RecordFieldList(_) => return None, | ||
68 | }; | ||
69 | let variant_def = ctx.sema.to_def(&variant)?; | ||
70 | |||
71 | let target = variant.syntax().text_range(); | ||
72 | acc.add( | ||
73 | AssistId("convert_tuple_variant_to_named_variant", AssistKind::RefactorRewrite), | ||
74 | "Convert to named struct", | ||
75 | target, | ||
76 | |edit| { | ||
77 | let names = generate_names(tuple_fields.fields()); | ||
78 | edit_field_references(ctx, edit, tuple_fields.fields(), &names); // TODO: is this needed? | ||
79 | edit_variant_references(ctx, edit, variant_def, &names); | ||
80 | edit_variant_def(ctx, edit, tuple_fields, names); | ||
81 | }, | ||
82 | ) | ||
83 | } | ||
84 | |||
85 | fn edit_variant_def( | ||
86 | ctx: &AssistContext, | ||
87 | edit: &mut AssistBuilder, | ||
88 | tuple_fields: ast::TupleFieldList, | ||
89 | names: Vec<ast::Name>, | ||
90 | ) { | ||
91 | let record_fields = tuple_fields | ||
92 | .fields() | ||
93 | .zip(names) | ||
94 | .filter_map(|(f, name)| Some(ast::make::record_field(f.visibility(), name, f.ty()?))); | ||
95 | let record_fields = ast::make::record_field_list(record_fields); | ||
96 | let tuple_fields_text_range = tuple_fields.syntax().text_range(); | ||
97 | |||
98 | edit.edit_file(ctx.frange.file_id); | ||
99 | |||
100 | edit.insert(tuple_fields_text_range.start(), ast::make::tokens::single_space().text()); | ||
101 | |||
102 | edit.replace(tuple_fields_text_range, record_fields.to_string()); | ||
103 | } | ||
104 | |||
105 | fn edit_variant_references( | ||
106 | ctx: &AssistContext, | ||
107 | edit: &mut AssistBuilder, | ||
108 | variant: hir::Variant, | ||
109 | names: &[ast::Name], | ||
110 | ) { | ||
111 | let variant_def = Definition::ModuleDef(hir::ModuleDef::Variant(variant)); | ||
112 | let usages = variant_def.usages(&ctx.sema).include_self_refs().all(); | ||
113 | |||
114 | let edit_node = |edit: &mut AssistBuilder, node: SyntaxNode| -> Option<()> { | ||
115 | match_ast! { | ||
116 | match node { | ||
117 | ast::TupleStructPat(tuple_struct_pat) => { | ||
118 | edit.replace( | ||
119 | tuple_struct_pat.syntax().text_range(), | ||
120 | ast::make::record_pat_with_fields( | ||
121 | tuple_struct_pat.path()?, | ||
122 | ast::make::record_pat_field_list(tuple_struct_pat.fields().zip(names).map( | ||
123 | |(pat, name)| { | ||
124 | ast::make::record_pat_field( | ||
125 | ast::make::name_ref(&name.to_string()), | ||
126 | pat, | ||
127 | ) | ||
128 | }, | ||
129 | )), | ||
130 | ) | ||
131 | .to_string(), | ||
132 | ); | ||
133 | }, | ||
134 | // for tuple struct creations like Foo(42) | ||
135 | ast::CallExpr(call_expr) => { | ||
136 | let path = call_expr.syntax().descendants().find_map(ast::PathExpr::cast).and_then(|expr| expr.path())?; | ||
137 | |||
138 | // this also includes method calls like Foo::new(42), we should skip them | ||
139 | if let Some(name_ref) = path.segment().and_then(|s| s.name_ref()) { | ||
140 | match NameRefClass::classify(&ctx.sema, &name_ref) { | ||
141 | Some(NameRefClass::Definition(Definition::SelfType(_))) => {}, | ||
142 | Some(NameRefClass::Definition(def)) if def == variant_def => {}, | ||
143 | _ => return None, | ||
144 | }; | ||
145 | } | ||
146 | |||
147 | let arg_list = call_expr.syntax().descendants().find_map(ast::ArgList::cast)?; | ||
148 | |||
149 | edit.replace( | ||
150 | call_expr.syntax().text_range(), | ||
151 | ast::make::record_expr( | ||
152 | path, | ||
153 | ast::make::record_expr_field_list(arg_list.args().zip(names).map( | ||
154 | |(expr, name)| { | ||
155 | ast::make::record_expr_field( | ||
156 | ast::make::name_ref(&name.to_string()), | ||
157 | Some(expr), | ||
158 | ) | ||
159 | }, | ||
160 | )), | ||
161 | ) | ||
162 | .to_string(), | ||
163 | ); | ||
164 | }, | ||
165 | _ => return None, | ||
166 | } | ||
167 | } | ||
168 | Some(()) | ||
169 | }; | ||
170 | |||
171 | for (file_id, refs) in usages { | ||
172 | edit.edit_file(file_id); | ||
173 | for r in refs { | ||
174 | for node in r.name.syntax().ancestors() { | ||
175 | if edit_node(edit, node).is_some() { | ||
176 | break; | ||
177 | } | ||
178 | } | ||
179 | } | ||
180 | } | ||
181 | } | ||
182 | |||
183 | fn edit_field_references( | ||
184 | ctx: &AssistContext, | ||
185 | edit: &mut AssistBuilder, | ||
186 | fields: impl Iterator<Item = ast::TupleField>, | ||
187 | names: &[ast::Name], | ||
188 | ) { | ||
189 | for (field, name) in fields.zip(names) { | ||
190 | let field = match ctx.sema.to_def(&field) { | ||
191 | Some(it) => it, | ||
192 | None => continue, | ||
193 | }; | ||
194 | let def = Definition::Field(field); | ||
195 | let usages = def.usages(&ctx.sema).all(); | ||
196 | for (file_id, refs) in usages { | ||
197 | edit.edit_file(file_id); | ||
198 | for r in refs { | ||
199 | if let Some(name_ref) = r.name.as_name_ref() { | ||
200 | edit.replace(name_ref.syntax().text_range(), name.text()); | ||
201 | } | ||
202 | } | ||
203 | } | ||
204 | } | ||
205 | } | ||
206 | |||
207 | fn generate_names(fields: impl Iterator<Item = ast::TupleField>) -> Vec<ast::Name> { | ||
208 | fields.enumerate().map(|(i, _)| ast::make::name(&format!("field{}", i + 1))).collect() | ||
209 | } | ||
210 | |||
211 | #[cfg(test)] | ||
212 | mod tests { | ||
213 | use crate::tests::{check_assist, check_assist_not_applicable}; | ||
214 | |||
215 | use super::*; | ||
216 | |||
217 | #[test] | ||
218 | fn not_applicable_other_than_tuple_variant() { | ||
219 | check_assist_not_applicable( | ||
220 | convert_tuple_variant_to_named_variant, | ||
221 | r#"enum Enum { Variant$0 { value: usize } };"#, | ||
222 | ); | ||
223 | check_assist_not_applicable(convert_tuple_variant_to_named_variant, r#"enum Enum { Variant$0 }"#); | ||
224 | } | ||
225 | |||
226 | #[test] | ||
227 | fn convert_simple_variant() { | ||
228 | check_assist( | ||
229 | convert_tuple_variant_to_named_variant, | ||
230 | r#" | ||
231 | enum A { | ||
232 | $0Variant(usize), | ||
233 | } | ||
234 | |||
235 | impl A { | ||
236 | fn new(value: usize) -> A { | ||
237 | A::Variant(value) | ||
238 | } | ||
239 | |||
240 | fn new_with_default() -> A { | ||
241 | A::new(Default::default()) | ||
242 | } | ||
243 | |||
244 | fn value(self) -> usize { | ||
245 | match self { | ||
246 | A::Variant(value) => value, | ||
247 | } | ||
248 | } | ||
249 | }"#, | ||
250 | r#" | ||
251 | enum A { | ||
252 | Variant { field1: usize }, | ||
253 | } | ||
254 | |||
255 | impl A { | ||
256 | fn new(value: usize) -> A { | ||
257 | A::Variant { field1: value } | ||
258 | } | ||
259 | |||
260 | fn new_with_default() -> A { | ||
261 | A::new(Default::default()) | ||
262 | } | ||
263 | |||
264 | fn value(self) -> usize { | ||
265 | match self { | ||
266 | A::Variant { field1: value } => value, | ||
267 | } | ||
268 | } | ||
269 | }"#, | ||
270 | ); | ||
271 | } | ||
272 | |||
273 | #[test] | ||
274 | fn convert_variant_referenced_via_self_kw() { | ||
275 | check_assist( | ||
276 | convert_tuple_variant_to_named_variant, | ||
277 | r#" | ||
278 | enum A { | ||
279 | $0Variant(usize), | ||
280 | } | ||
281 | |||
282 | impl A { | ||
283 | fn new(value: usize) -> A { | ||
284 | Self::Variant(value) | ||
285 | } | ||
286 | |||
287 | fn new_with_default() -> A { | ||
288 | Self::new(Default::default()) | ||
289 | } | ||
290 | |||
291 | fn value(self) -> usize { | ||
292 | match self { | ||
293 | Self::Variant(value) => value, | ||
294 | } | ||
295 | } | ||
296 | }"#, | ||
297 | r#" | ||
298 | enum A { | ||
299 | Variant { field1: usize }, | ||
300 | } | ||
301 | |||
302 | impl A { | ||
303 | fn new(value: usize) -> A { | ||
304 | Self::Variant { field1: value } | ||
305 | } | ||
306 | |||
307 | fn new_with_default() -> A { | ||
308 | Self::new(Default::default()) | ||
309 | } | ||
310 | |||
311 | fn value(self) -> usize { | ||
312 | match self { | ||
313 | Self::Variant { field1: value } => value, | ||
314 | } | ||
315 | } | ||
316 | }"#, | ||
317 | ); | ||
318 | } | ||
319 | |||
320 | #[test] | ||
321 | fn convert_destructured_variant() { | ||
322 | check_assist( | ||
323 | convert_tuple_variant_to_named_variant, | ||
324 | r#" | ||
325 | enum A { | ||
326 | $0Variant(usize), | ||
327 | } | ||
328 | |||
329 | impl A { | ||
330 | fn into_inner(self) -> usize { | ||
331 | let A::Variant(first) = self; | ||
332 | first | ||
333 | } | ||
334 | |||
335 | fn into_inner_via_self(self) -> usize { | ||
336 | let Self::Variant(first) = self; | ||
337 | first | ||
338 | } | ||
339 | }"#, | ||
340 | r#" | ||
341 | enum A { | ||
342 | Variant { field1: usize }, | ||
343 | } | ||
344 | |||
345 | impl A { | ||
346 | fn into_inner(self) -> usize { | ||
347 | let A::Variant { field1: first } = self; | ||
348 | first | ||
349 | } | ||
350 | |||
351 | fn into_inner_via_self(self) -> usize { | ||
352 | let Self::Variant { field1: first } = self; | ||
353 | first | ||
354 | } | ||
355 | }"#, | ||
356 | ); | ||
357 | } | ||
358 | |||
359 | #[test] | ||
360 | fn convert_variant_with_wrapped_references() { | ||
361 | check_assist( | ||
362 | convert_tuple_variant_to_named_variant, | ||
363 | r#" | ||
364 | enum Inner { | ||
365 | $0Variant(usize), | ||
366 | } | ||
367 | enum Outer { | ||
368 | Variant(Inner), | ||
369 | } | ||
370 | |||
371 | impl Outer { | ||
372 | fn new() -> Self { | ||
373 | Self::Variant(Inner::Variant(42)) | ||
374 | } | ||
375 | |||
376 | fn into_inner_destructed(self) -> u32 { | ||
377 | let Outer::Variant(Inner::Variant(x)) = self; | ||
378 | x | ||
379 | } | ||
380 | }"#, | ||
381 | r#" | ||
382 | enum Inner { | ||
383 | Variant { field1: usize }, | ||
384 | } | ||
385 | enum Outer { | ||
386 | Variant(Inner), | ||
387 | } | ||
388 | |||
389 | impl Outer { | ||
390 | fn new() -> Self { | ||
391 | Self::Variant(Inner::Variant { field1: 42 }) | ||
392 | } | ||
393 | |||
394 | fn into_inner_destructed(self) -> u32 { | ||
395 | let Outer::Variant(Inner::Variant { field1: x }) = self; | ||
396 | x | ||
397 | } | ||
398 | }"#, | ||
399 | ); | ||
400 | |||
401 | check_assist( | ||
402 | convert_tuple_variant_to_named_variant, | ||
403 | r#" | ||
404 | enum Inner { | ||
405 | Variant(usize), | ||
406 | } | ||
407 | enum Outer { | ||
408 | $0Variant(Inner), | ||
409 | } | ||
410 | |||
411 | impl Outer { | ||
412 | fn new() -> Self { | ||
413 | Self::Variant(Inner::Variant(42)) | ||
414 | } | ||
415 | |||
416 | fn into_inner_destructed(self) -> u32 { | ||
417 | let Outer::Variant(Inner::Variant(x)) = self; | ||
418 | x | ||
419 | } | ||
420 | }"#, | ||
421 | r#" | ||
422 | enum Inner { | ||
423 | Variant(usize), | ||
424 | } | ||
425 | enum Outer { | ||
426 | Variant { field1: Inner }, | ||
427 | } | ||
428 | |||
429 | impl Outer { | ||
430 | fn new() -> Self { | ||
431 | Self::Variant { field1: Inner::Variant(42) } | ||
432 | } | ||
433 | |||
434 | fn into_inner_destructed(self) -> u32 { | ||
435 | let Outer::Variant { field1: Inner::Variant(x) } = self; | ||
436 | x | ||
437 | } | ||
438 | }"#, | ||
439 | ); | ||
440 | } | ||
441 | |||
442 | #[test] | ||
443 | fn convert_variant_with_multi_file_references() { | ||
444 | check_assist( | ||
445 | convert_tuple_variant_to_named_variant, | ||
446 | r#" | ||
447 | //- /main.rs | ||
448 | struct Inner; | ||
449 | enum A { | ||
450 | $0Variant(Inner), | ||
451 | } | ||
452 | |||
453 | mod foo; | ||
454 | |||
455 | //- /foo.rs | ||
456 | use crate::{A, Inner}; | ||
457 | fn f() { | ||
458 | let a = A::Variant(Inner); | ||
459 | } | ||
460 | "#, | ||
461 | r#" | ||
462 | //- /main.rs | ||
463 | struct Inner; | ||
464 | enum A { | ||
465 | Variant { field1: Inner }, | ||
466 | } | ||
467 | |||
468 | mod foo; | ||
469 | |||
470 | //- /foo.rs | ||
471 | use crate::{A, Inner}; | ||
472 | fn f() { | ||
473 | let a = A::Variant { field1: Inner }; | ||
474 | } | ||
475 | "#, | ||
476 | ); | ||
477 | } | ||
478 | |||
479 | #[test] | ||
480 | fn convert_directly_used_variant() { | ||
481 | check_assist( | ||
482 | convert_tuple_variant_to_named_variant, | ||
483 | r#" | ||
484 | //- /main.rs | ||
485 | struct Inner; | ||
486 | enum A { | ||
487 | $0Variant(Inner), | ||
488 | } | ||
489 | |||
490 | mod foo; | ||
491 | |||
492 | //- /foo.rs | ||
493 | use crate::{A::Variant, Inner}; | ||
494 | fn f() { | ||
495 | let a = Variant(Inner); | ||
496 | } | ||
497 | "#, | ||
498 | r#" | ||
499 | //- /main.rs | ||
500 | struct Inner; | ||
501 | enum A { | ||
502 | Variant { field1: Inner }, | ||
503 | } | ||
504 | |||
505 | mod foo; | ||
506 | |||
507 | //- /foo.rs | ||
508 | use crate::{A::Variant, Inner}; | ||
509 | fn f() { | ||
510 | let a = Variant { field1: Inner }; | ||
511 | } | ||
512 | "#, | ||
513 | ); | ||
514 | } | ||
515 | } | ||