From 7619c2afeaac09ced8a55b6f2db21550b80fba5e Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 2 Feb 2022 18:18:08 +0100 Subject: [PATCH] Simplify --- crates/ide_completion/src/completions.rs | 13 ++++++++++++ .../src/completions/attribute.rs | 12 ++--------- .../ide_completion/src/completions/pattern.rs | 20 ++++--------------- crates/ide_completion/src/completions/use_.rs | 9 ++------- crates/ide_completion/src/context.rs | 8 ++++---- 5 files changed, 25 insertions(+), 37 deletions(-) diff --git a/crates/ide_completion/src/completions.rs b/crates/ide_completion/src/completions.rs index be056a9b63b..9c65efdb102 100644 --- a/crates/ide_completion/src/completions.rs +++ b/crates/ide_completion/src/completions.rs @@ -99,6 +99,19 @@ impl Completions { item.add_to(self); } + pub(crate) fn add_nameref_keywords(&mut self, ctx: &CompletionContext) { + ["self::", "super::", "crate::"].into_iter().for_each(|kw| self.add_keyword(ctx, kw)); + } + + pub(crate) fn add_crate_roots(&mut self, ctx: &CompletionContext) { + ctx.process_all_names(&mut |name, res| match res { + ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => { + self.add_resolution(ctx, name, res); + } + _ => (), + }); + } + pub(crate) fn add_resolution( &mut self, ctx: &CompletionContext, diff --git a/crates/ide_completion/src/completions/attribute.rs b/crates/ide_completion/src/completions/attribute.rs index 3dd5a7ba223..cb45d9de03f 100644 --- a/crates/ide_completion/src/completions/attribute.rs +++ b/crates/ide_completion/src/completions/attribute.rs @@ -2,7 +2,6 @@ //! //! This module uses a bit of static metadata to provide completions for builtin-in attributes and lints. -use hir::ScopeDef; use ide_db::{ helpers::{ generated_lints::{ @@ -103,14 +102,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) return; } // fresh use tree with leading colon2, only show crate roots - None if is_absolute_path => { - ctx.process_all_names(&mut |name, res| match res { - ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => { - acc.add_resolution(ctx, name, res); - } - _ => (), - }); - } + None if is_absolute_path => acc.add_crate_roots(ctx), // only show modules in a fresh UseTree None => { ctx.process_all_names(&mut |name, def| { @@ -118,7 +110,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) acc.add_resolution(ctx, name, def); } }); - ["self::", "super::", "crate::"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw)); + acc.add_nameref_keywords(ctx); } } diff --git a/crates/ide_completion/src/completions/pattern.rs b/crates/ide_completion/src/completions/pattern.rs index 438230c58f1..f1b4fa72059 100644 --- a/crates/ide_completion/src/completions/pattern.rs +++ b/crates/ide_completion/src/completions/pattern.rs @@ -141,7 +141,6 @@ fn pattern_path_completion( _ => return, }; - // Note associated consts cannot be referenced in patterns if let Some(hir::Adt::Enum(e)) = ty.as_adt() { e.variants(ctx.db) .into_iter() @@ -157,9 +156,9 @@ fn pattern_path_completion( ctx.module, None, |_ty, item| { - // We might iterate candidates of a trait multiple times here, so deduplicate - // them. + // Note associated consts cannot be referenced in patterns if let AssocItem::TypeAlias(ta) = item { + // We might iterate candidates of a trait multiple times here, so deduplicate them. if seen.insert(item) { acc.add_type_alias(ctx, ta); } @@ -173,18 +172,7 @@ fn pattern_path_completion( } } // qualifier can only be none here if we are in a TuplePat or RecordPat in which case special characters have to follow the path - // so executing the rest of this completion doesn't make sense - // fresh use tree with leading colon2, only show crate roots - None if *is_absolute_path => { - cov_mark::hit!(use_tree_crate_roots_only); - ctx.process_all_names(&mut |name, res| match res { - ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => { - acc.add_resolution(ctx, name, res); - } - _ => (), - }); - } - // only show modules in a fresh UseTree + None if *is_absolute_path => acc.add_crate_roots(ctx), None => { cov_mark::hit!(unqualified_path_only_modules_in_import); ctx.process_all_names(&mut |name, res| { @@ -192,7 +180,7 @@ fn pattern_path_completion( acc.add_resolution(ctx, name, res); } }); - ["self::", "super::", "crate::"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw)); + acc.add_nameref_keywords(ctx); } } } diff --git a/crates/ide_completion/src/completions/use_.rs b/crates/ide_completion/src/completions/use_.rs index eac96c7cba0..6f980845c7d 100644 --- a/crates/ide_completion/src/completions/use_.rs +++ b/crates/ide_completion/src/completions/use_.rs @@ -79,12 +79,7 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) // fresh use tree with leading colon2, only show crate roots None if is_absolute_path => { cov_mark::hit!(use_tree_crate_roots_only); - ctx.process_all_names(&mut |name, res| match res { - ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => { - acc.add_resolution(ctx, name, res); - } - _ => (), - }); + acc.add_crate_roots(ctx); } // only show modules in a fresh UseTree None => { @@ -94,7 +89,7 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext) acc.add_resolution(ctx, name, res); } }); - ["self::", "super::", "crate::"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw)); + acc.add_nameref_keywords(ctx); } } } diff --git a/crates/ide_completion/src/context.rs b/crates/ide_completion/src/context.rs index 910652cba89..d711215491c 100644 --- a/crates/ide_completion/src/context.rs +++ b/crates/ide_completion/src/context.rs @@ -72,12 +72,12 @@ pub(crate) struct PathCompletionCtx { #[derive(Debug)] pub(crate) struct PathQualifierCtx { - pub path: ast::Path, - pub resolution: Option, + pub(crate) path: ast::Path, + pub(crate) resolution: Option, /// Whether this path consists solely of `super` segments - pub is_super_chain: bool, + pub(crate) is_super_chain: bool, /// Whether the qualifier comes from a use tree parent or not - pub use_tree_parent: bool, + pub(crate) use_tree_parent: bool, } #[derive(Debug)]