This commit is contained in:
Kirill Bulatov 2021-03-01 13:55:47 +02:00
parent e74c55bb4a
commit e214c3a6bd
2 changed files with 23 additions and 38 deletions

View file

@ -48,7 +48,7 @@
//! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding //! 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. //! capability enabled.
use hir::{AsAssocItem, ItemInNs, ModPath, ScopeDef}; use hir::{AsAssocItem, ModPath, ModuleDef, ScopeDef};
use ide_db::helpers::{ use ide_db::helpers::{
import_assets::{ImportAssets, ImportCandidate}, import_assets::{ImportAssets, ImportCandidate},
insert_use::ImportScope, insert_use::ImportScope,
@ -91,33 +91,26 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
&ctx.sema, &ctx.sema,
)?; )?;
let mut all_mod_paths = import_assets let mut all_imports =
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind) import_assets.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind);
.into_iter() all_imports.sort_by_cached_key(|import| {
.map(|import| {
let def_to_display = match import.item_to_display() {
ItemInNs::Types(id) => ScopeDef::ModuleDef(id.into()),
ItemInNs::Values(id) => ScopeDef::ModuleDef(id.into()),
ItemInNs::Macros(id) => ScopeDef::MacroDef(id.into()),
};
(import, def_to_display)
})
.collect::<Vec<_>>();
all_mod_paths.sort_by_cached_key(|(import, _)| {
compute_fuzzy_completion_order_key(import.display_path(), &user_input_lowercased) compute_fuzzy_completion_order_key(import.display_path(), &user_input_lowercased)
}); });
acc.add_all(all_mod_paths.into_iter().filter_map(|(import, def_to_display)| { acc.add_all(all_imports.into_iter().filter_map(|import| {
let import_for_trait_assoc_item = match def_to_display { let import_for_trait_assoc_item = import
ScopeDef::ModuleDef(module_def) => module_def .item_to_display()
.as_assoc_item(ctx.db) .as_module_def_id()
.and_then(|assoc| assoc.containing_trait(ctx.db)) .and_then(|module_def_id| {
.is_some(), ModuleDef::from(module_def_id).as_assoc_item(ctx.db)?.containing_trait(ctx.db)
_ => false, })
}; .is_some();
let import_edit = let def_to_display = ScopeDef::from(import.item_to_display());
ImportEdit { import, import_scope: import_scope.clone(), import_for_trait_assoc_item }; render_resolution_with_import(
render_resolution_with_import(RenderContext::new(ctx), import_edit, &def_to_display) RenderContext::new(ctx),
ImportEdit { import, import_scope: import_scope.clone(), import_for_trait_assoc_item },
&def_to_display,
)
})); }));
Some(()) Some(())
} }

View file

@ -130,7 +130,6 @@ impl<'a> ImportAssets<'a> {
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LocatedImport { pub struct LocatedImport {
// TODO kb extract both into a separate struct + add another field: `assoc_item_name: Optional<String|Name>`
import_path: ModPath, import_path: ModPath,
item_to_import: ItemInNs, item_to_import: ItemInNs,
data_to_display: Option<(ModPath, ItemInNs)>, data_to_display: Option<(ModPath, ItemInNs)>,
@ -146,7 +145,7 @@ impl LocatedImport {
} }
pub fn display_path(&self) -> &ModPath { pub fn display_path(&self) -> &ModPath {
self.data_to_display.as_ref().map(|(mod_pathh, _)| mod_pathh).unwrap_or(&self.import_path) self.data_to_display.as_ref().map(|(mod_path, _)| mod_path).unwrap_or(&self.import_path)
} }
pub fn import_path(&self) -> &ModPath { pub fn import_path(&self) -> &ModPath {
@ -227,14 +226,7 @@ impl<'a> ImportAssets<'a> {
self.applicable_defs(sema.db, prefixed, defs_for_candidate_name) self.applicable_defs(sema.db, prefixed, defs_for_candidate_name)
.into_iter() .into_iter()
.filter(|import| import.import_path().len() > 1) .filter(|import| import.import_path().len() > 1)
.filter(|import| { .filter(|import| !scope_definitions.contains(&ScopeDef::from(import.item_to_import())))
let proposed_def = match import.item_to_import() {
ItemInNs::Types(id) => ScopeDef::ModuleDef(id.into()),
ItemInNs::Values(id) => ScopeDef::ModuleDef(id.into()),
ItemInNs::Macros(id) => ScopeDef::MacroDef(id.into()),
};
!scope_definitions.contains(&proposed_def)
})
.collect() .collect()
} }
@ -314,8 +306,8 @@ fn import_for_item(
unresolved_qualifier: &str, unresolved_qualifier: &str,
original_item: ItemInNs, original_item: ItemInNs,
) -> Option<LocatedImport> { ) -> Option<LocatedImport> {
let (item_candidate, trait_to_import) = match original_item { let (item_candidate, trait_to_import) = match original_item.as_module_def_id() {
ItemInNs::Types(module_def_id) | ItemInNs::Values(module_def_id) => { Some(module_def_id) => {
match ModuleDef::from(module_def_id).as_assoc_item(db).map(|assoc| assoc.container(db)) match ModuleDef::from(module_def_id).as_assoc_item(db).map(|assoc| assoc.container(db))
{ {
Some(AssocItemContainer::Trait(trait_)) => { Some(AssocItemContainer::Trait(trait_)) => {
@ -328,7 +320,7 @@ fn import_for_item(
None => (original_item, None), None => (original_item, None),
} }
} }
ItemInNs::Macros(_) => (original_item, None), None => (original_item, None),
}; };
let import_path_candidate = mod_path(item_candidate)?; let import_path_candidate = mod_path(item_candidate)?;