From 971b0836ef425d8b962ecac70a974887a8356567 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 31 May 2021 14:13:09 +0200 Subject: Use `Name`s instead of Strings in the completion rendering api --- crates/ide_completion/src/completions/lifetime.rs | 7 ++-- .../src/completions/macro_in_item_position.rs | 4 +-- crates/ide_completion/src/completions/pattern.rs | 2 +- .../src/completions/qualified_path.rs | 38 +++++++++++----------- .../src/completions/unqualified_path.rs | 8 ++--- 5 files changed, 29 insertions(+), 30 deletions(-) (limited to 'crates/ide_completion/src/completions') diff --git a/crates/ide_completion/src/completions/lifetime.rs b/crates/ide_completion/src/completions/lifetime.rs index 5f6285b84..8ccccb646 100644 --- a/crates/ide_completion/src/completions/lifetime.rs +++ b/crates/ide_completion/src/completions/lifetime.rs @@ -16,15 +16,14 @@ pub(crate) fn complete_lifetime(acc: &mut Completions, ctx: &CompletionContext) (Some(lt), Some(lp)) if lp == lt.clone() => return, (Some(_), Some(lp)) => { lp_string = lp.to_string(); - Some(&lp_string) + Some(&*lp_string) } _ => None, }; ctx.scope.process_all_names(&mut |name, res| { if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res { - let name = name.to_string(); - if param_lifetime != Some(&name) { + if param_lifetime != Some(&*name.to_string()) { acc.add_resolution(ctx, name, &res); } } @@ -41,7 +40,7 @@ pub(crate) fn complete_label(acc: &mut Completions, ctx: &CompletionContext) { } ctx.scope.process_all_names(&mut |name, res| { if let ScopeDef::Label(_) = res { - acc.add_resolution(ctx, name.to_string(), &res); + acc.add_resolution(ctx, name, &res); } }); } diff --git a/crates/ide_completion/src/completions/macro_in_item_position.rs b/crates/ide_completion/src/completions/macro_in_item_position.rs index ec57aee30..202e71215 100644 --- a/crates/ide_completion/src/completions/macro_in_item_position.rs +++ b/crates/ide_completion/src/completions/macro_in_item_position.rs @@ -11,11 +11,11 @@ pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &Compl ctx.scope.process_all_names(&mut |name, res| { if let hir::ScopeDef::MacroDef(mac) = res { - acc.add_macro(ctx, Some(name.to_string()), mac); + acc.add_macro(ctx, Some(name.clone()), mac); } // FIXME: This should be done in qualified_path/unqualified_path instead? if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res { - acc.add_resolution(ctx, name.to_string(), &res); + acc.add_resolution(ctx, name, &res); } }) } diff --git a/crates/ide_completion/src/completions/pattern.rs b/crates/ide_completion/src/completions/pattern.rs index b84e9a967..8a728c67e 100644 --- a/crates/ide_completion/src/completions/pattern.rs +++ b/crates/ide_completion/src/completions/pattern.rs @@ -51,7 +51,7 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) { _ => false, }; if add_resolution { - acc.add_resolution(ctx, name.to_string(), &res); + acc.add_resolution(ctx, name, &res); } }); } diff --git a/crates/ide_completion/src/completions/qualified_path.rs b/crates/ide_completion/src/completions/qualified_path.rs index 7a0e1ead3..de58ce1cd 100644 --- a/crates/ide_completion/src/completions/qualified_path.rs +++ b/crates/ide_completion/src/completions/qualified_path.rs @@ -1,6 +1,6 @@ //! Completion of paths, i.e. `some::prefix::$0`. -use hir::{Adt, HasVisibility, PathResolution, ScopeDef}; +use hir::HasVisibility; use rustc_hash::FxHashSet; use syntax::AstNode; @@ -21,14 +21,14 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon }; let context_module = ctx.scope.module(); if ctx.expects_assoc_item() { - if let PathResolution::Def(hir::ModuleDef::Module(module)) = resolution { + if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution { let module_scope = module.scope(ctx.db, context_module); for (name, def) in module_scope { - if let ScopeDef::MacroDef(macro_def) = def { - acc.add_macro(ctx, Some(name.to_string()), macro_def); + if let hir::ScopeDef::MacroDef(macro_def) = def { + acc.add_macro(ctx, Some(name.clone()), macro_def); } - if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def { - acc.add_resolution(ctx, name.to_string(), &def); + if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def { + acc.add_resolution(ctx, name, &def); } } } @@ -42,11 +42,11 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon }); match resolution { - PathResolution::Def(hir::ModuleDef::Module(module)) => { + hir::PathResolution::Def(hir::ModuleDef::Module(module)) => { let module_scope = module.scope(ctx.db, context_module); for (name, def) in module_scope { if ctx.use_item_syntax.is_some() { - if let ScopeDef::Unknown = def { + if let hir::ScopeDef::Unknown = def { if let Some(name_ref) = ctx.name_ref_syntax.as_ref() { if name_ref.syntax().text() == name.to_string().as_str() { // for `use self::foo$0`, don't suggest `foo` as a completion @@ -57,20 +57,20 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon } } - acc.add_resolution(ctx, name.to_string(), &def); + acc.add_resolution(ctx, name, &def); } } - PathResolution::Def(def @ hir::ModuleDef::Adt(_)) - | PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_)) - | PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => { - if let hir::ModuleDef::Adt(Adt::Enum(e)) = def { + hir::PathResolution::Def(def @ hir::ModuleDef::Adt(_)) + | hir::PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_)) + | hir::PathResolution::Def(def @ hir::ModuleDef::BuiltinType(_)) => { + if let hir::ModuleDef::Adt(hir::Adt::Enum(e)) = def { add_enum_variants(ctx, acc, e); } let ty = match def { hir::ModuleDef::Adt(adt) => adt.ty(ctx.db), hir::ModuleDef::TypeAlias(a) => { let ty = a.ty(ctx.db); - if let Some(Adt::Enum(e)) = ty.as_adt() { + if let Some(hir::Adt::Enum(e)) = ty.as_adt() { cov_mark::hit!(completes_variant_through_alias); add_enum_variants(ctx, acc, e); } @@ -117,7 +117,7 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon }); } } - PathResolution::Def(hir::ModuleDef::Trait(t)) => { + hir::PathResolution::Def(hir::ModuleDef::Trait(t)) => { // Handles `Trait::assoc` as well as `::assoc`. for item in t.items(ctx.db) { if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) { @@ -130,15 +130,15 @@ pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionCon } } } - PathResolution::TypeParam(_) | PathResolution::SelfType(_) => { + hir::PathResolution::TypeParam(_) | hir::PathResolution::SelfType(_) => { if let Some(krate) = ctx.krate { let ty = match resolution { - PathResolution::TypeParam(param) => param.ty(ctx.db), - PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db), + hir::PathResolution::TypeParam(param) => param.ty(ctx.db), + hir::PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db), _ => return, }; - if let Some(Adt::Enum(e)) = ty.as_adt() { + if let Some(hir::Adt::Enum(e)) = ty.as_adt() { add_enum_variants(ctx, acc, e); } diff --git a/crates/ide_completion/src/completions/unqualified_path.rs b/crates/ide_completion/src/completions/unqualified_path.rs index ede07f605..9db8516d0 100644 --- a/crates/ide_completion/src/completions/unqualified_path.rs +++ b/crates/ide_completion/src/completions/unqualified_path.rs @@ -14,10 +14,10 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC if ctx.expects_assoc_item() { ctx.scope.process_all_names(&mut |name, def| { if let ScopeDef::MacroDef(macro_def) = def { - acc.add_macro(ctx, Some(name.to_string()), macro_def); + acc.add_macro(ctx, Some(name.clone()), macro_def); } if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def { - acc.add_resolution(ctx, name.to_string(), &def); + acc.add_resolution(ctx, name, &def); } }); return; @@ -27,7 +27,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC cov_mark::hit!(only_completes_modules_in_import); ctx.scope.process_all_names(&mut |name, res| { if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res { - acc.add_resolution(ctx, name.to_string(), &res); + acc.add_resolution(ctx, name, &res); } }); return; @@ -45,7 +45,7 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC cov_mark::hit!(skip_lifetime_completion); return; } - acc.add_resolution(ctx, name.to_string(), &res); + acc.add_resolution(ctx, name, &res); }); } -- cgit v1.2.3