From 33c83e72b9b48177a6171fd06a26676679963a4d Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Wed, 3 Mar 2021 01:26:53 +0200 Subject: Work towards better import labels --- crates/ide_completion/src/completions/flyimport.rs | 101 +++++++++++---------- crates/ide_completion/src/item.rs | 43 +++++---- crates/ide_completion/src/lib.rs | 17 ++-- crates/ide_completion/src/render.rs | 14 +-- 4 files changed, 92 insertions(+), 83 deletions(-) (limited to 'crates/ide_completion/src') diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs index 1ef6f8afb..c1e3f091f 100644 --- a/crates/ide_completion/src/completions/flyimport.rs +++ b/crates/ide_completion/src/completions/flyimport.rs @@ -87,11 +87,12 @@ //! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding //! capability enabled. -use hir::{AsAssocItem, ModPath, ModuleDef, ScopeDef}; +use hir::ModPath; use ide_db::helpers::{ import_assets::{ImportAssets, ImportCandidate}, insert_use::ImportScope, }; +use itertools::Itertools; use syntax::{AstNode, SyntaxNode, T}; use crate::{ @@ -130,27 +131,23 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext) &ctx.sema, )?; - let mut all_imports = - import_assets.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind); - all_imports.sort_by_cached_key(|import| { - compute_fuzzy_completion_order_key(import.display_path(), &user_input_lowercased) - }); - - acc.add_all(all_imports.into_iter().filter_map(|import| { - let import_for_trait_assoc_item = import - .item_to_display() - .as_module_def_id() - .and_then(|module_def_id| { - ModuleDef::from(module_def_id).as_assoc_item(ctx.db)?.containing_trait(ctx.db) + acc.add_all( + import_assets + .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind) + .into_iter() + .sorted_by_key(|located_import| { + compute_fuzzy_completion_order_key( + &located_import.import_path, + &user_input_lowercased, + ) }) - .is_some(); - let def_to_display = ScopeDef::from(import.item_to_display()); - render_resolution_with_import( - RenderContext::new(ctx), - ImportEdit { import, import_scope: import_scope.clone(), import_for_trait_assoc_item }, - &def_to_display, - ) - })); + .filter_map(|import| { + render_resolution_with_import( + RenderContext::new(ctx), + ImportEdit { import, import_scope: import_scope.clone() }, + ) + }), + ); Some(()) } @@ -190,6 +187,7 @@ fn import_assets<'a>(ctx: &'a CompletionContext, fuzzy_name: String) -> Option, label: String, insert_text: Option, @@ -322,19 +322,22 @@ impl Builder { pub(crate) fn build(self) -> CompletionItem { let _p = profile::span("item::Builder::build"); - let mut label = self.label; - let mut lookup = self.lookup; - let mut insert_text = self.insert_text; - - if let Some(import_to_add) = self.import_to_add.as_ref() { - lookup = lookup.or_else(|| Some(label.clone())); - insert_text = insert_text.or_else(|| Some(label.clone())); - let display_path = import_to_add.import.display_path(); - if import_to_add.import_for_trait_assoc_item { - label = format!("{} ({})", label, display_path); - } else { - label = display_path.to_string(); - } + let label = self.label; + let lookup = self.lookup; + let insert_text = self.insert_text; + + if let Some(_import_to_add) = self.import_to_add.as_ref() { + todo!("todo kb") + // let import = &import_to_add.import; + // let item_to_import = import.item_to_import(); + // lookup = lookup.or_else(|| Some(label.clone())); + // insert_text = insert_text.or_else(|| Some(label.clone())); + // let display_path = import_to_add.import.display_path(); + // if import_to_add.import { + // label = format!("{} ({})", label, display_path); + // } else { + // label = display_path.to_string(); + // } } let text_edit = match self.text_edit { @@ -438,8 +441,8 @@ impl Builder { } } -impl<'a> Into for Builder { - fn into(self) -> CompletionItem { - self.build() - } -} +// impl<'a> Into for Builder { +// fn into(self) -> CompletionItem { +// self.build() +// } +// } diff --git a/crates/ide_completion/src/lib.rs b/crates/ide_completion/src/lib.rs index ca2e5e706..d19368de0 100644 --- a/crates/ide_completion/src/lib.rs +++ b/crates/ide_completion/src/lib.rs @@ -15,7 +15,7 @@ use completions::flyimport::position_for_import; use ide_db::{ base_db::FilePosition, helpers::{import_assets::LocatedImport, insert_use::ImportScope}, - imports_locator, RootDatabase, + items_locator, RootDatabase, }; use text_edit::TextEdit; @@ -141,7 +141,6 @@ pub fn resolve_completion_edits( position: FilePosition, full_import_path: &str, imported_name: String, - import_for_trait_assoc_item: bool, ) -> Option> { let ctx = CompletionContext::new(db, position, config)?; let position_for_import = position_for_import(&ctx, None)?; @@ -151,19 +150,17 @@ pub fn resolve_completion_edits( let current_crate = current_module.krate(); let (import_path, item_to_import) = - imports_locator::find_exact_imports(&ctx.sema, current_crate, imported_name) + items_locator::with_for_exact_name(&ctx.sema, current_crate, imported_name) + .into_iter() .filter_map(|candidate| { - let item: hir::ItemInNs = candidate.either(Into::into, Into::into); current_module - .find_use_path_prefixed(db, item, config.insert_use.prefix_kind) - .zip(Some(item)) + .find_use_path_prefixed(db, candidate, config.insert_use.prefix_kind) + .zip(Some(candidate)) }) .find(|(mod_path, _)| mod_path.to_string() == full_import_path)?; - let import = LocatedImport::new(import_path, item_to_import, None); + let import = LocatedImport::new(import_path, item_to_import, item_to_import); - ImportEdit { import_path, import_scope, import_for_trait_assoc_item } - .to_text_edit(config.insert_use) - .map(|edit| vec![edit]) + ImportEdit { import, import_scope }.to_text_edit(config.insert_use).map(|edit| vec![edit]) } #[cfg(test)] diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs index 4bddc3957..fae5685e2 100644 --- a/crates/ide_completion/src/render.rs +++ b/crates/ide_completion/src/render.rs @@ -53,18 +53,20 @@ pub(crate) fn render_resolution<'a>( pub(crate) fn render_resolution_with_import<'a>( ctx: RenderContext<'a>, import_edit: ImportEdit, - resolution: &ScopeDef, ) -> Option { + let resolution = ScopeDef::from(import_edit.import.original_item); let local_name = match resolution { ScopeDef::ModuleDef(ModuleDef::Function(f)) => f.name(ctx.completion.db).to_string(), ScopeDef::ModuleDef(ModuleDef::Const(c)) => c.name(ctx.completion.db)?.to_string(), ScopeDef::ModuleDef(ModuleDef::TypeAlias(t)) => t.name(ctx.completion.db).to_string(), - _ => item_name(ctx.db(), import_edit.import.item_to_display())?.to_string(), + _ => item_name(ctx.db(), import_edit.import.original_item)?.to_string(), }; - Render::new(ctx).render_resolution(local_name, Some(import_edit), resolution).map(|mut item| { - item.completion_kind = CompletionKind::Magic; - item - }) + Render::new(ctx).render_resolution(local_name, Some(import_edit), &resolution).map( + |mut item| { + item.completion_kind = CompletionKind::Magic; + item + }, + ) } /// Interface for data and methods required for items rendering. -- cgit v1.2.3