mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 04:19:13 +00:00
Auto merge of #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 #12500 P.S: If wanted, I will add the implementation of the assist in this PR as well.
This commit is contained in:
commit
fc1ee6136c
5 changed files with 82 additions and 0 deletions
|
@ -238,6 +238,8 @@ fn import_on_the_fly(
|
||||||
(PathKind::Type { location }, ItemInNs::Types(ty)) => {
|
(PathKind::Type { location }, ItemInNs::Types(ty)) => {
|
||||||
if matches!(location, TypeLocation::TypeBound) {
|
if matches!(location, TypeLocation::TypeBound) {
|
||||||
matches!(ty, ModuleDef::Trait(_))
|
matches!(ty, ModuleDef::Trait(_))
|
||||||
|
} else if matches!(location, TypeLocation::ImplTrait) {
|
||||||
|
matches!(ty, ModuleDef::Trait(_) | ModuleDef::Module(_))
|
||||||
} else {
|
} else {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,11 @@ pub(crate) fn complete_type_path(
|
||||||
ScopeDef::ImplSelfType(_) => location.complete_self_type(),
|
ScopeDef::ImplSelfType(_) => location.complete_self_type(),
|
||||||
// Don't suggest attribute macros and derives.
|
// Don't suggest attribute macros and derives.
|
||||||
ScopeDef::ModuleDef(Macro(mac)) => mac.is_fn_like(ctx.db),
|
ScopeDef::ModuleDef(Macro(mac)) => mac.is_fn_like(ctx.db),
|
||||||
|
ScopeDef::ModuleDef(Trait(_) | Module(_))
|
||||||
|
if matches!(location, TypeLocation::ImplTrait) =>
|
||||||
|
{
|
||||||
|
true
|
||||||
|
}
|
||||||
// Type things are fine
|
// Type things are fine
|
||||||
ScopeDef::ModuleDef(
|
ScopeDef::ModuleDef(
|
||||||
BuiltinType(_) | Adt(_) | Module(_) | Trait(_) | TraitAlias(_) | TypeAlias(_),
|
BuiltinType(_) | Adt(_) | Module(_) | Trait(_) | TraitAlias(_) | TypeAlias(_),
|
||||||
|
@ -184,6 +189,21 @@ 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_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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -202,6 +202,7 @@ impl TypeLocation {
|
||||||
}
|
}
|
||||||
TypeLocation::AssocConstEq => false,
|
TypeLocation::AssocConstEq => false,
|
||||||
TypeLocation::AssocTypeEq => true,
|
TypeLocation::AssocTypeEq => true,
|
||||||
|
TypeLocation::ImplTrait => false,
|
||||||
_ => true,
|
_ => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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)
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
@ -989,3 +989,43 @@ 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#"
|
||||||
|
md module
|
||||||
|
tt Foo
|
||||||
|
tt Trait
|
||||||
|
kw crate::
|
||||||
|
kw self::
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue