From bbe1fbd1786b416908d3c6bc34c8cf805b39b761 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sat, 14 Nov 2020 14:50:57 +0200 Subject: Qualify autoimport completion suggestions --- .../completion/src/completions/unqualified_path.rs | 35 ++++++++++++++-------- crates/completion/src/item.rs | 24 ++++++++++++--- 2 files changed, 43 insertions(+), 16 deletions(-) (limited to 'crates') diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs index 7ce92a07b..fca8d3a72 100644 --- a/crates/completion/src/completions/unqualified_path.rs +++ b/crates/completion/src/completions/unqualified_path.rs @@ -71,7 +71,6 @@ fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &T } } -// TODO kb add a setting toggle for this feature? fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { let _p = profile::span("fuzzy_completion®"); let current_module = ctx.scope.module()?; @@ -97,23 +96,35 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<() }) .filter(|(mod_path, _)| mod_path.len() > 1) .filter_map(|(mod_path, definition)| { - let mut resolution_with_missing_import = render_resolution( - RenderContext::new(ctx), - mod_path.segments.last()?.to_string(), - &definition, - )?; + let use_to_insert = mod_path_to_ast(&mod_path); + let mut mod_path_without_last_segment = mod_path; + let name_after_import = mod_path_without_last_segment.segments.pop()?.to_string(); + + let resolution_with_missing_import = + render_resolution(RenderContext::new(ctx), name_after_import, &definition)?; + let lookup_string = resolution_with_missing_import.lookup().to_owned(); let mut text_edits = resolution_with_missing_import.text_edit().to_owned().into_builder(); - - let rewriter = - insert_use(&import_scope, mod_path_to_ast(&mod_path), ctx.config.merge); + let rewriter = insert_use(&import_scope, use_to_insert, ctx.config.merge); let old_ast = rewriter.rewrite_root()?; algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut text_edits); - resolution_with_missing_import.update_text_edit(text_edits.finish()); - - Some(resolution_with_missing_import) + let qualifier_string = mod_path_without_last_segment.to_string(); + let qualified_label = if qualifier_string.is_empty() { + resolution_with_missing_import.label().to_owned() + } else { + format!("{}::{}", qualifier_string, resolution_with_missing_import.label()) + }; + + Some( + resolution_with_missing_import + .into_builder() + .text_edit(text_edits.finish()) + .label(qualified_label) + .lookup_by(lookup_string) + .build(), + ) }) .take(20); diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs index 53a12a763..24b9d036a 100644 --- a/crates/completion/src/item.rs +++ b/crates/completion/src/item.rs @@ -202,6 +202,26 @@ impl CompletionItem { ref_match: None, } } + + pub(crate) fn into_builder(self) -> Builder { + Builder { + source_range: self.source_range, + completion_kind: self.completion_kind, + label: self.label, + insert_text: None, + insert_text_format: self.insert_text_format, + detail: self.detail, + documentation: self.documentation, + lookup: self.lookup, + kind: self.kind, + text_edit: Some(self.text_edit), + deprecated: Some(self.deprecated), + trigger_call_info: Some(self.trigger_call_info), + score: self.score, + ref_match: self.ref_match, + } + } + /// What user sees in pop-up in the UI. pub fn label(&self) -> &str { &self.label @@ -218,10 +238,6 @@ impl CompletionItem { &self.text_edit } - pub fn update_text_edit(&mut self, new_text_edit: TextEdit) { - self.text_edit = new_text_edit; - } - /// Short one-line additional information, like a type pub fn detail(&self) -> Option<&str> { self.detail.as_deref() -- cgit v1.2.3