2 use ide_db::{helpers::insert_whitespace_into_node::insert_ws_into, 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 missing_items = missing_items
123 if ctx.sema.hir_file_for(it.syntax()).is_macro() {
124 if let Some(it) = ast::AssocItem::cast(insert_ws_into(it.syntax().clone())) {
128 it.clone_for_update()
131 let (new_impl_def, first_new_item) = add_trait_assoc_items_to_impl(
138 match ctx.config.snippet_cap {
139 None => builder.replace(target, new_impl_def.to_string()),
141 let mut cursor = Cursor::Before(first_new_item.syntax());
143 if let ast::AssocItem::Fn(func) = &first_new_item {
144 if try_gen_trait_body(ctx, func, &trait_, &impl_def).is_none() {
145 if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast)
147 if m.syntax().text() == "todo!()" {
149 cursor = Cursor::Replace(placeholder.syntax());
154 builder.replace_snippet(
157 render_snippet(cap, new_impl_def.syntax(), cursor),
164 fn try_gen_trait_body(
168 impl_def: &ast::Impl,
170 let trait_path = make::ext::ident_path(&trait_.name(ctx.db()).to_string());
171 let hir_ty = ctx.sema.resolve_type(&impl_def.self_ty()?)?;
172 let adt = hir_ty.as_adt()?.source(ctx.db())?;
173 gen_trait_fn_body(func, &trait_path, &adt.value)
178 use crate::tests::{check_assist, check_assist_not_applicable};
183 fn test_add_missing_impl_members() {
185 add_missing_impl_members,
190 const CONST: usize = 42;
207 const CONST: usize = 42;
221 const CONST: usize = 42;
236 fn test_copied_overriden_members() {
238 add_missing_impl_members,
242 fn bar(&self) -> bool { true }
243 fn baz(&self) -> u32 { 42 }
255 fn bar(&self) -> bool { true }
256 fn baz(&self) -> u32 { 42 }
273 fn test_empty_impl_def() {
275 add_missing_impl_members,
277 trait Foo { fn foo(&self); }
279 impl Foo for S { $0 }"#,
281 trait Foo { fn foo(&self); }
292 fn test_impl_def_without_braces() {
294 add_missing_impl_members,
296 trait Foo { fn foo(&self); }
300 trait Foo { fn foo(&self); }
311 fn fill_in_type_params_1() {
313 add_missing_impl_members,
315 trait Foo<T> { fn foo(&self, t: T) -> &T; }
317 impl Foo<u32> for S { $0 }"#,
319 trait Foo<T> { fn foo(&self, t: T) -> &T; }
321 impl Foo<u32> for S {
322 fn foo(&self, t: u32) -> &u32 {
330 fn fill_in_type_params_2() {
332 add_missing_impl_members,
334 trait Foo<T> { fn foo(&self, t: T) -> &T; }
336 impl<U> Foo<U> for S { $0 }"#,
338 trait Foo<T> { fn foo(&self, t: T) -> &T; }
340 impl<U> Foo<U> for S {
341 fn foo(&self, t: U) -> &U {
349 fn test_cursor_after_empty_impl_def() {
351 add_missing_impl_members,
353 trait Foo { fn foo(&self); }
355 impl Foo for S {}$0"#,
357 trait Foo { fn foo(&self); }
368 fn test_qualify_path_1() {
370 add_missing_impl_members,
374 trait Foo { fn foo(&self, bar: Bar); }
377 impl foo::Foo for S { $0 }"#,
381 trait Foo { fn foo(&self, bar: Bar); }
384 impl foo::Foo for S {
385 fn foo(&self, bar: foo::Bar) {
393 fn test_qualify_path_2() {
395 add_missing_impl_members,
400 pub trait Foo { fn foo(&self, bar: Bar); }
407 impl bar::Foo for S { $0 }"#,
412 pub trait Foo { fn foo(&self, bar: Bar); }
419 impl bar::Foo for S {
420 fn foo(&self, bar: bar::Bar) {
428 fn test_qualify_path_generic() {
430 add_missing_impl_members,
434 trait Foo { fn foo(&self, bar: Bar<u32>); }
437 impl foo::Foo for S { $0 }"#,
441 trait Foo { fn foo(&self, bar: Bar<u32>); }
444 impl foo::Foo for S {
445 fn foo(&self, bar: foo::Bar<u32>) {
453 fn test_qualify_path_and_substitute_param() {
455 add_missing_impl_members,
459 trait Foo<T> { fn foo(&self, bar: Bar<T>); }
462 impl foo::Foo<u32> for S { $0 }"#,
466 trait Foo<T> { fn foo(&self, bar: Bar<T>); }
469 impl foo::Foo<u32> for S {
470 fn foo(&self, bar: foo::Bar<u32>) {
478 fn test_substitute_param_no_qualify() {
479 // when substituting params, the substituted param should not be qualified!
481 add_missing_impl_members,
484 trait Foo<T> { fn foo(&self, bar: T); }
489 impl foo::Foo<Param> for S { $0 }"#,
492 trait Foo<T> { fn foo(&self, bar: T); }
497 impl foo::Foo<Param> for S {
498 fn foo(&self, bar: Param) {
506 fn test_qualify_path_associated_item() {
508 add_missing_impl_members,
512 impl Bar<T> { type Assoc = u32; }
513 trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
516 impl foo::Foo for S { $0 }"#,
520 impl Bar<T> { type Assoc = u32; }
521 trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
524 impl foo::Foo for S {
525 fn foo(&self, bar: foo::Bar<u32>::Assoc) {
533 fn test_qualify_path_nested() {
535 add_missing_impl_members,
540 trait Foo { fn foo(&self, bar: Bar<Baz>); }
543 impl foo::Foo for S { $0 }"#,
548 trait Foo { fn foo(&self, bar: Bar<Baz>); }
551 impl foo::Foo for S {
552 fn foo(&self, bar: foo::Bar<foo::Baz>) {
560 fn test_qualify_path_fn_trait_notation() {
562 add_missing_impl_members,
565 pub trait Fn<Args> { type Output; }
566 trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
569 impl foo::Foo for S { $0 }"#,
572 pub trait Fn<Args> { type Output; }
573 trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
576 impl foo::Foo for S {
577 fn foo(&self, bar: dyn Fn(u32) -> i32) {
585 fn test_empty_trait() {
586 check_assist_not_applicable(
587 add_missing_impl_members,
591 impl Foo for S { $0 }"#,
596 fn test_ignore_unnamed_trait_members_and_default_methods() {
597 check_assist_not_applicable(
598 add_missing_impl_members,
602 fn valid(some: u32) -> bool { false }
605 impl Foo for S { $0 }"#,
610 fn test_with_docstring_and_attrs() {
612 add_missing_impl_members,
614 #[doc(alias = "test alias")]
623 impl Foo for S {}$0"#,
625 #[doc(alias = "test alias")]
645 fn test_default_methods() {
647 add_missing_default_members,
652 const CONST: usize = 42;
654 fn valid(some: u32) -> bool { false }
655 fn foo(some: u32) -> bool;
658 impl Foo for S { $0 }"#,
663 const CONST: usize = 42;
665 fn valid(some: u32) -> bool { false }
666 fn foo(some: u32) -> bool;
670 $0fn valid(some: u32) -> bool { false }
676 fn test_generic_single_default_parameter() {
678 add_missing_impl_members,
680 trait Foo<T = Self> {
681 fn bar(&self, other: &T);
685 impl Foo for S { $0 }"#,
687 trait Foo<T = Self> {
688 fn bar(&self, other: &T);
693 fn bar(&self, other: &Self) {
701 fn test_generic_default_parameter_is_second() {
703 add_missing_impl_members,
705 trait Foo<T1, T2 = Self> {
706 fn bar(&self, this: &T1, that: &T2);
710 impl Foo<T> for S<T> { $0 }"#,
712 trait Foo<T1, T2 = Self> {
713 fn bar(&self, this: &T1, that: &T2);
717 impl Foo<T> for S<T> {
718 fn bar(&self, this: &T, that: &Self) {
726 fn test_assoc_type_bounds_are_removed() {
728 add_missing_impl_members,
731 type Ty: Copy + 'static;
738 type Ty: Copy + 'static;
748 fn test_whitespace_fixup_preserves_bad_tokens() {
750 add_missing_impl_members,
774 fn test_whitespace_fixup_preserves_comments() {
776 add_missing_impl_members,
802 add_missing_impl_members,
805 fn foo(&self, x: crate)
813 fn foo(&self, x: crate)
816 fn foo(&self, x: crate) {
825 fn missing_generic_type() {
827 add_missing_impl_members,
830 fn foo(&self, bar: BAR);
838 fn foo(&self, bar: BAR);
841 fn foo(&self, bar: BAR) {
850 fn does_not_requalify_self_as_crate() {
852 add_missing_default_members,
854 struct Wrapper<T>(T);
857 fn f(self) -> Wrapper<Self> {
867 struct Wrapper<T>(T);
870 fn f(self) -> Wrapper<Self> {
876 $0fn f(self) -> Wrapper<Self> {
885 fn test_default_body_generation() {
887 add_missing_impl_members,
889 //- minicore: default
892 impl Default for Foo {
899 impl Default for Foo {
900 $0fn default() -> Self {
901 Self(Default::default())
909 fn test_from_macro() {
911 add_missing_default_members,
916 fn foo<'lt>(&'lt self) {}
931 fn foo<'lt>(&'lt self) {}
939 $0fn foo< 'lt>(& 'lt self){}
947 fn test_assoc_type_when_trait_with_same_name_in_scope() {
949 add_missing_impl_members,
957 pub trait Behavior<T: Types> {
958 fn reproduce(&self, foo: T::Foo);
963 impl<T: Types> Behavior<T> for Impl { $0 }"#,
971 pub trait Behavior<T: Types> {
972 fn reproduce(&self, foo: T::Foo);
977 impl<T: Types> Behavior<T> for Impl {
978 fn reproduce(&self, foo: <T as Types>::Foo) {
986 fn test_assoc_type_on_concrete_type() {
988 add_missing_impl_members,
998 pub trait Behavior<T: Types> {
999 fn reproduce(&self, foo: T::Foo);
1004 impl Behavior<u32> for Impl { $0 }"#,
1010 impl Types for u32 {
1014 pub trait Behavior<T: Types> {
1015 fn reproduce(&self, foo: T::Foo);
1020 impl Behavior<u32> for Impl {
1021 fn reproduce(&self, foo: <u32 as Types>::Foo) {
1029 fn test_assoc_type_on_concrete_type_qualified() {
1031 add_missing_impl_members,
1037 impl Types for std::string::String {
1041 pub trait Behavior<T: Types> {
1042 fn reproduce(&self, foo: T::Foo);
1047 impl Behavior<std::string::String> for Impl { $0 }"#,
1053 impl Types for std::string::String {
1057 pub trait Behavior<T: Types> {
1058 fn reproduce(&self, foo: T::Foo);
1063 impl Behavior<std::string::String> for Impl {
1064 fn reproduce(&self, foo: <std::string::String as Types>::Foo) {
1072 fn test_assoc_type_on_concrete_type_multi_option_ambiguous() {
1074 add_missing_impl_members,
1084 impl Types for u32 {
1088 impl Types2 for u32 {
1092 pub trait Behavior<T: Types + Types2> {
1093 fn reproduce(&self, foo: <T as Types2>::Foo);
1098 impl Behavior<u32> for Impl { $0 }"#,
1108 impl Types for u32 {
1112 impl Types2 for u32 {
1116 pub trait Behavior<T: Types + Types2> {
1117 fn reproduce(&self, foo: <T as Types2>::Foo);
1122 impl Behavior<u32> for Impl {
1123 fn reproduce(&self, foo: <u32 as Types2>::Foo) {
1131 fn test_assoc_type_on_concrete_type_multi_option() {
1133 add_missing_impl_members,
1143 impl Types for u32 {
1147 impl Types2 for u32 {
1151 pub trait Behavior<T: Types + Types2> {
1152 fn reproduce(&self, foo: T::Bar);
1157 impl Behavior<u32> for Impl { $0 }"#,
1167 impl Types for u32 {
1171 impl Types2 for u32 {
1175 pub trait Behavior<T: Types + Types2> {
1176 fn reproduce(&self, foo: T::Bar);
1181 impl Behavior<u32> for Impl {
1182 fn reproduce(&self, foo: <u32 as Types2>::Bar) {
1190 fn test_assoc_type_on_concrete_type_multi_option_foreign() {
1192 add_missing_impl_members,
1204 impl Types for u32 {
1208 impl bar::Types2 for u32 {
1212 pub trait Behavior<T: Types + bar::Types2> {
1213 fn reproduce(&self, foo: T::Bar);
1218 impl Behavior<u32> for Impl { $0 }"#,
1230 impl Types for u32 {
1234 impl bar::Types2 for u32 {
1238 pub trait Behavior<T: Types + bar::Types2> {
1239 fn reproduce(&self, foo: T::Bar);
1244 impl Behavior<u32> for Impl {
1245 fn reproduce(&self, foo: <u32 as bar::Types2>::Bar) {
1253 fn test_transform_path_in_path_expr() {
1255 add_missing_default_members,
1261 pub trait Trait<T: Const> {
1270 impl Const for u32 {
1276 impl Trait<u32> for Impl { $0 }"#,
1282 pub trait Trait<T: Const> {
1291 impl Const for u32 {
1297 impl Trait<u32> for Impl {
1298 $0fn foo() -> bool {
1299 match <u32 as Const>::FOO {