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 +++++++++++---------- 1 file changed, 54 insertions(+), 47 deletions(-) (limited to 'crates/ide_completion/src/completions') 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