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
//! capability enabled.
use hir::{AsAssocItem, ItemInNs, ModPath, ScopeDef};
use hir::{AsAssocItem, ModPath, ModuleDef, ScopeDef};
use ide_db::helpers::{
import_assets::{ImportAssets, ImportCandidate},
insert_use::ImportScope,
@ -91,33 +91,26 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
&ctx.sema,
)?;
let mut all_mod_paths = import_assets
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
.into_iter()
.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, _)| {
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_mod_paths.into_iter().filter_map(|(import, def_to_display)| {
let import_for_trait_assoc_item = match def_to_display {
ScopeDef::ModuleDef(module_def) => module_def
.as_assoc_item(ctx.db)
.and_then(|assoc| assoc.containing_trait(ctx.db))
.is_some(),
_ => false,
};
let import_edit =
ImportEdit { import, import_scope: import_scope.clone(), import_for_trait_assoc_item };
render_resolution_with_import(RenderContext::new(ctx), import_edit, &def_to_display)
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)
})
.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,
)
}));
Some(())
}

View File

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