aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers/add_missing_impl_members.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/assists/src/handlers/add_missing_impl_members.rs')
-rw-r--r--crates/assists/src/handlers/add_missing_impl_members.rs711
1 files changed, 711 insertions, 0 deletions
diff --git a/crates/assists/src/handlers/add_missing_impl_members.rs b/crates/assists/src/handlers/add_missing_impl_members.rs
new file mode 100644
index 000000000..81b61ebf8
--- /dev/null
+++ b/crates/assists/src/handlers/add_missing_impl_members.rs
@@ -0,0 +1,711 @@
1use hir::HasSource;
2use syntax::{
3 ast::{
4 self,
5 edit::{self, AstNodeEdit, IndentLevel},
6 make, AstNode, NameOwner,
7 },
8 SmolStr,
9};
10
11use crate::{
12 assist_context::{AssistContext, Assists},
13 ast_transform::{self, AstTransform, QualifyPaths, SubstituteTypeParams},
14 utils::{get_missing_assoc_items, render_snippet, resolve_target_trait, Cursor},
15 AssistId, AssistKind,
16};
17
18#[derive(PartialEq)]
19enum AddMissingImplMembersMode {
20 DefaultMethodsOnly,
21 NoDefaultMethods,
22}
23
24// Assist: add_impl_missing_members
25//
26// Adds scaffold for required impl members.
27//
28// ```
29// trait Trait<T> {
30// Type X;
31// fn foo(&self) -> T;
32// fn bar(&self) {}
33// }
34//
35// impl Trait<u32> for () {<|>
36//
37// }
38// ```
39// ->
40// ```
41// trait Trait<T> {
42// Type X;
43// fn foo(&self) -> T;
44// fn bar(&self) {}
45// }
46//
47// impl Trait<u32> for () {
48// fn foo(&self) -> u32 {
49// ${0:todo!()}
50// }
51//
52// }
53// ```
54pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
55 add_missing_impl_members_inner(
56 acc,
57 ctx,
58 AddMissingImplMembersMode::NoDefaultMethods,
59 "add_impl_missing_members",
60 "Implement missing members",
61 )
62}
63
64// Assist: add_impl_default_members
65//
66// Adds scaffold for overriding default impl members.
67//
68// ```
69// trait Trait {
70// Type X;
71// fn foo(&self);
72// fn bar(&self) {}
73// }
74//
75// impl Trait for () {
76// Type X = ();
77// fn foo(&self) {}<|>
78//
79// }
80// ```
81// ->
82// ```
83// trait Trait {
84// Type X;
85// fn foo(&self);
86// fn bar(&self) {}
87// }
88//
89// impl Trait for () {
90// Type X = ();
91// fn foo(&self) {}
92// $0fn bar(&self) {}
93//
94// }
95// ```
96pub(crate) fn add_missing_default_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
97 add_missing_impl_members_inner(
98 acc,
99 ctx,
100 AddMissingImplMembersMode::DefaultMethodsOnly,
101 "add_impl_default_members",
102 "Implement default members",
103 )
104}
105
106fn add_missing_impl_members_inner(
107 acc: &mut Assists,
108 ctx: &AssistContext,
109 mode: AddMissingImplMembersMode,
110 assist_id: &'static str,
111 label: &'static str,
112) -> Option<()> {
113 let _p = profile::span("add_missing_impl_members_inner");
114 let impl_def = ctx.find_node_at_offset::<ast::Impl>()?;
115 let impl_item_list = impl_def.assoc_item_list()?;
116
117 let trait_ = resolve_target_trait(&ctx.sema, &impl_def)?;
118
119 let def_name = |item: &ast::AssocItem| -> Option<SmolStr> {
120 match item {
121 ast::AssocItem::Fn(def) => def.name(),
122 ast::AssocItem::TypeAlias(def) => def.name(),
123 ast::AssocItem::Const(def) => def.name(),
124 ast::AssocItem::MacroCall(_) => None,
125 }
126 .map(|it| it.text().clone())
127 };
128
129 let missing_items = get_missing_assoc_items(&ctx.sema, &impl_def)
130 .iter()
131 .map(|i| match i {
132 hir::AssocItem::Function(i) => ast::AssocItem::Fn(i.source(ctx.db()).value),
133 hir::AssocItem::TypeAlias(i) => ast::AssocItem::TypeAlias(i.source(ctx.db()).value),
134 hir::AssocItem::Const(i) => ast::AssocItem::Const(i.source(ctx.db()).value),
135 })
136 .filter(|t| def_name(&t).is_some())
137 .filter(|t| match t {
138 ast::AssocItem::Fn(def) => match mode {
139 AddMissingImplMembersMode::DefaultMethodsOnly => def.body().is_some(),
140 AddMissingImplMembersMode::NoDefaultMethods => def.body().is_none(),
141 },
142 _ => mode == AddMissingImplMembersMode::NoDefaultMethods,
143 })
144 .collect::<Vec<_>>();
145
146 if missing_items.is_empty() {
147 return None;
148 }
149
150 let target = impl_def.syntax().text_range();
151 acc.add(AssistId(assist_id, AssistKind::QuickFix), label, target, |builder| {
152 let n_existing_items = impl_item_list.assoc_items().count();
153 let source_scope = ctx.sema.scope_for_def(trait_);
154 let target_scope = ctx.sema.scope(impl_item_list.syntax());
155 let ast_transform = QualifyPaths::new(&target_scope, &source_scope)
156 .or(SubstituteTypeParams::for_trait_impl(&source_scope, trait_, impl_def));
157 let items = missing_items
158 .into_iter()
159 .map(|it| ast_transform::apply(&*ast_transform, it))
160 .map(|it| match it {
161 ast::AssocItem::Fn(def) => ast::AssocItem::Fn(add_body(def)),
162 ast::AssocItem::TypeAlias(def) => ast::AssocItem::TypeAlias(def.remove_bounds()),
163 _ => it,
164 })
165 .map(|it| edit::remove_attrs_and_docs(&it));
166 let new_impl_item_list = impl_item_list.append_items(items);
167 let first_new_item = new_impl_item_list.assoc_items().nth(n_existing_items).unwrap();
168
169 let original_range = impl_item_list.syntax().text_range();
170 match ctx.config.snippet_cap {
171 None => builder.replace(original_range, new_impl_item_list.to_string()),
172 Some(cap) => {
173 let mut cursor = Cursor::Before(first_new_item.syntax());
174 let placeholder;
175 if let ast::AssocItem::Fn(func) = &first_new_item {
176 if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast) {
177 if m.syntax().text() == "todo!()" {
178 placeholder = m;
179 cursor = Cursor::Replace(placeholder.syntax());
180 }
181 }
182 }
183 builder.replace_snippet(
184 cap,
185 original_range,
186 render_snippet(cap, new_impl_item_list.syntax(), cursor),
187 )
188 }
189 };
190 })
191}
192
193fn add_body(fn_def: ast::Fn) -> ast::Fn {
194 if fn_def.body().is_some() {
195 return fn_def;
196 }
197 let body = make::block_expr(None, Some(make::expr_todo())).indent(IndentLevel(1));
198 fn_def.with_body(body)
199}
200
201#[cfg(test)]
202mod tests {
203 use crate::tests::{check_assist, check_assist_not_applicable};
204
205 use super::*;
206
207 #[test]
208 fn test_add_missing_impl_members() {
209 check_assist(
210 add_missing_impl_members,
211 r#"
212trait Foo {
213 type Output;
214
215 const CONST: usize = 42;
216
217 fn foo(&self);
218 fn bar(&self);
219 fn baz(&self);
220}
221
222struct S;
223
224impl Foo for S {
225 fn bar(&self) {}
226<|>
227}"#,
228 r#"
229trait Foo {
230 type Output;
231
232 const CONST: usize = 42;
233
234 fn foo(&self);
235 fn bar(&self);
236 fn baz(&self);
237}
238
239struct S;
240
241impl Foo for S {
242 fn bar(&self) {}
243 $0type Output;
244 const CONST: usize = 42;
245 fn foo(&self) {
246 todo!()
247 }
248 fn baz(&self) {
249 todo!()
250 }
251
252}"#,
253 );
254 }
255
256 #[test]
257 fn test_copied_overriden_members() {
258 check_assist(
259 add_missing_impl_members,
260 r#"
261trait Foo {
262 fn foo(&self);
263 fn bar(&self) -> bool { true }
264 fn baz(&self) -> u32 { 42 }
265}
266
267struct S;
268
269impl Foo for S {
270 fn bar(&self) {}
271<|>
272}"#,
273 r#"
274trait Foo {
275 fn foo(&self);
276 fn bar(&self) -> bool { true }
277 fn baz(&self) -> u32 { 42 }
278}
279
280struct S;
281
282impl Foo for S {
283 fn bar(&self) {}
284 fn foo(&self) {
285 ${0:todo!()}
286 }
287
288}"#,
289 );
290 }
291
292 #[test]
293 fn test_empty_impl_def() {
294 check_assist(
295 add_missing_impl_members,
296 r#"
297trait Foo { fn foo(&self); }
298struct S;
299impl Foo for S { <|> }"#,
300 r#"
301trait Foo { fn foo(&self); }
302struct S;
303impl Foo for S {
304 fn foo(&self) {
305 ${0:todo!()}
306 }
307}"#,
308 );
309 }
310
311 #[test]
312 fn fill_in_type_params_1() {
313 check_assist(
314 add_missing_impl_members,
315 r#"
316trait Foo<T> { fn foo(&self, t: T) -> &T; }
317struct S;
318impl Foo<u32> for S { <|> }"#,
319 r#"
320trait Foo<T> { fn foo(&self, t: T) -> &T; }
321struct S;
322impl Foo<u32> for S {
323 fn foo(&self, t: u32) -> &u32 {
324 ${0:todo!()}
325 }
326}"#,
327 );
328 }
329
330 #[test]
331 fn fill_in_type_params_2() {
332 check_assist(
333 add_missing_impl_members,
334 r#"
335trait Foo<T> { fn foo(&self, t: T) -> &T; }
336struct S;
337impl<U> Foo<U> for S { <|> }"#,
338 r#"
339trait Foo<T> { fn foo(&self, t: T) -> &T; }
340struct S;
341impl<U> Foo<U> for S {
342 fn foo(&self, t: U) -> &U {
343 ${0:todo!()}
344 }
345}"#,
346 );
347 }
348
349 #[test]
350 fn test_cursor_after_empty_impl_def() {
351 check_assist(
352 add_missing_impl_members,
353 r#"
354trait Foo { fn foo(&self); }
355struct S;
356impl Foo for S {}<|>"#,
357 r#"
358trait Foo { fn foo(&self); }
359struct S;
360impl Foo for S {
361 fn foo(&self) {
362 ${0:todo!()}
363 }
364}"#,
365 )
366 }
367
368 #[test]
369 fn test_qualify_path_1() {
370 check_assist(
371 add_missing_impl_members,
372 r#"
373mod foo {
374 pub struct Bar;
375 trait Foo { fn foo(&self, bar: Bar); }
376}
377struct S;
378impl foo::Foo for S { <|> }"#,
379 r#"
380mod foo {
381 pub struct Bar;
382 trait Foo { fn foo(&self, bar: Bar); }
383}
384struct S;
385impl foo::Foo for S {
386 fn foo(&self, bar: foo::Bar) {
387 ${0:todo!()}
388 }
389}"#,
390 );
391 }
392
393 #[test]
394 fn test_qualify_path_generic() {
395 check_assist(
396 add_missing_impl_members,
397 r#"
398mod foo {
399 pub struct Bar<T>;
400 trait Foo { fn foo(&self, bar: Bar<u32>); }
401}
402struct S;
403impl foo::Foo for S { <|> }"#,
404 r#"
405mod foo {
406 pub struct Bar<T>;
407 trait Foo { fn foo(&self, bar: Bar<u32>); }
408}
409struct S;
410impl foo::Foo for S {
411 fn foo(&self, bar: foo::Bar<u32>) {
412 ${0:todo!()}
413 }
414}"#,
415 );
416 }
417
418 #[test]
419 fn test_qualify_path_and_substitute_param() {
420 check_assist(
421 add_missing_impl_members,
422 r#"
423mod foo {
424 pub struct Bar<T>;
425 trait Foo<T> { fn foo(&self, bar: Bar<T>); }
426}
427struct S;
428impl foo::Foo<u32> for S { <|> }"#,
429 r#"
430mod foo {
431 pub struct Bar<T>;
432 trait Foo<T> { fn foo(&self, bar: Bar<T>); }
433}
434struct S;
435impl foo::Foo<u32> for S {
436 fn foo(&self, bar: foo::Bar<u32>) {
437 ${0:todo!()}
438 }
439}"#,
440 );
441 }
442
443 #[test]
444 fn test_substitute_param_no_qualify() {
445 // when substituting params, the substituted param should not be qualified!
446 check_assist(
447 add_missing_impl_members,
448 r#"
449mod foo {
450 trait Foo<T> { fn foo(&self, bar: T); }
451 pub struct Param;
452}
453struct Param;
454struct S;
455impl foo::Foo<Param> for S { <|> }"#,
456 r#"
457mod foo {
458 trait Foo<T> { fn foo(&self, bar: T); }
459 pub struct Param;
460}
461struct Param;
462struct S;
463impl foo::Foo<Param> for S {
464 fn foo(&self, bar: Param) {
465 ${0:todo!()}
466 }
467}"#,
468 );
469 }
470
471 #[test]
472 fn test_qualify_path_associated_item() {
473 check_assist(
474 add_missing_impl_members,
475 r#"
476mod foo {
477 pub struct Bar<T>;
478 impl Bar<T> { type Assoc = u32; }
479 trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
480}
481struct S;
482impl foo::Foo for S { <|> }"#,
483 r#"
484mod foo {
485 pub struct Bar<T>;
486 impl Bar<T> { type Assoc = u32; }
487 trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
488}
489struct S;
490impl foo::Foo for S {
491 fn foo(&self, bar: foo::Bar<u32>::Assoc) {
492 ${0:todo!()}
493 }
494}"#,
495 );
496 }
497
498 #[test]
499 fn test_qualify_path_nested() {
500 check_assist(
501 add_missing_impl_members,
502 r#"
503mod foo {
504 pub struct Bar<T>;
505 pub struct Baz;
506 trait Foo { fn foo(&self, bar: Bar<Baz>); }
507}
508struct S;
509impl foo::Foo for S { <|> }"#,
510 r#"
511mod foo {
512 pub struct Bar<T>;
513 pub struct Baz;
514 trait Foo { fn foo(&self, bar: Bar<Baz>); }
515}
516struct S;
517impl foo::Foo for S {
518 fn foo(&self, bar: foo::Bar<foo::Baz>) {
519 ${0:todo!()}
520 }
521}"#,
522 );
523 }
524
525 #[test]
526 fn test_qualify_path_fn_trait_notation() {
527 check_assist(
528 add_missing_impl_members,
529 r#"
530mod foo {
531 pub trait Fn<Args> { type Output; }
532 trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
533}
534struct S;
535impl foo::Foo for S { <|> }"#,
536 r#"
537mod foo {
538 pub trait Fn<Args> { type Output; }
539 trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
540}
541struct S;
542impl foo::Foo for S {
543 fn foo(&self, bar: dyn Fn(u32) -> i32) {
544 ${0:todo!()}
545 }
546}"#,
547 );
548 }
549
550 #[test]
551 fn test_empty_trait() {
552 check_assist_not_applicable(
553 add_missing_impl_members,
554 r#"
555trait Foo;
556struct S;
557impl Foo for S { <|> }"#,
558 )
559 }
560
561 #[test]
562 fn test_ignore_unnamed_trait_members_and_default_methods() {
563 check_assist_not_applicable(
564 add_missing_impl_members,
565 r#"
566trait Foo {
567 fn (arg: u32);
568 fn valid(some: u32) -> bool { false }
569}
570struct S;
571impl Foo for S { <|> }"#,
572 )
573 }
574
575 #[test]
576 fn test_with_docstring_and_attrs() {
577 check_assist(
578 add_missing_impl_members,
579 r#"
580#[doc(alias = "test alias")]
581trait Foo {
582 /// doc string
583 type Output;
584
585 #[must_use]
586 fn foo(&self);
587}
588struct S;
589impl Foo for S {}<|>"#,
590 r#"
591#[doc(alias = "test alias")]
592trait Foo {
593 /// doc string
594 type Output;
595
596 #[must_use]
597 fn foo(&self);
598}
599struct S;
600impl Foo for S {
601 $0type Output;
602 fn foo(&self) {
603 todo!()
604 }
605}"#,
606 )
607 }
608
609 #[test]
610 fn test_default_methods() {
611 check_assist(
612 add_missing_default_members,
613 r#"
614trait Foo {
615 type Output;
616
617 const CONST: usize = 42;
618
619 fn valid(some: u32) -> bool { false }
620 fn foo(some: u32) -> bool;
621}
622struct S;
623impl Foo for S { <|> }"#,
624 r#"
625trait Foo {
626 type Output;
627
628 const CONST: usize = 42;
629
630 fn valid(some: u32) -> bool { false }
631 fn foo(some: u32) -> bool;
632}
633struct S;
634impl Foo for S {
635 $0fn valid(some: u32) -> bool { false }
636}"#,
637 )
638 }
639
640 #[test]
641 fn test_generic_single_default_parameter() {
642 check_assist(
643 add_missing_impl_members,
644 r#"
645trait Foo<T = Self> {
646 fn bar(&self, other: &T);
647}
648
649struct S;
650impl Foo for S { <|> }"#,
651 r#"
652trait Foo<T = Self> {
653 fn bar(&self, other: &T);
654}
655
656struct S;
657impl Foo for S {
658 fn bar(&self, other: &Self) {
659 ${0:todo!()}
660 }
661}"#,
662 )
663 }
664
665 #[test]
666 fn test_generic_default_parameter_is_second() {
667 check_assist(
668 add_missing_impl_members,
669 r#"
670trait Foo<T1, T2 = Self> {
671 fn bar(&self, this: &T1, that: &T2);
672}
673
674struct S<T>;
675impl Foo<T> for S<T> { <|> }"#,
676 r#"
677trait Foo<T1, T2 = Self> {
678 fn bar(&self, this: &T1, that: &T2);
679}
680
681struct S<T>;
682impl Foo<T> for S<T> {
683 fn bar(&self, this: &T, that: &Self) {
684 ${0:todo!()}
685 }
686}"#,
687 )
688 }
689
690 #[test]
691 fn test_assoc_type_bounds_are_removed() {
692 check_assist(
693 add_missing_impl_members,
694 r#"
695trait Tr {
696 type Ty: Copy + 'static;
697}
698
699impl Tr for ()<|> {
700}"#,
701 r#"
702trait Tr {
703 type Ty: Copy + 'static;
704}
705
706impl Tr for () {
707 $0type Ty;
708}"#,
709 )
710 }
711}