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 DefaultMethods
::No
= mode
{
149 if let ast
::AssocItem
::Fn(func
) = &first_new_item
{
150 if try_gen_trait_body(ctx
, func
, &trait_
, &impl_def
).is_none() {
152 func
.syntax().descendants().find_map(ast
::MacroCall
::cast
)
154 if m
.syntax().text() == "todo!()" {
156 cursor
= Cursor
::Replace(placeholder
.syntax());
162 builder
.replace_snippet(
165 render_snippet(cap
, new_impl_def
.syntax(), cursor
),
172 fn try_gen_trait_body(
173 ctx
: &AssistContext
<'_
>,
176 impl_def
: &ast
::Impl
,
178 let trait_path
= make
::ext
::ident_path(&trait_
.name(ctx
.db()).to_string());
179 let hir_ty
= ctx
.sema
.resolve_type(&impl_def
.self_ty()?
)?
;
180 let adt
= hir_ty
.as_adt()?
.source(ctx
.db())?
;
181 gen_trait_fn_body(func
, &trait_path
, &adt
.value
)
186 use crate::tests
::{check_assist, check_assist_not_applicable}
;
191 fn test_add_missing_impl_members() {
193 add_missing_impl_members
,
198 const CONST: usize = 42;
215 const CONST: usize = 42;
229 const CONST: usize = 42;
244 fn test_copied_overriden_members() {
246 add_missing_impl_members
,
250 fn bar(&self) -> bool { true }
251 fn baz(&self) -> u32 { 42 }
263 fn bar(&self) -> bool { true }
264 fn baz(&self) -> u32 { 42 }
281 fn test_empty_impl_def() {
283 add_missing_impl_members
,
285 trait Foo { fn foo(&self); }
287 impl Foo for S { $0 }"#,
289 trait Foo { fn foo(&self); }
300 fn test_impl_def_without_braces() {
302 add_missing_impl_members
,
304 trait Foo { fn foo(&self); }
308 trait Foo { fn foo(&self); }
319 fn fill_in_type_params_1() {
321 add_missing_impl_members
,
323 trait Foo<T> { fn foo(&self, t: T) -> &T; }
325 impl Foo<u32> for S { $0 }"#,
327 trait Foo<T> { fn foo(&self, t: T) -> &T; }
329 impl Foo<u32> for S {
330 fn foo(&self, t: u32) -> &u32 {
338 fn fill_in_type_params_2() {
340 add_missing_impl_members
,
342 trait Foo<T> { fn foo(&self, t: T) -> &T; }
344 impl<U> Foo<U> for S { $0 }"#,
346 trait Foo<T> { fn foo(&self, t: T) -> &T; }
348 impl<U> Foo<U> for S {
349 fn foo(&self, t: U) -> &U {
357 fn test_cursor_after_empty_impl_def() {
359 add_missing_impl_members
,
361 trait Foo { fn foo(&self); }
363 impl Foo for S {}$0"#,
365 trait Foo { fn foo(&self); }
376 fn test_qualify_path_1() {
378 add_missing_impl_members
,
382 trait Foo { fn foo(&self, bar: Bar); }
385 impl foo::Foo for S { $0 }"#,
389 trait Foo { fn foo(&self, bar: Bar); }
392 impl foo::Foo for S {
393 fn foo(&self, bar: foo::Bar) {
401 fn test_qualify_path_2() {
403 add_missing_impl_members
,
408 pub trait Foo { fn foo(&self, bar: Bar); }
415 impl bar::Foo for S { $0 }"#,
420 pub trait Foo { fn foo(&self, bar: Bar); }
427 impl bar::Foo for S {
428 fn foo(&self, bar: bar::Bar) {
436 fn test_qualify_path_generic() {
438 add_missing_impl_members
,
442 trait Foo { fn foo(&self, bar: Bar<u32>); }
445 impl foo::Foo for S { $0 }"#,
449 trait Foo { fn foo(&self, bar: Bar<u32>); }
452 impl foo::Foo for S {
453 fn foo(&self, bar: foo::Bar<u32>) {
461 fn test_qualify_path_and_substitute_param() {
463 add_missing_impl_members
,
467 trait Foo<T> { fn foo(&self, bar: Bar<T>); }
470 impl foo::Foo<u32> for S { $0 }"#,
474 trait Foo<T> { fn foo(&self, bar: Bar<T>); }
477 impl foo::Foo<u32> for S {
478 fn foo(&self, bar: foo::Bar<u32>) {
486 fn test_substitute_param_no_qualify() {
487 // when substituting params, the substituted param should not be qualified!
489 add_missing_impl_members
,
492 trait Foo<T> { fn foo(&self, bar: T); }
497 impl foo::Foo<Param> for S { $0 }"#,
500 trait Foo<T> { fn foo(&self, bar: T); }
505 impl foo::Foo<Param> for S {
506 fn foo(&self, bar: Param) {
514 fn test_qualify_path_associated_item() {
516 add_missing_impl_members
,
520 impl Bar<T> { type Assoc = u32; }
521 trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
524 impl foo::Foo for S { $0 }"#,
528 impl Bar<T> { type Assoc = u32; }
529 trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
532 impl foo::Foo for S {
533 fn foo(&self, bar: foo::Bar<u32>::Assoc) {
541 fn test_qualify_path_nested() {
543 add_missing_impl_members
,
548 trait Foo { fn foo(&self, bar: Bar<Baz>); }
551 impl foo::Foo for S { $0 }"#,
556 trait Foo { fn foo(&self, bar: Bar<Baz>); }
559 impl foo::Foo for S {
560 fn foo(&self, bar: foo::Bar<foo::Baz>) {
568 fn test_qualify_path_fn_trait_notation() {
570 add_missing_impl_members
,
573 pub trait Fn<Args> { type Output; }
574 trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
577 impl foo::Foo for S { $0 }"#,
580 pub trait Fn<Args> { type Output; }
581 trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
584 impl foo::Foo for S {
585 fn foo(&self, bar: dyn Fn(u32) -> i32) {
593 fn test_empty_trait() {
594 check_assist_not_applicable(
595 add_missing_impl_members
,
599 impl Foo for S { $0 }"#,
604 fn test_ignore_unnamed_trait_members_and_default_methods() {
605 check_assist_not_applicable(
606 add_missing_impl_members
,
610 fn valid(some: u32) -> bool { false }
613 impl Foo for S { $0 }"#,
618 fn test_with_docstring_and_attrs() {
620 add_missing_impl_members
,
622 #[doc(alias = "test alias")]
631 impl Foo for S {}$0"#,
633 #[doc(alias = "test alias")]
653 fn test_default_methods() {
655 add_missing_default_members
,
660 const CONST: usize = 42;
662 fn valid(some: u32) -> bool { false }
663 fn foo(some: u32) -> bool;
666 impl Foo for S { $0 }"#,
671 const CONST: usize = 42;
673 fn valid(some: u32) -> bool { false }
674 fn foo(some: u32) -> bool;
678 $0fn valid(some: u32) -> bool { false }
684 fn test_generic_single_default_parameter() {
686 add_missing_impl_members
,
688 trait Foo<T = Self> {
689 fn bar(&self, other: &T);
693 impl Foo for S { $0 }"#,
695 trait Foo<T = Self> {
696 fn bar(&self, other: &T);
701 fn bar(&self, other: &Self) {
709 fn test_generic_default_parameter_is_second() {
711 add_missing_impl_members
,
713 trait Foo<T1, T2 = Self> {
714 fn bar(&self, this: &T1, that: &T2);
718 impl Foo<T> for S<T> { $0 }"#,
720 trait Foo<T1, T2 = Self> {
721 fn bar(&self, this: &T1, that: &T2);
725 impl Foo<T> for S<T> {
726 fn bar(&self, this: &T, that: &Self) {
734 fn test_assoc_type_bounds_are_removed() {
736 add_missing_impl_members
,
739 type Ty: Copy + 'static;
746 type Ty: Copy + 'static;
756 fn test_whitespace_fixup_preserves_bad_tokens() {
758 add_missing_impl_members
,
782 fn test_whitespace_fixup_preserves_comments() {
784 add_missing_impl_members
,
810 add_missing_impl_members
,
813 fn foo(&self, x: crate)
821 fn foo(&self, x: crate)
824 fn foo(&self, x: crate) {
833 fn missing_generic_type() {
835 add_missing_impl_members
,
838 fn foo(&self, bar: BAR);
846 fn foo(&self, bar: BAR);
849 fn foo(&self, bar: BAR) {
858 fn does_not_requalify_self_as_crate() {
860 add_missing_default_members
,
862 struct Wrapper<T>(T);
865 fn f(self) -> Wrapper<Self> {
875 struct Wrapper<T>(T);
878 fn f(self) -> Wrapper<Self> {
884 $0fn f(self) -> Wrapper<Self> {
893 fn test_default_body_generation() {
895 add_missing_impl_members
,
897 //- minicore: default
900 impl Default for Foo {
907 impl Default for Foo {
908 $0fn default() -> Self {
909 Self(Default::default())
917 fn test_from_macro() {
919 add_missing_default_members
,
924 fn foo<'lt>(&'lt self) {}
939 fn foo<'lt>(&'lt self) {}
947 $0fn foo< 'lt>(& 'lt self){}
954 fn test_assoc_type_when_trait_with_same_name_in_scope() {
956 add_missing_impl_members
,
964 pub trait Behavior<T: Types> {
965 fn reproduce(&self, foo: T::Foo);
970 impl<T: Types> Behavior<T> for Impl { $0 }"#,
978 pub trait Behavior<T: Types> {
979 fn reproduce(&self, foo: T::Foo);
984 impl<T: Types> Behavior<T> for Impl {
985 fn reproduce(&self, foo: <T as Types>::Foo) {
993 fn test_assoc_type_on_concrete_type() {
995 add_missing_impl_members
,
1001 impl Types for u32 {
1005 pub trait Behavior<T: Types> {
1006 fn reproduce(&self, foo: T::Foo);
1011 impl Behavior<u32> for Impl { $0 }"#,
1017 impl Types for u32 {
1021 pub trait Behavior<T: Types> {
1022 fn reproduce(&self, foo: T::Foo);
1027 impl Behavior<u32> for Impl {
1028 fn reproduce(&self, foo: <u32 as Types>::Foo) {
1036 fn test_assoc_type_on_concrete_type_qualified() {
1038 add_missing_impl_members
,
1044 impl Types for std::string::String {
1048 pub trait Behavior<T: Types> {
1049 fn reproduce(&self, foo: T::Foo);
1054 impl Behavior<std::string::String> for Impl { $0 }"#,
1060 impl Types for std::string::String {
1064 pub trait Behavior<T: Types> {
1065 fn reproduce(&self, foo: T::Foo);
1070 impl Behavior<std::string::String> for Impl {
1071 fn reproduce(&self, foo: <std::string::String as Types>::Foo) {
1079 fn test_assoc_type_on_concrete_type_multi_option_ambiguous() {
1081 add_missing_impl_members
,
1091 impl Types for u32 {
1095 impl Types2 for u32 {
1099 pub trait Behavior<T: Types + Types2> {
1100 fn reproduce(&self, foo: <T as Types2>::Foo);
1105 impl Behavior<u32> for Impl { $0 }"#,
1115 impl Types for u32 {
1119 impl Types2 for u32 {
1123 pub trait Behavior<T: Types + Types2> {
1124 fn reproduce(&self, foo: <T as Types2>::Foo);
1129 impl Behavior<u32> for Impl {
1130 fn reproduce(&self, foo: <u32 as Types2>::Foo) {
1138 fn test_assoc_type_on_concrete_type_multi_option() {
1140 add_missing_impl_members
,
1150 impl Types for u32 {
1154 impl Types2 for u32 {
1158 pub trait Behavior<T: Types + Types2> {
1159 fn reproduce(&self, foo: T::Bar);
1164 impl Behavior<u32> for Impl { $0 }"#,
1174 impl Types for u32 {
1178 impl Types2 for u32 {
1182 pub trait Behavior<T: Types + Types2> {
1183 fn reproduce(&self, foo: T::Bar);
1188 impl Behavior<u32> for Impl {
1189 fn reproduce(&self, foo: <u32 as Types2>::Bar) {
1197 fn test_assoc_type_on_concrete_type_multi_option_foreign() {
1199 add_missing_impl_members
,
1211 impl Types for u32 {
1215 impl bar::Types2 for u32 {
1219 pub trait Behavior<T: Types + bar::Types2> {
1220 fn reproduce(&self, foo: T::Bar);
1225 impl Behavior<u32> for Impl { $0 }"#,
1237 impl Types for u32 {
1241 impl bar::Types2 for u32 {
1245 pub trait Behavior<T: Types + bar::Types2> {
1246 fn reproduce(&self, foo: T::Bar);
1251 impl Behavior<u32> for Impl {
1252 fn reproduce(&self, foo: <u32 as bar::Types2>::Bar) {
1260 fn test_transform_path_in_path_expr() {
1262 add_missing_default_members
,
1268 pub trait Trait<T: Const> {
1277 impl Const for u32 {
1283 impl Trait<u32> for Impl { $0 }"#,
1289 pub trait Trait<T: Const> {
1298 impl Const for u32 {
1304 impl Trait<u32> for Impl {
1305 $0fn foo() -> bool {
1306 match <u32 as Const>::FOO {
1316 fn test_default_partial_eq() {
1318 add_missing_default_members
,
1323 field: (usize, usize),
1325 impl PartialEq for SomeStruct {$0}
1330 field: (usize, usize),
1332 impl PartialEq for SomeStruct {
1333 $0fn ne(&self, other: &Self) -> bool {