1 use ide_db::traits::resolve_target_trait;
2 use syntax::ast::{self, AstNode};
5 assist_context::{AssistContext, Assists},
7 add_trait_assoc_items_to_impl, filter_assoc_items, render_snippet, Cursor, DefaultMethods,
12 // Assist: add_impl_missing_members
14 // Adds scaffold for required impl members.
19 // fn foo(&self) -> T;
23 // impl Trait<u32> for () {$0
31 // fn foo(&self) -> T;
35 // impl Trait<u32> for () {
38 // fn foo(&self) -> u32 {
43 pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
44 add_missing_impl_members_inner(
48 "add_impl_missing_members",
49 "Implement missing members",
53 // Assist: add_impl_default_members
55 // Adds scaffold for overriding default impl members.
64 // impl Trait for () {
77 // impl Trait for () {
84 pub(crate) fn add_missing_default_members(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
85 add_missing_impl_members_inner(
89 "add_impl_default_members",
90 "Implement default members",
94 fn add_missing_impl_members_inner(
98 assist_id: &'static str,
101 let _p = profile::span("add_missing_impl_members_inner");
102 let impl_def = ctx.find_node_at_offset::<ast::Impl>()?;
103 let trait_ = resolve_target_trait(&ctx.sema, &impl_def)?;
105 let missing_items = filter_assoc_items(
107 &ide_db::traits::get_missing_assoc_items(&ctx.sema, &impl_def),
111 if missing_items.is_empty() {
115 let target = impl_def.syntax().text_range();
116 acc.add(AssistId(assist_id, AssistKind::QuickFix), label, target, |builder| {
117 let target_scope = ctx.sema.scope(impl_def.syntax());
118 let (new_impl_def, first_new_item) =
119 add_trait_assoc_items_to_impl(&ctx.sema, missing_items, trait_, impl_def, target_scope);
120 match ctx.config.snippet_cap {
121 None => builder.replace(target, new_impl_def.to_string()),
123 let mut cursor = Cursor::Before(first_new_item.syntax());
125 if let ast::AssocItem::Fn(func) = &first_new_item {
126 if let Some(m) = func.syntax().descendants().find_map(ast::MacroCall::cast) {
127 if m.syntax().text() == "todo!()" {
129 cursor = Cursor::Replace(placeholder.syntax());
133 builder.replace_snippet(
136 render_snippet(cap, new_impl_def.syntax(), cursor),
145 use crate::tests::{check_assist, check_assist_not_applicable};
150 fn test_add_missing_impl_members() {
152 add_missing_impl_members,
157 const CONST: usize = 42;
174 const CONST: usize = 42;
188 const CONST: usize = 42;
203 fn test_copied_overriden_members() {
205 add_missing_impl_members,
209 fn bar(&self) -> bool { true }
210 fn baz(&self) -> u32 { 42 }
222 fn bar(&self) -> bool { true }
223 fn baz(&self) -> u32 { 42 }
240 fn test_empty_impl_def() {
242 add_missing_impl_members,
244 trait Foo { fn foo(&self); }
246 impl Foo for S { $0 }"#,
248 trait Foo { fn foo(&self); }
259 fn test_impl_def_without_braces() {
261 add_missing_impl_members,
263 trait Foo { fn foo(&self); }
267 trait Foo { fn foo(&self); }
278 fn fill_in_type_params_1() {
280 add_missing_impl_members,
282 trait Foo<T> { fn foo(&self, t: T) -> &T; }
284 impl Foo<u32> for S { $0 }"#,
286 trait Foo<T> { fn foo(&self, t: T) -> &T; }
288 impl Foo<u32> for S {
289 fn foo(&self, t: u32) -> &u32 {
297 fn fill_in_type_params_2() {
299 add_missing_impl_members,
301 trait Foo<T> { fn foo(&self, t: T) -> &T; }
303 impl<U> Foo<U> for S { $0 }"#,
305 trait Foo<T> { fn foo(&self, t: T) -> &T; }
307 impl<U> Foo<U> for S {
308 fn foo(&self, t: U) -> &U {
316 fn test_cursor_after_empty_impl_def() {
318 add_missing_impl_members,
320 trait Foo { fn foo(&self); }
322 impl Foo for S {}$0"#,
324 trait Foo { fn foo(&self); }
335 fn test_qualify_path_1() {
337 add_missing_impl_members,
341 trait Foo { fn foo(&self, bar: Bar); }
344 impl foo::Foo for S { $0 }"#,
348 trait Foo { fn foo(&self, bar: Bar); }
351 impl foo::Foo for S {
352 fn foo(&self, bar: foo::Bar) {
360 fn test_qualify_path_2() {
362 add_missing_impl_members,
367 pub trait Foo { fn foo(&self, bar: Bar); }
374 impl bar::Foo for S { $0 }"#,
379 pub trait Foo { fn foo(&self, bar: Bar); }
386 impl bar::Foo for S {
387 fn foo(&self, bar: bar::Bar) {
395 fn test_qualify_path_generic() {
397 add_missing_impl_members,
401 trait Foo { fn foo(&self, bar: Bar<u32>); }
404 impl foo::Foo for S { $0 }"#,
408 trait Foo { fn foo(&self, bar: Bar<u32>); }
411 impl foo::Foo for S {
412 fn foo(&self, bar: foo::Bar<u32>) {
420 fn test_qualify_path_and_substitute_param() {
422 add_missing_impl_members,
426 trait Foo<T> { fn foo(&self, bar: Bar<T>); }
429 impl foo::Foo<u32> for S { $0 }"#,
433 trait Foo<T> { fn foo(&self, bar: Bar<T>); }
436 impl foo::Foo<u32> for S {
437 fn foo(&self, bar: foo::Bar<u32>) {
445 fn test_substitute_param_no_qualify() {
446 // when substituting params, the substituted param should not be qualified!
448 add_missing_impl_members,
451 trait Foo<T> { fn foo(&self, bar: T); }
456 impl foo::Foo<Param> for S { $0 }"#,
459 trait Foo<T> { fn foo(&self, bar: T); }
464 impl foo::Foo<Param> for S {
465 fn foo(&self, bar: Param) {
473 fn test_qualify_path_associated_item() {
475 add_missing_impl_members,
479 impl Bar<T> { type Assoc = u32; }
480 trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
483 impl foo::Foo for S { $0 }"#,
487 impl Bar<T> { type Assoc = u32; }
488 trait Foo { fn foo(&self, bar: Bar<u32>::Assoc); }
491 impl foo::Foo for S {
492 fn foo(&self, bar: foo::Bar<u32>::Assoc) {
500 fn test_qualify_path_nested() {
502 add_missing_impl_members,
507 trait Foo { fn foo(&self, bar: Bar<Baz>); }
510 impl foo::Foo for S { $0 }"#,
515 trait Foo { fn foo(&self, bar: Bar<Baz>); }
518 impl foo::Foo for S {
519 fn foo(&self, bar: foo::Bar<foo::Baz>) {
527 fn test_qualify_path_fn_trait_notation() {
529 add_missing_impl_members,
532 pub trait Fn<Args> { type Output; }
533 trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
536 impl foo::Foo for S { $0 }"#,
539 pub trait Fn<Args> { type Output; }
540 trait Foo { fn foo(&self, bar: dyn Fn(u32) -> i32); }
543 impl foo::Foo for S {
544 fn foo(&self, bar: dyn Fn(u32) -> i32) {
552 fn test_empty_trait() {
553 check_assist_not_applicable(
554 add_missing_impl_members,
558 impl Foo for S { $0 }"#,
563 fn test_ignore_unnamed_trait_members_and_default_methods() {
564 check_assist_not_applicable(
565 add_missing_impl_members,
569 fn valid(some: u32) -> bool { false }
572 impl Foo for S { $0 }"#,
577 fn test_with_docstring_and_attrs() {
579 add_missing_impl_members,
581 #[doc(alias = "test alias")]
590 impl Foo for S {}$0"#,
592 #[doc(alias = "test alias")]
612 fn test_default_methods() {
614 add_missing_default_members,
619 const CONST: usize = 42;
621 fn valid(some: u32) -> bool { false }
622 fn foo(some: u32) -> bool;
625 impl Foo for S { $0 }"#,
630 const CONST: usize = 42;
632 fn valid(some: u32) -> bool { false }
633 fn foo(some: u32) -> bool;
637 $0fn valid(some: u32) -> bool { false }
643 fn test_generic_single_default_parameter() {
645 add_missing_impl_members,
647 trait Foo<T = Self> {
648 fn bar(&self, other: &T);
652 impl Foo for S { $0 }"#,
654 trait Foo<T = Self> {
655 fn bar(&self, other: &T);
660 fn bar(&self, other: &Self) {
668 fn test_generic_default_parameter_is_second() {
670 add_missing_impl_members,
672 trait Foo<T1, T2 = Self> {
673 fn bar(&self, this: &T1, that: &T2);
677 impl Foo<T> for S<T> { $0 }"#,
679 trait Foo<T1, T2 = Self> {
680 fn bar(&self, this: &T1, that: &T2);
684 impl Foo<T> for S<T> {
685 fn bar(&self, this: &T, that: &Self) {
693 fn test_assoc_type_bounds_are_removed() {
695 add_missing_impl_members,
698 type Ty: Copy + 'static;
705 type Ty: Copy + 'static;
715 fn test_whitespace_fixup_preserves_bad_tokens() {
717 add_missing_impl_members,
741 fn test_whitespace_fixup_preserves_comments() {
743 add_missing_impl_members,
769 add_missing_impl_members,
772 fn foo(&self, x: crate)
780 fn foo(&self, x: crate)
783 fn foo(&self, x: crate) {
792 fn missing_generic_type() {
794 add_missing_impl_members,
797 fn foo(&self, bar: BAR);
805 fn foo(&self, bar: BAR);
808 fn foo(&self, bar: BAR) {