Skip to content

Commit e214c3a

Browse files
Simplify
1 parent e74c55b commit e214c3a

File tree

2 files changed

+23
-38
lines changed

2 files changed

+23
-38
lines changed

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
//! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding
4949
//! capability enabled.
5050
51-
use hir::{AsAssocItem, ItemInNs, ModPath, ScopeDef};
51+
use hir::{AsAssocItem, ModPath, ModuleDef, ScopeDef};
5252
use ide_db::helpers::{
5353
import_assets::{ImportAssets, ImportCandidate},
5454
insert_use::ImportScope,
@@ -91,33 +91,26 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
9191
&ctx.sema,
9292
)?;
9393

94-
let mut all_mod_paths = import_assets
95-
.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
96-
.into_iter()
97-
.map(|import| {
98-
let def_to_display = match import.item_to_display() {
99-
ItemInNs::Types(id) => ScopeDef::ModuleDef(id.into()),
100-
ItemInNs::Values(id) => ScopeDef::ModuleDef(id.into()),
101-
ItemInNs::Macros(id) => ScopeDef::MacroDef(id.into()),
102-
};
103-
(import, def_to_display)
104-
})
105-
.collect::<Vec<_>>();
106-
all_mod_paths.sort_by_cached_key(|(import, _)| {
94+
let mut all_imports =
95+
import_assets.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind);
96+
all_imports.sort_by_cached_key(|import| {
10797
compute_fuzzy_completion_order_key(import.display_path(), &user_input_lowercased)
10898
});
10999

110-
acc.add_all(all_mod_paths.into_iter().filter_map(|(import, def_to_display)| {
111-
let import_for_trait_assoc_item = match def_to_display {
112-
ScopeDef::ModuleDef(module_def) => module_def
113-
.as_assoc_item(ctx.db)
114-
.and_then(|assoc| assoc.containing_trait(ctx.db))
115-
.is_some(),
116-
_ => false,
117-
};
118-
let import_edit =
119-
ImportEdit { import, import_scope: import_scope.clone(), import_for_trait_assoc_item };
120-
render_resolution_with_import(RenderContext::new(ctx), import_edit, &def_to_display)
100+
acc.add_all(all_imports.into_iter().filter_map(|import| {
101+
let import_for_trait_assoc_item = import
102+
.item_to_display()
103+
.as_module_def_id()
104+
.and_then(|module_def_id| {
105+
ModuleDef::from(module_def_id).as_assoc_item(ctx.db)?.containing_trait(ctx.db)
106+
})
107+
.is_some();
108+
let def_to_display = ScopeDef::from(import.item_to_display());
109+
render_resolution_with_import(
110+
RenderContext::new(ctx),
111+
ImportEdit { import, import_scope: import_scope.clone(), import_for_trait_assoc_item },
112+
&def_to_display,
113+
)
121114
}));
122115
Some(())
123116
}

crates/ide_db/src/helpers/import_assets.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ impl<'a> ImportAssets<'a> {
130130

131131
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
132132
pub struct LocatedImport {
133-
// TODO kb extract both into a separate struct + add another field: `assoc_item_name: Optional<String|Name>`
134133
import_path: ModPath,
135134
item_to_import: ItemInNs,
136135
data_to_display: Option<(ModPath, ItemInNs)>,
@@ -146,7 +145,7 @@ impl LocatedImport {
146145
}
147146

148147
pub fn display_path(&self) -> &ModPath {
149-
self.data_to_display.as_ref().map(|(mod_pathh, _)| mod_pathh).unwrap_or(&self.import_path)
148+
self.data_to_display.as_ref().map(|(mod_path, _)| mod_path).unwrap_or(&self.import_path)
150149
}
151150

152151
pub fn import_path(&self) -> &ModPath {
@@ -227,14 +226,7 @@ impl<'a> ImportAssets<'a> {
227226
self.applicable_defs(sema.db, prefixed, defs_for_candidate_name)
228227
.into_iter()
229228
.filter(|import| import.import_path().len() > 1)
230-
.filter(|import| {
231-
let proposed_def = match import.item_to_import() {
232-
ItemInNs::Types(id) => ScopeDef::ModuleDef(id.into()),
233-
ItemInNs::Values(id) => ScopeDef::ModuleDef(id.into()),
234-
ItemInNs::Macros(id) => ScopeDef::MacroDef(id.into()),
235-
};
236-
!scope_definitions.contains(&proposed_def)
237-
})
229+
.filter(|import| !scope_definitions.contains(&ScopeDef::from(import.item_to_import())))
238230
.collect()
239231
}
240232

@@ -314,8 +306,8 @@ fn import_for_item(
314306
unresolved_qualifier: &str,
315307
original_item: ItemInNs,
316308
) -> Option<LocatedImport> {
317-
let (item_candidate, trait_to_import) = match original_item {
318-
ItemInNs::Types(module_def_id) | ItemInNs::Values(module_def_id) => {
309+
let (item_candidate, trait_to_import) = match original_item.as_module_def_id() {
310+
Some(module_def_id) => {
319311
match ModuleDef::from(module_def_id).as_assoc_item(db).map(|assoc| assoc.container(db))
320312
{
321313
Some(AssocItemContainer::Trait(trait_)) => {
@@ -328,7 +320,7 @@ fn import_for_item(
328320
None => (original_item, None),
329321
}
330322
}
331-
ItemInNs::Macros(_) => (original_item, None),
323+
None => (original_item, None),
332324
};
333325
let import_path_candidate = mod_path(item_candidate)?;
334326

0 commit comments

Comments
 (0)