mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 21:43:37 +00:00
Qualify autoimport completion suggestions
This commit is contained in:
parent
38ef1fd4ad
commit
bbe1fbd178
2 changed files with 42 additions and 15 deletions
|
@ -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());
|
||||
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)
|
||||
Some(
|
||||
resolution_with_missing_import
|
||||
.into_builder()
|
||||
.text_edit(text_edits.finish())
|
||||
.label(qualified_label)
|
||||
.lookup_by(lookup_string)
|
||||
.build(),
|
||||
)
|
||||
})
|
||||
.take(20);
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue