From bed12833cc4ce7c6b4085ba8dc47fc167c83f8ea Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 19 Jan 2021 20:01:49 +0100 Subject: Show const params in completions --- .../completion/src/completions/unqualified_path.rs | 24 ++++++++++++++++++++- crates/hir/src/code_model.rs | 2 +- crates/hir/src/semantics.rs | 2 +- crates/hir_def/src/resolver.rs | 25 ++++++++++++++-------- 4 files changed, 41 insertions(+), 12 deletions(-) (limited to 'crates') diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs index ac5596ca4..809e1645a 100644 --- a/crates/completion/src/completions/unqualified_path.rs +++ b/crates/completion/src/completions/unqualified_path.rs @@ -29,6 +29,10 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC } 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 let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) { 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() "#]], ); + check( + r#"fn quux() { $0 }"#, + expect![[r#" + tp C + fn quux() fn quux() + "#]], + ); + } + + #[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] diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 2950f08b8..6f48322db 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -2045,7 +2045,7 @@ impl Callable { pub enum ScopeDef { ModuleDef(ModuleDef), MacroDef(MacroDef), - GenericParam(TypeParam), + GenericParam(GenericParam), ImplSelfType(Impl), AdtSelfType(Adt), Local(Local), diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs index ab213e04c..0a30b4f5b 100644 --- a/crates/hir/src/semantics.rs +++ b/crates/hir/src/semantics.rs @@ -814,7 +814,7 @@ impl<'a> SemanticsScope<'a> { } resolver::ScopeDef::ImplSelfType(it) => ScopeDef::ImplSelfType(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) => { let parent = resolver.body_owner().unwrap().into(); ScopeDef::Local(Local { parent, pat_id }) diff --git a/crates/hir_def/src/resolver.rs b/crates/hir_def/src/resolver.rs index e7e92c72d..a505bf2be 100644 --- a/crates/hir_def/src/resolver.rs +++ b/crates/hir_def/src/resolver.rs @@ -21,8 +21,9 @@ use crate::{ per_ns::PerNs, visibility::{RawVisibility, Visibility}, AdtId, AssocContainerId, ConstId, ConstParamId, ContainerId, DefWithBodyId, EnumId, - EnumVariantId, FunctionId, GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, - ModuleId, StaticId, StructId, TraitId, TypeAliasId, TypeParamId, VariantId, + EnumVariantId, FunctionId, GenericDefId, GenericParamId, HasModule, ImplId, LifetimeParamId, + LocalModuleId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId, TypeAliasId, + TypeParamId, VariantId, }; #[derive(Debug, Clone, Default)] @@ -484,7 +485,7 @@ pub enum ScopeDef { PerNs(PerNs), ImplSelfType(ImplId), AdtSelfType(AdtId), - GenericParam(TypeParamId), + GenericParam(GenericParamId), Local(PatId), } @@ -527,15 +528,21 @@ impl Scope { Scope::LocalItemsScope(body) => body.item_scope.entries().for_each(|(name, 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() { - if let Some(name) = ¶m.name { - f( - name.clone(), - ScopeDef::GenericParam(TypeParamId { local_id, parent: *def }), - ) + if let Some(ref name) = param.name { + let id = TypeParamId { local_id, parent }; + f(name.clone(), ScopeDef::GenericParam(id.into())) } } + 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) => { f(name![Self], ScopeDef::ImplSelfType(*i)); -- cgit v1.2.3