Show const params in completions

This commit is contained in:
Lukas Wirth 2021-01-19 20:01:49 +01:00
parent bf889bcf3b
commit bed12833cc
4 changed files with 41 additions and 12 deletions

View file

@ -29,6 +29,10 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
} }
ctx.scope.process_all_names(&mut |name, res| { ctx.scope.process_all_names(&mut |name, res| {
if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
mark::hit!(skip_lifetime_completion);
return;
}
if ctx.use_item_syntax.is_some() { if ctx.use_item_syntax.is_some() {
if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) { if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
if name_ref.syntax().text() == name.to_string().as_str() { if name_ref.syntax().text() == name.to_string().as_str() {
@ -37,7 +41,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
} }
} }
} }
acc.add_resolution(ctx, name.to_string(), &res) acc.add_resolution(ctx, name.to_string(), &res);
}); });
} }
@ -234,6 +238,24 @@ fn main() {
fn quux() fn quux<T>() fn quux() fn quux<T>()
"#]], "#]],
); );
check(
r#"fn quux<const C: usize>() { $0 }"#,
expect![[r#"
tp C
fn quux() fn quux<const C: usize>()
"#]],
);
}
#[test]
fn does_not_complete_lifetimes() {
mark::check!(skip_lifetime_completion);
check(
r#"fn quux<'a>() { $0 }"#,
expect![[r#"
fn quux() fn quux<'a>()
"#]],
);
} }
#[test] #[test]

View file

@ -2045,7 +2045,7 @@ impl Callable {
pub enum ScopeDef { pub enum ScopeDef {
ModuleDef(ModuleDef), ModuleDef(ModuleDef),
MacroDef(MacroDef), MacroDef(MacroDef),
GenericParam(TypeParam), GenericParam(GenericParam),
ImplSelfType(Impl), ImplSelfType(Impl),
AdtSelfType(Adt), AdtSelfType(Adt),
Local(Local), Local(Local),

View file

@ -814,7 +814,7 @@ impl<'a> SemanticsScope<'a> {
} }
resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()), resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(it.into()),
resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()), resolver::ScopeDef::AdtSelfType(it) => ScopeDef::AdtSelfType(it.into()),
resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(TypeParam { id }), resolver::ScopeDef::GenericParam(id) => ScopeDef::GenericParam(id.into()),
resolver::ScopeDef::Local(pat_id) => { resolver::ScopeDef::Local(pat_id) => {
let parent = resolver.body_owner().unwrap().into(); let parent = resolver.body_owner().unwrap().into();
ScopeDef::Local(Local { parent, pat_id }) ScopeDef::Local(Local { parent, pat_id })

View file

@ -21,8 +21,9 @@ use crate::{
per_ns::PerNs, per_ns::PerNs,
visibility::{RawVisibility, Visibility}, visibility::{RawVisibility, Visibility},
AdtId, AssocContainerId, ConstId, ConstParamId, ContainerId, DefWithBodyId, EnumId, AdtId, AssocContainerId, ConstId, ConstParamId, ContainerId, DefWithBodyId, EnumId,
EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, EnumVariantId, FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, LifetimeParamId,
ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, VariantId, LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
TypeParamId, VariantId,
}; };
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
@ -484,7 +485,7 @@ pub enum ScopeDef {
PerNs(PerNs), PerNs(PerNs),
ImplSelfType(ImplId), ImplSelfType(ImplId),
AdtSelfType(AdtId), AdtSelfType(AdtId),
GenericParam(TypeParamId), GenericParam(GenericParamId),
Local(PatId), Local(PatId),
} }
@ -527,15 +528,21 @@ impl Scope {
Scope::LocalItemsScope(body) => body.item_scope.entries().for_each(|(name, def)| { Scope::LocalItemsScope(body) => body.item_scope.entries().for_each(|(name, def)| {
f(name.clone(), ScopeDef::PerNs(def)); f(name.clone(), ScopeDef::PerNs(def));
}), }),
Scope::GenericParams { params, def } => { &Scope::GenericParams { ref params, def: parent } => {
for (local_id, param) in params.types.iter() { for (local_id, param) in params.types.iter() {
if let Some(name) = &param.name { if let Some(ref name) = param.name {
f( let id = TypeParamId { local_id, parent };
name.clone(), f(name.clone(), ScopeDef::GenericParam(id.into()))
ScopeDef::GenericParam(TypeParamId { local_id, parent: *def }),
)
} }
} }
for (local_id, param) in params.consts.iter() {
let id = ConstParamId { local_id, parent };
f(param.name.clone(), ScopeDef::GenericParam(id.into()))
}
for (local_id, param) in params.lifetimes.iter() {
let id = LifetimeParamId { local_id, parent };
f(param.name.clone(), ScopeDef::GenericParam(id.into()))
}
} }
Scope::ImplDefScope(i) => { Scope::ImplDefScope(i) => {
f(name![Self], ScopeDef::ImplSelfType(*i)); f(name![Self], ScopeDef::ImplSelfType(*i));