mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Restrict type bound completions to traits, modules and macros
This commit is contained in:
parent
d4877ae992
commit
9abd28ac37
3 changed files with 24 additions and 26 deletions
|
@ -36,6 +36,20 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if matches!(&ctx.completion_location, Some(ImmediateLocation::TypeBound)) {
|
||||||
|
ctx.scope.process_all_names(&mut |name, res| {
|
||||||
|
let add_resolution = match res {
|
||||||
|
ScopeDef::MacroDef(mac) => mac.is_fn_like(),
|
||||||
|
ScopeDef::ModuleDef(hir::ModuleDef::Trait(_) | hir::ModuleDef::Module(_)) => true,
|
||||||
|
_ => false,
|
||||||
|
};
|
||||||
|
if add_resolution {
|
||||||
|
acc.add_resolution(ctx, name, &res);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if !ctx.expects_type() {
|
if !ctx.expects_type() {
|
||||||
if let Some(hir::Adt::Enum(e)) =
|
if let Some(hir::Adt::Enum(e)) =
|
||||||
ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
|
ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
|
||||||
|
|
|
@ -36,6 +36,7 @@ pub(crate) enum ImmediateLocation {
|
||||||
IdentPat,
|
IdentPat,
|
||||||
BlockExpr,
|
BlockExpr,
|
||||||
ItemList,
|
ItemList,
|
||||||
|
TypeBound,
|
||||||
// Fake file ast node
|
// Fake file ast node
|
||||||
Attribute(ast::Attr),
|
Attribute(ast::Attr),
|
||||||
// Fake file ast node
|
// Fake file ast node
|
||||||
|
@ -154,6 +155,13 @@ pub(crate) fn determine_location(
|
||||||
ast::NameLike::Lifetime(lt) => lt.syntax().clone(),
|
ast::NameLike::Lifetime(lt) => lt.syntax().clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
match_ast! {
|
||||||
|
match node {
|
||||||
|
ast::TypeBoundList(_it) => return Some(ImmediateLocation::TypeBound),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let parent = match node.parent() {
|
let parent = match node.parent() {
|
||||||
Some(parent) => match ast::MacroCall::cast(parent.clone()) {
|
Some(parent) => match ast::MacroCall::cast(parent.clone()) {
|
||||||
// When a path is being typed in an (Assoc)ItemList the parser will always emit a macro_call.
|
// When a path is being typed in an (Assoc)ItemList the parser will always emit a macro_call.
|
||||||
|
@ -195,6 +203,8 @@ pub(crate) fn determine_location(
|
||||||
},
|
},
|
||||||
ast::TupleField(_it) => ImmediateLocation::TupleField,
|
ast::TupleField(_it) => ImmediateLocation::TupleField,
|
||||||
ast::TupleFieldList(_it) => ImmediateLocation::TupleField,
|
ast::TupleFieldList(_it) => ImmediateLocation::TupleField,
|
||||||
|
ast::TypeBound(_it) => ImmediateLocation::TypeBound,
|
||||||
|
ast::TypeBoundList(_it) => ImmediateLocation::TypeBound,
|
||||||
ast::AssocItemList(it) => match it.syntax().parent().map(|it| it.kind()) {
|
ast::AssocItemList(it) => match it.syntax().parent().map(|it| it.kind()) {
|
||||||
Some(IMPL) => ImmediateLocation::Impl,
|
Some(IMPL) => ImmediateLocation::Impl,
|
||||||
Some(TRAIT) => ImmediateLocation::Trait,
|
Some(TRAIT) => ImmediateLocation::Trait,
|
||||||
|
|
|
@ -37,17 +37,9 @@ fn bound_for_type_pred() {
|
||||||
struct Foo<'lt, T, const C: usize> where T: $0 {}
|
struct Foo<'lt, T, const C: usize> where T: $0 {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
sp Self
|
|
||||||
tp T
|
|
||||||
tt Trait
|
tt Trait
|
||||||
en Enum
|
|
||||||
st Record
|
|
||||||
st Tuple
|
|
||||||
md module
|
md module
|
||||||
st Foo<…>
|
|
||||||
st Unit
|
|
||||||
ma makro!(…) macro_rules! makro
|
ma makro!(…) macro_rules! makro
|
||||||
bt u32
|
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -60,40 +52,23 @@ fn bound_for_lifetime_pred() {
|
||||||
struct Foo<'lt, T, const C: usize> where 'lt: $0 {}
|
struct Foo<'lt, T, const C: usize> where 'lt: $0 {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
sp Self
|
|
||||||
tp T
|
|
||||||
tt Trait
|
tt Trait
|
||||||
en Enum
|
|
||||||
st Record
|
|
||||||
st Tuple
|
|
||||||
md module
|
md module
|
||||||
st Foo<…>
|
|
||||||
st Unit
|
|
||||||
ma makro!(…) macro_rules! makro
|
ma makro!(…) macro_rules! makro
|
||||||
bt u32
|
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn bound_for_for_pred() {
|
fn bound_for_for_pred() {
|
||||||
// FIXME: only show traits, macros and modules
|
|
||||||
check(
|
check(
|
||||||
r#"
|
r#"
|
||||||
struct Foo<'lt, T, const C: usize> where for<'a> T: $0 {}
|
struct Foo<'lt, T, const C: usize> where for<'a> T: $0 {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
sp Self
|
|
||||||
tp T
|
|
||||||
tt Trait
|
tt Trait
|
||||||
en Enum
|
|
||||||
st Record
|
|
||||||
st Tuple
|
|
||||||
md module
|
md module
|
||||||
st Foo<…>
|
|
||||||
st Unit
|
|
||||||
ma makro!(…) macro_rules! makro
|
ma makro!(…) macro_rules! makro
|
||||||
bt u32
|
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -120,7 +95,6 @@ struct Foo<'lt, T, const C: usize> where for<'a> $0 {}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn pred_on_fn_in_impl() {
|
fn pred_on_fn_in_impl() {
|
||||||
// FIXME: only show traits, macros and modules
|
|
||||||
check(
|
check(
|
||||||
r#"
|
r#"
|
||||||
impl Record {
|
impl Record {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue