From 38ef1fd4ad7fd26439201a1a4147a7d90a13601f Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sat, 14 Nov 2020 11:59:23 +0200 Subject: [PATCH] Better filter mod paths --- crates/assists/src/utils/import_assets.rs | 2 +- .../src/completions/unqualified_path.rs | 52 ++++++++++--------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/crates/assists/src/utils/import_assets.rs b/crates/assists/src/utils/import_assets.rs index 89e62ba79c..ff5c0e78ee 100644 --- a/crates/assists/src/utils/import_assets.rs +++ b/crates/assists/src/utils/import_assets.rs @@ -195,7 +195,7 @@ impl ImportAssets { } .map(|path| (path, item)) }) - .filter(|(use_path, _)| !use_path.segments.is_empty()) + .filter(|(use_path, _)| use_path.len() > 1) .take(20) .collect::>(); res.sort_by_key(|(path, _)| path.clone()); diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs index ecda37862e..7ce92a07bf 100644 --- a/crates/completion/src/completions/unqualified_path.rs +++ b/crates/completion/src/completions/unqualified_path.rs @@ -47,6 +47,30 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC fuzzy_completion(acc, ctx).unwrap_or_default() } +fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) { + if let Some(Adt::Enum(enum_data)) = ty.as_adt() { + let variants = enum_data.variants(ctx.db); + + let module = if let Some(module) = ctx.scope.module() { + // Compute path from the completion site if available. + module + } else { + // Otherwise fall back to the enum's definition site. + enum_data.module(ctx.db) + }; + + for variant in variants { + if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) { + // Variants with trivial paths are already added by the existing completion logic, + // so we should avoid adding these twice + if path.segments.len() > 1 { + acc.add_qualified_enum_variant(ctx, variant, path); + } + } + } + } +} + // TODO kb add a setting toggle for this feature? fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { let _p = profile::span("fuzzy_completion®"); @@ -71,6 +95,7 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<() ScopeDef::MacroDef(macro_def), )), }) + .filter(|(mod_path, _)| mod_path.len() > 1) .filter_map(|(mod_path, definition)| { let mut resolution_with_missing_import = render_resolution( RenderContext::new(ctx), @@ -89,36 +114,13 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<() resolution_with_missing_import.update_text_edit(text_edits.finish()); Some(resolution_with_missing_import) - }); + }) + .take(20); acc.add_all(possible_imports); Some(()) } -fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) { - if let Some(Adt::Enum(enum_data)) = ty.as_adt() { - let variants = enum_data.variants(ctx.db); - - let module = if let Some(module) = ctx.scope.module() { - // Compute path from the completion site if available. - module - } else { - // Otherwise fall back to the enum's definition site. - enum_data.module(ctx.db) - }; - - for variant in variants { - if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) { - // Variants with trivial paths are already added by the existing completion logic, - // so we should avoid adding these twice - if path.segments.len() > 1 { - acc.add_qualified_enum_variant(ctx, variant, path); - } - } - } - } -} - #[cfg(test)] mod tests { use expect_test::{expect, Expect};