From 0209c28136bd9d2684fd39d6f0e13b3e32ae0526 Mon Sep 17 00:00:00 2001 From: dfireBird Date: Mon, 12 Feb 2024 22:56:03 +0530 Subject: [PATCH 1/4] add completions to show only traits in trait `impl` statement --- crates/ide-completion/src/completions/type.rs | 10 ++++++++++ crates/ide-completion/src/tests/type_pos.rs | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/crates/ide-completion/src/completions/type.rs b/crates/ide-completion/src/completions/type.rs index e6a4335c3fec..eca17bb7c7f2 100644 --- a/crates/ide-completion/src/completions/type.rs +++ b/crates/ide-completion/src/completions/type.rs @@ -184,6 +184,16 @@ pub(crate) fn complete_type_path( } } } + TypeLocation::ImplTrait => { + acc.add_nameref_keywords_with_colon(ctx); + ctx.process_all_names(&mut |name, def, doc_aliases| { + let is_trait = matches!(def, ScopeDef::ModuleDef(hir::ModuleDef::Trait(_))); + if is_trait { + acc.add_path_resolution(ctx, path_ctx, name, def, doc_aliases); + } + }); + return; + } _ => {} }; diff --git a/crates/ide-completion/src/tests/type_pos.rs b/crates/ide-completion/src/tests/type_pos.rs index c7161f82ce74..656592ebfd62 100644 --- a/crates/ide-completion/src/tests/type_pos.rs +++ b/crates/ide-completion/src/tests/type_pos.rs @@ -989,3 +989,21 @@ fn foo<'a>() { S::<'static, F$0, _, _>; } "#]], ); } + +#[test] +fn complete_traits_on_impl_trait_block() { + check( + r#" +trait Foo {} + +struct Bar; + +impl $0 for Bar { }"#, + expect![[r#" + tt Foo + tt Trait + kw crate:: + kw self:: + "#]], + ); +} From 9897662bf70f3cbf79291ff8a880abce021ec2e2 Mon Sep 17 00:00:00 2001 From: dfireBird Date: Tue, 13 Feb 2024 18:22:32 +0530 Subject: [PATCH 2/4] add completions to show only traits with qualified path prefix --- crates/ide-completion/src/completions/type.rs | 14 +++++++++-- crates/ide-completion/src/context.rs | 1 + crates/ide-completion/src/tests/type_pos.rs | 24 ++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/ide-completion/src/completions/type.rs b/crates/ide-completion/src/completions/type.rs index eca17bb7c7f2..e4678089462a 100644 --- a/crates/ide-completion/src/completions/type.rs +++ b/crates/ide-completion/src/completions/type.rs @@ -31,6 +31,11 @@ pub(crate) fn complete_type_path( ScopeDef::ImplSelfType(_) => location.complete_self_type(), // Don't suggest attribute macros and derives. ScopeDef::ModuleDef(Macro(mac)) => mac.is_fn_like(ctx.db), + ScopeDef::ModuleDef(Trait(_) | Module(_)) + if matches!(location, TypeLocation::ImplTrait) => + { + true + } // Type things are fine ScopeDef::ModuleDef( BuiltinType(_) | Adt(_) | Module(_) | Trait(_) | TraitAlias(_) | TypeAlias(_), @@ -187,8 +192,13 @@ pub(crate) fn complete_type_path( TypeLocation::ImplTrait => { acc.add_nameref_keywords_with_colon(ctx); ctx.process_all_names(&mut |name, def, doc_aliases| { - let is_trait = matches!(def, ScopeDef::ModuleDef(hir::ModuleDef::Trait(_))); - if is_trait { + let is_trait_or_module = matches!( + def, + ScopeDef::ModuleDef( + hir::ModuleDef::Module(_) | hir::ModuleDef::Trait(_) + ) + ); + if is_trait_or_module { acc.add_path_resolution(ctx, path_ctx, name, def, doc_aliases); } }); diff --git a/crates/ide-completion/src/context.rs b/crates/ide-completion/src/context.rs index 2a0004f60b82..314f84f5df16 100644 --- a/crates/ide-completion/src/context.rs +++ b/crates/ide-completion/src/context.rs @@ -202,6 +202,7 @@ impl TypeLocation { } TypeLocation::AssocConstEq => false, TypeLocation::AssocTypeEq => true, + TypeLocation::ImplTrait => false, _ => true, } } diff --git a/crates/ide-completion/src/tests/type_pos.rs b/crates/ide-completion/src/tests/type_pos.rs index 656592ebfd62..db4ac9381ced 100644 --- a/crates/ide-completion/src/tests/type_pos.rs +++ b/crates/ide-completion/src/tests/type_pos.rs @@ -998,8 +998,10 @@ trait Foo {} struct Bar; -impl $0 for Bar { }"#, +impl $0 for Bar { } +"#, expect![[r#" + md module tt Foo tt Trait kw crate:: @@ -1007,3 +1009,23 @@ impl $0 for Bar { }"#, "#]], ); } + +#[test] +fn complete_traits_with_path_on_impl_trait_block() { + check( + r#" +mod outer { + pub trait Foo {} + pub struct Bar; + pub mod inner { + } +} + +impl outer::$0 for Bar { } +"#, + expect![[r#" + md inner + tt Foo + "#]], + ); +} From 0e47befaf3cdf30fcf851fd34d4d6fdb4d274382 Mon Sep 17 00:00:00 2001 From: dfireBird Date: Tue, 13 Feb 2024 19:31:04 +0530 Subject: [PATCH 3/4] fix flyimport showing other types in `impl` trait statement --- .../src/completions/flyimport.rs | 2 ++ crates/ide-completion/src/tests/flyimport.rs | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/crates/ide-completion/src/completions/flyimport.rs b/crates/ide-completion/src/completions/flyimport.rs index b9f91d34b2c2..1c6cbf713bc8 100644 --- a/crates/ide-completion/src/completions/flyimport.rs +++ b/crates/ide-completion/src/completions/flyimport.rs @@ -238,6 +238,8 @@ fn import_on_the_fly( (PathKind::Type { location }, ItemInNs::Types(ty)) => { if matches!(location, TypeLocation::TypeBound) { matches!(ty, ModuleDef::Trait(_)) + } else if matches!(location, TypeLocation::ImplTrait) { + matches!(ty, ModuleDef::Trait(_)) || matches!(ty, ModuleDef::Module(_)) } else { true } diff --git a/crates/ide-completion/src/tests/flyimport.rs b/crates/ide-completion/src/tests/flyimport.rs index eaa1bebc03c7..fff193ba4c9b 100644 --- a/crates/ide-completion/src/tests/flyimport.rs +++ b/crates/ide-completion/src/tests/flyimport.rs @@ -1397,3 +1397,22 @@ pub use bridge2::server2::Span2; "#]], ); } + +#[test] +fn flyimport_only_traits_in_impl_trait_block() { + check( + r#" +//- /main.rs crate:main deps:dep +pub struct Bar; + +impl Foo$0 for Bar { } +//- /lib.rs crate:dep +pub trait FooTrait; + +pub struct FooStruct; +"#, + expect![[r#" + tt FooTrait (use dep::FooTrait) + "#]], + ); +} From 7f661782cdd09dc9b94230e979b5f26b751803d9 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 13 Feb 2024 17:26:14 +0100 Subject: [PATCH 4/4] Simplify --- crates/ide-completion/src/completions/flyimport.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ide-completion/src/completions/flyimport.rs b/crates/ide-completion/src/completions/flyimport.rs index 1c6cbf713bc8..3bc329ecd748 100644 --- a/crates/ide-completion/src/completions/flyimport.rs +++ b/crates/ide-completion/src/completions/flyimport.rs @@ -239,7 +239,7 @@ fn import_on_the_fly( if matches!(location, TypeLocation::TypeBound) { matches!(ty, ModuleDef::Trait(_)) } else if matches!(location, TypeLocation::ImplTrait) { - matches!(ty, ModuleDef::Trait(_)) || matches!(ty, ModuleDef::Module(_)) + matches!(ty, ModuleDef::Trait(_) | ModuleDef::Module(_)) } else { true }