Skip to content

Commit fc1ee61

Browse files
committed
Auto merge of rust-lang#16544 - dfireBird:impl_trait_completion, r=Veykril
Add completions to show only traits in trait `impl` statement This is prerequisite PR for adding the assist mentioned in rust-lang#12500 P.S: If wanted, I will add the implementation of the assist in this PR as well.
2 parents 22ca3fd + 7f66178 commit fc1ee61

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

crates/ide-completion/src/completions/flyimport.rs

+2
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ fn import_on_the_fly(
238238
(PathKind::Type { location }, ItemInNs::Types(ty)) => {
239239
if matches!(location, TypeLocation::TypeBound) {
240240
matches!(ty, ModuleDef::Trait(_))
241+
} else if matches!(location, TypeLocation::ImplTrait) {
242+
matches!(ty, ModuleDef::Trait(_) | ModuleDef::Module(_))
241243
} else {
242244
true
243245
}

crates/ide-completion/src/completions/type.rs

+20
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ pub(crate) fn complete_type_path(
3131
ScopeDef::ImplSelfType(_) => location.complete_self_type(),
3232
// Don't suggest attribute macros and derives.
3333
ScopeDef::ModuleDef(Macro(mac)) => mac.is_fn_like(ctx.db),
34+
ScopeDef::ModuleDef(Trait(_) | Module(_))
35+
if matches!(location, TypeLocation::ImplTrait) =>
36+
{
37+
true
38+
}
3439
// Type things are fine
3540
ScopeDef::ModuleDef(
3641
BuiltinType(_) | Adt(_) | Module(_) | Trait(_) | TraitAlias(_) | TypeAlias(_),
@@ -184,6 +189,21 @@ pub(crate) fn complete_type_path(
184189
}
185190
}
186191
}
192+
TypeLocation::ImplTrait => {
193+
acc.add_nameref_keywords_with_colon(ctx);
194+
ctx.process_all_names(&mut |name, def, doc_aliases| {
195+
let is_trait_or_module = matches!(
196+
def,
197+
ScopeDef::ModuleDef(
198+
hir::ModuleDef::Module(_) | hir::ModuleDef::Trait(_)
199+
)
200+
);
201+
if is_trait_or_module {
202+
acc.add_path_resolution(ctx, path_ctx, name, def, doc_aliases);
203+
}
204+
});
205+
return;
206+
}
187207
_ => {}
188208
};
189209

crates/ide-completion/src/context.rs

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ impl TypeLocation {
202202
}
203203
TypeLocation::AssocConstEq => false,
204204
TypeLocation::AssocTypeEq => true,
205+
TypeLocation::ImplTrait => false,
205206
_ => true,
206207
}
207208
}

crates/ide-completion/src/tests/flyimport.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1397,3 +1397,22 @@ pub use bridge2::server2::Span2;
13971397
"#]],
13981398
);
13991399
}
1400+
1401+
#[test]
1402+
fn flyimport_only_traits_in_impl_trait_block() {
1403+
check(
1404+
r#"
1405+
//- /main.rs crate:main deps:dep
1406+
pub struct Bar;
1407+
1408+
impl Foo$0 for Bar { }
1409+
//- /lib.rs crate:dep
1410+
pub trait FooTrait;
1411+
1412+
pub struct FooStruct;
1413+
"#,
1414+
expect![[r#"
1415+
tt FooTrait (use dep::FooTrait)
1416+
"#]],
1417+
);
1418+
}

crates/ide-completion/src/tests/type_pos.rs

+40
Original file line numberDiff line numberDiff line change
@@ -989,3 +989,43 @@ fn foo<'a>() { S::<'static, F$0, _, _>; }
989989
"#]],
990990
);
991991
}
992+
993+
#[test]
994+
fn complete_traits_on_impl_trait_block() {
995+
check(
996+
r#"
997+
trait Foo {}
998+
999+
struct Bar;
1000+
1001+
impl $0 for Bar { }
1002+
"#,
1003+
expect![[r#"
1004+
md module
1005+
tt Foo
1006+
tt Trait
1007+
kw crate::
1008+
kw self::
1009+
"#]],
1010+
);
1011+
}
1012+
1013+
#[test]
1014+
fn complete_traits_with_path_on_impl_trait_block() {
1015+
check(
1016+
r#"
1017+
mod outer {
1018+
pub trait Foo {}
1019+
pub struct Bar;
1020+
pub mod inner {
1021+
}
1022+
}
1023+
1024+
impl outer::$0 for Bar { }
1025+
"#,
1026+
expect![[r#"
1027+
md inner
1028+
tt Foo
1029+
"#]],
1030+
);
1031+
}

0 commit comments

Comments
 (0)