2 use ide_db::traits::resolve_target_trait;
3 use syntax::ast::{self, make, AstNode};
6 assist_context::{AssistContext, Assists},
8 add_trait_assoc_items_to_impl, filter_assoc_items, gen_trait_fn_body, render_snippet,
9 Cursor, DefaultMethods,
14 // Assist: add_impl_missing_members
16 // Adds scaffold for required impl members.
21 // fn foo(&self) -> T;
25 // impl Trait<u32> for () {$0
33 // fn foo(&self) -> T;
37 // impl Trait<u32> for () {
40 // fn foo(&self) -> u32 {
45 pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
46 add_missing_impl_members_inner(
50 "add_impl_missing_members",
51 "Implement missing members",
55 // Assist: add_impl_default_members
57 // Adds scaffold for overriding default impl members.
66 // impl Trait for () {
79 // impl Trait for () {
86 pub(crate) fn add_missing_default_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
87 add_missing_impl_members_inner(
91 "add_impl_default_members",
92 "Implement default members",
96 fn add_missing_impl_members_inner(
100 assist_id: &'static str,
103 let _p = profile::span("add_missing_impl_members_inner");
104 let impl_def = ctx.find_node_at_offset::<ast::Impl>()?;
105 let trait_ = resolve_target_trait(&ctx.sema, &impl_def)?;
107 let missing_items = filter_assoc_items(
109 &ide_db::traits::get_missing_assoc_items(&ctx.sema, &impl_def),
113 if missing_items.is_empty() {
117 let target = impl_def.syntax().text_range();
118 acc.add(AssistId(assist_id, AssistKind::QuickFix), label, target, |builder| {
119 let target_scope = ctx.sema.scope(impl_def.syntax());
120 let (new_impl_def, first_new_item) = add_trait_assoc_items_to_impl(
127 // if target_scope.in_macro_file() {
130 match ctx.config.snippet_cap {
131 None => builder.replace(target, new_impl_def.to_string()),
133 let mut cursor = Cursor::Before(first_new_item.syntax());
135 if let ast::AssocItem::Fn(func) = &first_new_item {
136 if try_gen_trait_body(ctx, func, &trait_, &impl_def).is_none() {
137 if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast)
139 if m.syntax().text() == "todo!()" {
141 cursor = Cursor::Replace(placeholder.syntax());
146 builder.replace_snippet(
149 render_snippet(cap, new_impl_def.syntax(), cursor),
156 fn try_gen_trait_body(
160 impl_def: &ast::Impl,
162 let trait_path = make::ext::ident_path(&trait_.name(ctx.db()).to_string());
163 let hir_ty = ctx.sema.resolve_type(&impl_def.self_ty()?)?;
164 let adt = hir_ty.as_adt()?.source(ctx.db())?;
165 gen_trait_fn_body(func, &trait_path, &adt.value)
170 use crate::tests::{check_assist, check_assist_not_applicable};
175 fn test_add_missing_impl_members() {
177 add_missing_impl_members,
182 const CONST: usize = 42;
199 const CONST: usize = 42;
213 const CONST: usize = 42;
228 fn test_copied_overriden_members() {
230 add_missing_impl_members,
234 fn bar(&self) -> bool { true }
235 fn baz(&self) -> u32 { 42 }
247 fn bar(&self) -> bool { true }
248 fn baz(&self) -> u32 { 42 }
265 fn test_empty_impl_def() {
267 add_missing_impl_members,
269 trait Foo { fn foo(&self); }
271 impl Foo for S { $0 }"#,
273 trait Foo { fn foo(&self); }
284 fn test_impl_def_without_braces() {
286 add_missing_impl_members,
288 trait Foo { fn foo(&self); }
292 trait Foo { fn foo(&self); }
303 fn fill_in_type_params_1() {
305 add_missing_impl_members,
307 trait Foo<T> { fn foo(&self, t: T) -> &T; }
309 impl Foo<u32> for S { $0 }"#,
311 trait Foo<T> { fn foo(&self, t: T) -> &T; }
313 impl Foo<u32> for S {
314 fn foo(&self, t: u32) -> &u32 {
322 fn fill_in_type_params_2() {
324 add_missing_impl_members,
326 trait Foo<T> { fn foo(&self, t: T) -> &T; }
328 impl<U> Foo<U> for S { $0 }"#,
330 trait Foo<T> { fn foo(&self, t: T) -> &T; }
332 impl<U> Foo<U> for S {
333 fn foo(&self, t: U) -> &U {
341 fn test_cursor_after_empty_impl_def() {
343 add_missing_impl_members,
345 trait Foo { fn foo(&self); }
347 impl Foo for S {}$0"#,
349 trait Foo { fn foo(&self); }
360 fn test_qualify_path_1() {
362 add_missing_impl_members,
366 trait Foo { fn foo(&self, bar: Bar); }
369 impl foo::Foo for S { $0 }"#,
373 trait Foo { fn foo(&self, bar: Bar); }
376 impl foo::Foo for S {
377 fn foo(&self, bar: foo::Bar) {
385 fn test_qualify_path_2() {
387 add_missing_impl_members,
392 pub trait Foo { fn foo(&self, bar: Bar); }
399 impl bar::Foo for S { $0 }"#,
404 pub trait Foo { fn foo(&self, bar: Bar); }
411 impl bar::Foo for S {
412 fn foo(&self, bar: bar::Bar) {
420 fn test_qualify_path_generic() {
422 add_missing_impl_members,
426 trait Foo { fn foo(&self, bar: Bar<u32>); }
429 impl foo::Foo for S { $0 }"#,
433 trait Foo { fn foo(&self, bar: Bar<u32>); }
436 impl foo::Foo for S {
437 fn foo(&self, bar: foo::Bar<u32>) {
445 fn test_qualify_path_and_substitute_param() {
447 add_missing_impl_members,
451 trait Foo<T> { fn foo(&self, bar: Bar<T>); }
454 impl foo::Foo<u32> for S { $0 }"#,
458 trait Foo<T> { fn foo(&self, bar: Bar<T>); }
461 impl foo::Foo<u32> for S {
462 fn foo(&self, bar: foo::Bar<u32>) {
470 fn test_substitute_param_no_qualify() {
471 // when substituting params, the substituted param should not be qualified!
473 add_missing_impl_members,
476 trait Foo<T> { fn foo(&self, bar: T); }
481 impl foo::Foo<Param> for S { $0 }"#,
484 trait Foo<T> { fn foo(&self, bar: T); }
489 impl foo::Foo<Param> for S {
490 fn foo(&self, bar: Param) {
498 fn test_qualify_path_associated_item() {
500 add_missing_impl_members,
504 impl Bar<T> { type Assoc = u32; }
505 trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
508 impl foo::Foo for S { $0 }"#,
512 impl Bar<T> { type Assoc = u32; }
513 trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
516 impl foo::Foo for S {
517 fn foo(&self, bar: foo::Bar<u32>::Assoc) {
525 fn test_qualify_path_nested() {
527 add_missing_impl_members,
532 trait Foo { fn foo(&self, bar: Bar<Baz>); }
535 impl foo::Foo for S { $0 }"#,
540 trait Foo { fn foo(&self, bar: Bar<Baz>); }
543 impl foo::Foo for S {
544 fn foo(&self, bar: foo::Bar<foo::Baz>) {
552 fn test_qualify_path_fn_trait_notation() {
554 add_missing_impl_members,
557 pub trait Fn<Args> { type Output; }
558 trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
561 impl foo::Foo for S { $0 }"#,
564 pub trait Fn<Args> { type Output; }
565 trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
568 impl foo::Foo for S {
569 fn foo(&self, bar: dyn Fn(u32) -> i32) {
577 fn test_empty_trait() {
578 check_assist_not_applicable(
579 add_missing_impl_members,
583 impl Foo for S { $0 }"#,
588 fn test_ignore_unnamed_trait_members_and_default_methods() {
589 check_assist_not_applicable(
590 add_missing_impl_members,
594 fn valid(some: u32) -> bool { false }
597 impl Foo for S { $0 }"#,
602 fn test_with_docstring_and_attrs() {
604 add_missing_impl_members,
606 #[doc(alias = "test alias")]
615 impl Foo for S {}$0"#,
617 #[doc(alias = "test alias")]
637 fn test_default_methods() {
639 add_missing_default_members,
644 const CONST: usize = 42;
646 fn valid(some: u32) -> bool { false }
647 fn foo(some: u32) -> bool;
650 impl Foo for S { $0 }"#,
655 const CONST: usize = 42;
657 fn valid(some: u32) -> bool { false }
658 fn foo(some: u32) -> bool;
662 $0fn valid(some: u32) -> bool { false }
668 fn test_generic_single_default_parameter() {
670 add_missing_impl_members,
672 trait Foo<T = Self> {
673 fn bar(&self, other: &T);
677 impl Foo for S { $0 }"#,
679 trait Foo<T = Self> {
680 fn bar(&self, other: &T);
685 fn bar(&self, other: &Self) {
693 fn test_generic_default_parameter_is_second() {
695 add_missing_impl_members,
697 trait Foo<T1, T2 = Self> {
698 fn bar(&self, this: &T1, that: &T2);
702 impl Foo<T> for S<T> { $0 }"#,
704 trait Foo<T1, T2 = Self> {
705 fn bar(&self, this: &T1, that: &T2);
709 impl Foo<T> for S<T> {
710 fn bar(&self, this: &T, that: &Self) {
718 fn test_assoc_type_bounds_are_removed() {
720 add_missing_impl_members,
723 type Ty: Copy + 'static;
730 type Ty: Copy + 'static;
740 fn test_whitespace_fixup_preserves_bad_tokens() {
742 add_missing_impl_members,
766 fn test_whitespace_fixup_preserves_comments() {
768 add_missing_impl_members,
794 add_missing_impl_members,
797 fn foo(&self, x: crate)
805 fn foo(&self, x: crate)
808 fn foo(&self, x: crate) {
817 fn missing_generic_type() {
819 add_missing_impl_members,
822 fn foo(&self, bar: BAR);
830 fn foo(&self, bar: BAR);
833 fn foo(&self, bar: BAR) {
842 fn does_not_requalify_self_as_crate() {
844 add_missing_default_members,
846 struct Wrapper<T>(T);
849 fn f(self) -> Wrapper<Self> {
859 struct Wrapper<T>(T);
862 fn f(self) -> Wrapper<Self> {
868 $0fn f(self) -> Wrapper<Self> {
877 fn test_default_body_generation() {
879 add_missing_impl_members,
881 //- minicore: default
884 impl Default for Foo {
891 impl Default for Foo {
892 $0fn default() -> Self {
893 Self(Default::default())