3 syntax_helpers::insert_whitespace_into_node::insert_ws_into, traits::resolve_target_trait,
5 use syntax::ast::{self, make, AstNode};
8 assist_context::{AssistContext, Assists},
10 add_trait_assoc_items_to_impl, filter_assoc_items, gen_trait_fn_body, render_snippet,
11 Cursor, DefaultMethods,
16 // Assist: add_impl_missing_members
18 // Adds scaffold for required impl members.
23 // fn foo(&self) -> T;
27 // impl Trait<u32> for () {$0
35 // fn foo(&self) -> T;
39 // impl Trait<u32> for () {
42 // fn foo(&self) -> u32 {
47 pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
48 add_missing_impl_members_inner(
52 "add_impl_missing_members",
53 "Implement missing members",
57 // Assist: add_impl_default_members
59 // Adds scaffold for overriding default impl members.
68 // impl Trait for () {
81 // impl Trait for () {
88 pub(crate) fn add_missing_default_members(
90 ctx: &AssistContext<'_>,
92 add_missing_impl_members_inner(
96 "add_impl_default_members",
97 "Implement default members",
101 fn add_missing_impl_members_inner(
103 ctx: &AssistContext<'_>,
104 mode: DefaultMethods,
105 assist_id: &'static str,
108 let _p = profile::span("add_missing_impl_members_inner");
109 let impl_def = ctx.find_node_at_offset::<ast::Impl>()?;
110 let target_scope = ctx.sema.scope(impl_def.syntax())?;
111 let trait_ = resolve_target_trait(&ctx.sema, &impl_def)?;
113 let missing_items = filter_assoc_items(
115 &ide_db::traits::get_missing_assoc_items(&ctx.sema, &impl_def),
119 if missing_items.is_empty() {
123 let target = impl_def.syntax().text_range();
124 acc.add(AssistId(assist_id, AssistKind::QuickFix), label, target, |builder| {
125 let missing_items = missing_items
128 if ctx.sema.hir_file_for(it.syntax()).is_macro() {
129 if let Some(it) = ast::AssocItem::cast(insert_ws_into(it.syntax().clone())) {
133 it.clone_for_update()
136 let (new_impl_def, first_new_item) = add_trait_assoc_items_to_impl(
143 match ctx.config.snippet_cap {
144 None => builder.replace(target, new_impl_def.to_string()),
146 let mut cursor = Cursor::Before(first_new_item.syntax());
148 if let ast::AssocItem::Fn(func) = &first_new_item {
149 if try_gen_trait_body(ctx, func, &trait_, &impl_def).is_none() {
150 if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast)
152 if m.syntax().text() == "todo!()" {
154 cursor = Cursor::Replace(placeholder.syntax());
159 builder.replace_snippet(
162 render_snippet(cap, new_impl_def.syntax(), cursor),
169 fn try_gen_trait_body(
170 ctx: &AssistContext<'_>,
173 impl_def: &ast::Impl,
175 let trait_path = make::ext::ident_path(&trait_.name(ctx.db()).to_string());
176 let hir_ty = ctx.sema.resolve_type(&impl_def.self_ty()?)?;
177 let adt = hir_ty.as_adt()?.source(ctx.db())?;
178 gen_trait_fn_body(func, &trait_path, &adt.value)
183 use crate::tests::{check_assist, check_assist_not_applicable};
188 fn test_add_missing_impl_members() {
190 add_missing_impl_members,
195 const CONST: usize = 42;
212 const CONST: usize = 42;
226 const CONST: usize = 42;
241 fn test_copied_overriden_members() {
243 add_missing_impl_members,
247 fn bar(&self) -> bool { true }
248 fn baz(&self) -> u32 { 42 }
260 fn bar(&self) -> bool { true }
261 fn baz(&self) -> u32 { 42 }
278 fn test_empty_impl_def() {
280 add_missing_impl_members,
282 trait Foo { fn foo(&self); }
284 impl Foo for S { $0 }"#,
286 trait Foo { fn foo(&self); }
297 fn test_impl_def_without_braces() {
299 add_missing_impl_members,
301 trait Foo { fn foo(&self); }
305 trait Foo { fn foo(&self); }
316 fn fill_in_type_params_1() {
318 add_missing_impl_members,
320 trait Foo<T> { fn foo(&self, t: T) -> &T; }
322 impl Foo<u32> for S { $0 }"#,
324 trait Foo<T> { fn foo(&self, t: T) -> &T; }
326 impl Foo<u32> for S {
327 fn foo(&self, t: u32) -> &u32 {
335 fn fill_in_type_params_2() {
337 add_missing_impl_members,
339 trait Foo<T> { fn foo(&self, t: T) -> &T; }
341 impl<U> Foo<U> for S { $0 }"#,
343 trait Foo<T> { fn foo(&self, t: T) -> &T; }
345 impl<U> Foo<U> for S {
346 fn foo(&self, t: U) -> &U {
354 fn test_cursor_after_empty_impl_def() {
356 add_missing_impl_members,
358 trait Foo { fn foo(&self); }
360 impl Foo for S {}$0"#,
362 trait Foo { fn foo(&self); }
373 fn test_qualify_path_1() {
375 add_missing_impl_members,
379 trait Foo { fn foo(&self, bar: Bar); }
382 impl foo::Foo for S { $0 }"#,
386 trait Foo { fn foo(&self, bar: Bar); }
389 impl foo::Foo for S {
390 fn foo(&self, bar: foo::Bar) {
398 fn test_qualify_path_2() {
400 add_missing_impl_members,
405 pub trait Foo { fn foo(&self, bar: Bar); }
412 impl bar::Foo for S { $0 }"#,
417 pub trait Foo { fn foo(&self, bar: Bar); }
424 impl bar::Foo for S {
425 fn foo(&self, bar: bar::Bar) {
433 fn test_qualify_path_generic() {
435 add_missing_impl_members,
439 trait Foo { fn foo(&self, bar: Bar<u32>); }
442 impl foo::Foo for S { $0 }"#,
446 trait Foo { fn foo(&self, bar: Bar<u32>); }
449 impl foo::Foo for S {
450 fn foo(&self, bar: foo::Bar<u32>) {
458 fn test_qualify_path_and_substitute_param() {
460 add_missing_impl_members,
464 trait Foo<T> { fn foo(&self, bar: Bar<T>); }
467 impl foo::Foo<u32> for S { $0 }"#,
471 trait Foo<T> { fn foo(&self, bar: Bar<T>); }
474 impl foo::Foo<u32> for S {
475 fn foo(&self, bar: foo::Bar<u32>) {
483 fn test_substitute_param_no_qualify() {
484 // when substituting params, the substituted param should not be qualified!
486 add_missing_impl_members,
489 trait Foo<T> { fn foo(&self, bar: T); }
494 impl foo::Foo<Param> for S { $0 }"#,
497 trait Foo<T> { fn foo(&self, bar: T); }
502 impl foo::Foo<Param> for S {
503 fn foo(&self, bar: Param) {
511 fn test_qualify_path_associated_item() {
513 add_missing_impl_members,
517 impl Bar<T> { type Assoc = u32; }
518 trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
521 impl foo::Foo for S { $0 }"#,
525 impl Bar<T> { type Assoc = u32; }
526 trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
529 impl foo::Foo for S {
530 fn foo(&self, bar: foo::Bar<u32>::Assoc) {
538 fn test_qualify_path_nested() {
540 add_missing_impl_members,
545 trait Foo { fn foo(&self, bar: Bar<Baz>); }
548 impl foo::Foo for S { $0 }"#,
553 trait Foo { fn foo(&self, bar: Bar<Baz>); }
556 impl foo::Foo for S {
557 fn foo(&self, bar: foo::Bar<foo::Baz>) {
565 fn test_qualify_path_fn_trait_notation() {
567 add_missing_impl_members,
570 pub trait Fn<Args> { type Output; }
571 trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
574 impl foo::Foo for S { $0 }"#,
577 pub trait Fn<Args> { type Output; }
578 trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
581 impl foo::Foo for S {
582 fn foo(&self, bar: dyn Fn(u32) -> i32) {
590 fn test_empty_trait() {
591 check_assist_not_applicable(
592 add_missing_impl_members,
596 impl Foo for S { $0 }"#,
601 fn test_ignore_unnamed_trait_members_and_default_methods() {
602 check_assist_not_applicable(
603 add_missing_impl_members,
607 fn valid(some: u32) -> bool { false }
610 impl Foo for S { $0 }"#,
615 fn test_with_docstring_and_attrs() {
617 add_missing_impl_members,
619 #[doc(alias = "test alias")]
628 impl Foo for S {}$0"#,
630 #[doc(alias = "test alias")]
650 fn test_default_methods() {
652 add_missing_default_members,
657 const CONST: usize = 42;
659 fn valid(some: u32) -> bool { false }
660 fn foo(some: u32) -> bool;
663 impl Foo for S { $0 }"#,
668 const CONST: usize = 42;
670 fn valid(some: u32) -> bool { false }
671 fn foo(some: u32) -> bool;
675 $0fn valid(some: u32) -> bool { false }
681 fn test_generic_single_default_parameter() {
683 add_missing_impl_members,
685 trait Foo<T = Self> {
686 fn bar(&self, other: &T);
690 impl Foo for S { $0 }"#,
692 trait Foo<T = Self> {
693 fn bar(&self, other: &T);
698 fn bar(&self, other: &Self) {
706 fn test_generic_default_parameter_is_second() {
708 add_missing_impl_members,
710 trait Foo<T1, T2 = Self> {
711 fn bar(&self, this: &T1, that: &T2);
715 impl Foo<T> for S<T> { $0 }"#,
717 trait Foo<T1, T2 = Self> {
718 fn bar(&self, this: &T1, that: &T2);
722 impl Foo<T> for S<T> {
723 fn bar(&self, this: &T, that: &Self) {
731 fn test_assoc_type_bounds_are_removed() {
733 add_missing_impl_members,
736 type Ty: Copy + 'static;
743 type Ty: Copy + 'static;
753 fn test_whitespace_fixup_preserves_bad_tokens() {
755 add_missing_impl_members,
779 fn test_whitespace_fixup_preserves_comments() {
781 add_missing_impl_members,
807 add_missing_impl_members,
810 fn foo(&self, x: crate)
818 fn foo(&self, x: crate)
821 fn foo(&self, x: crate) {
830 fn missing_generic_type() {
832 add_missing_impl_members,
835 fn foo(&self, bar: BAR);
843 fn foo(&self, bar: BAR);
846 fn foo(&self, bar: BAR) {
855 fn does_not_requalify_self_as_crate() {
857 add_missing_default_members,
859 struct Wrapper<T>(T);
862 fn f(self) -> Wrapper<Self> {
872 struct Wrapper<T>(T);
875 fn f(self) -> Wrapper<Self> {
881 $0fn f(self) -> Wrapper<Self> {
890 fn test_default_body_generation() {
892 add_missing_impl_members,
894 //- minicore: default
897 impl Default for Foo {
904 impl Default for Foo {
905 $0fn default() -> Self {
906 Self(Default::default())
914 fn test_from_macro() {
916 add_missing_default_members,
921 fn foo<'lt>(&'lt self) {}
936 fn foo<'lt>(&'lt self) {}
944 $0fn foo< 'lt>(& 'lt self){}
951 fn test_assoc_type_when_trait_with_same_name_in_scope() {
953 add_missing_impl_members,
961 pub trait Behavior<T: Types> {
962 fn reproduce(&self, foo: T::Foo);
967 impl<T: Types> Behavior<T> for Impl { $0 }"#,
975 pub trait Behavior<T: Types> {
976 fn reproduce(&self, foo: T::Foo);
981 impl<T: Types> Behavior<T> for Impl {
982 fn reproduce(&self, foo: <T as Types>::Foo) {
990 fn test_assoc_type_on_concrete_type() {
992 add_missing_impl_members,
1002 pub trait Behavior<T: Types> {
1003 fn reproduce(&self, foo: T::Foo);
1008 impl Behavior<u32> for Impl { $0 }"#,
1014 impl Types for u32 {
1018 pub trait Behavior<T: Types> {
1019 fn reproduce(&self, foo: T::Foo);
1024 impl Behavior<u32> for Impl {
1025 fn reproduce(&self, foo: <u32 as Types>::Foo) {
1033 fn test_assoc_type_on_concrete_type_qualified() {
1035 add_missing_impl_members,
1041 impl Types for std::string::String {
1045 pub trait Behavior<T: Types> {
1046 fn reproduce(&self, foo: T::Foo);
1051 impl Behavior<std::string::String> for Impl { $0 }"#,
1057 impl Types for std::string::String {
1061 pub trait Behavior<T: Types> {
1062 fn reproduce(&self, foo: T::Foo);
1067 impl Behavior<std::string::String> for Impl {
1068 fn reproduce(&self, foo: <std::string::String as Types>::Foo) {
1076 fn test_assoc_type_on_concrete_type_multi_option_ambiguous() {
1078 add_missing_impl_members,
1088 impl Types for u32 {
1092 impl Types2 for u32 {
1096 pub trait Behavior<T: Types + Types2> {
1097 fn reproduce(&self, foo: <T as Types2>::Foo);
1102 impl Behavior<u32> for Impl { $0 }"#,
1112 impl Types for u32 {
1116 impl Types2 for u32 {
1120 pub trait Behavior<T: Types + Types2> {
1121 fn reproduce(&self, foo: <T as Types2>::Foo);
1126 impl Behavior<u32> for Impl {
1127 fn reproduce(&self, foo: <u32 as Types2>::Foo) {
1135 fn test_assoc_type_on_concrete_type_multi_option() {
1137 add_missing_impl_members,
1147 impl Types for u32 {
1151 impl Types2 for u32 {
1155 pub trait Behavior<T: Types + Types2> {
1156 fn reproduce(&self, foo: T::Bar);
1161 impl Behavior<u32> for Impl { $0 }"#,
1171 impl Types for u32 {
1175 impl Types2 for u32 {
1179 pub trait Behavior<T: Types + Types2> {
1180 fn reproduce(&self, foo: T::Bar);
1185 impl Behavior<u32> for Impl {
1186 fn reproduce(&self, foo: <u32 as Types2>::Bar) {
1194 fn test_assoc_type_on_concrete_type_multi_option_foreign() {
1196 add_missing_impl_members,
1208 impl Types for u32 {
1212 impl bar::Types2 for u32 {
1216 pub trait Behavior<T: Types + bar::Types2> {
1217 fn reproduce(&self, foo: T::Bar);
1222 impl Behavior<u32> for Impl { $0 }"#,
1234 impl Types for u32 {
1238 impl bar::Types2 for u32 {
1242 pub trait Behavior<T: Types + bar::Types2> {
1243 fn reproduce(&self, foo: T::Bar);
1248 impl Behavior<u32> for Impl {
1249 fn reproduce(&self, foo: <u32 as bar::Types2>::Bar) {
1257 fn test_transform_path_in_path_expr() {
1259 add_missing_default_members,
1265 pub trait Trait<T: Const> {
1274 impl Const for u32 {
1280 impl Trait<u32> for Impl { $0 }"#,
1286 pub trait Trait<T: Const> {
1295 impl Const for u32 {
1301 impl Trait<u32> for Impl {
1302 $0fn foo() -> bool {
1303 match <u32 as Const>::FOO {
1313 fn test_default_partial_eq() {
1315 add_missing_default_members,
1320 field: (usize, usize),
1322 impl PartialEq for SomeStruct {$0}
1327 field: (usize, usize),
1329 impl PartialEq for SomeStruct {
1330 $0fn ne(&self, other: &Self) -> bool {