diff options
-rw-r--r-- | crates/ide_completion/src/completions/flyimport.rs | 43 | ||||
-rw-r--r-- | crates/ide_db/src/helpers/import_assets.rs | 18 |
2 files changed, 23 insertions, 38 deletions
diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs index 55439d0e5..c6b83da3d 100644 --- a/crates/ide_completion/src/completions/flyimport.rs +++ b/crates/ide_completion/src/completions/flyimport.rs | |||
@@ -48,7 +48,7 @@ | |||
48 | //! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding | 48 | //! Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding |
49 | //! capability enabled. | 49 | //! capability enabled. |
50 | 50 | ||
51 | use hir::{AsAssocItem, ItemInNs, ModPath, ScopeDef}; | 51 | use hir::{AsAssocItem, ModPath, ModuleDef, ScopeDef}; |
52 | use ide_db::helpers::{ | 52 | use ide_db::helpers::{ |
53 | import_assets::{ImportAssets, ImportCandidate}, | 53 | import_assets::{ImportAssets, ImportCandidate}, |
54 | insert_use::ImportScope, | 54 | insert_use::ImportScope, |
@@ -91,33 +91,26 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext) | |||
91 | &ctx.sema, | 91 | &ctx.sema, |
92 | )?; | 92 | )?; |
93 | 93 | ||
94 | let mut all_mod_paths = import_assets | 94 | let mut all_imports = |
95 | .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind) | 95 | import_assets.search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind); |
96 | .into_iter() | 96 | all_imports.sort_by_cached_key(|import| { |
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, _)| { | ||
107 | compute_fuzzy_completion_order_key(import.display_path(), &user_input_lowercased) | 97 | compute_fuzzy_completion_order_key(import.display_path(), &user_input_lowercased) |
108 | }); | 98 | }); |
109 | 99 | ||
110 | acc.add_all(all_mod_paths.into_iter().filter_map(|(import, def_to_display)| { | 100 | acc.add_all(all_imports.into_iter().filter_map(|import| { |
111 | let import_for_trait_assoc_item = match def_to_display { | 101 | let import_for_trait_assoc_item = import |
112 | ScopeDef::ModuleDef(module_def) => module_def | 102 | .item_to_display() |
113 | .as_assoc_item(ctx.db) | 103 | .as_module_def_id() |
114 | .and_then(|assoc| assoc.containing_trait(ctx.db)) | 104 | .and_then(|module_def_id| { |
115 | .is_some(), | 105 | ModuleDef::from(module_def_id).as_assoc_item(ctx.db)?.containing_trait(ctx.db) |
116 | _ => false, | 106 | }) |
117 | }; | 107 | .is_some(); |
118 | let import_edit = | 108 | let def_to_display = ScopeDef::from(import.item_to_display()); |
119 | ImportEdit { import, import_scope: import_scope.clone(), import_for_trait_assoc_item }; | 109 | render_resolution_with_import( |
120 | render_resolution_with_import(RenderContext::new(ctx), import_edit, &def_to_display) | 110 | RenderContext::new(ctx), |
111 | ImportEdit { import, import_scope: import_scope.clone(), import_for_trait_assoc_item }, | ||
112 | &def_to_display, | ||
113 | ) | ||
121 | })); | 114 | })); |
122 | Some(()) | 115 | Some(()) |
123 | } | 116 | } |
diff --git a/crates/ide_db/src/helpers/import_assets.rs b/crates/ide_db/src/helpers/import_assets.rs index 2909ecd45..4e352d546 100644 --- a/crates/ide_db/src/helpers/import_assets.rs +++ b/crates/ide_db/src/helpers/import_assets.rs | |||
@@ -130,7 +130,6 @@ impl<'a> ImportAssets<'a> { | |||
130 | 130 | ||
131 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 131 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
132 | pub struct LocatedImport { | 132 | pub struct LocatedImport { |
133 | // TODO kb extract both into a separate struct + add another field: `assoc_item_name: Optional<String|Name>` | ||
134 | import_path: ModPath, | 133 | import_path: ModPath, |
135 | item_to_import: ItemInNs, | 134 | item_to_import: ItemInNs, |
136 | data_to_display: Option<(ModPath, ItemInNs)>, | 135 | data_to_display: Option<(ModPath, ItemInNs)>, |
@@ -146,7 +145,7 @@ impl LocatedImport { | |||
146 | } | 145 | } |
147 | 146 | ||
148 | pub fn display_path(&self) -> &ModPath { | 147 | 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) |
150 | } | 149 | } |
151 | 150 | ||
152 | pub fn import_path(&self) -> &ModPath { | 151 | pub fn import_path(&self) -> &ModPath { |
@@ -227,14 +226,7 @@ impl<'a> ImportAssets<'a> { | |||
227 | self.applicable_defs(sema.db, prefixed, defs_for_candidate_name) | 226 | self.applicable_defs(sema.db, prefixed, defs_for_candidate_name) |
228 | .into_iter() | 227 | .into_iter() |
229 | .filter(|import| import.import_path().len() > 1) | 228 | .filter(|import| import.import_path().len() > 1) |
230 | .filter(|import| { | 229 | .filter(|import| !scope_definitions.contains(&ScopeDef::from(import.item_to_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 | }) | ||
238 | .collect() | 230 | .collect() |
239 | } | 231 | } |
240 | 232 | ||
@@ -314,8 +306,8 @@ fn import_for_item( | |||
314 | unresolved_qualifier: &str, | 306 | unresolved_qualifier: &str, |
315 | original_item: ItemInNs, | 307 | original_item: ItemInNs, |
316 | ) -> Option<LocatedImport> { | 308 | ) -> Option<LocatedImport> { |
317 | let (item_candidate, trait_to_import) = match original_item { | 309 | let (item_candidate, trait_to_import) = match original_item.as_module_def_id() { |
318 | ItemInNs::Types(module_def_id) | ItemInNs::Values(module_def_id) => { | 310 | Some(module_def_id) => { |
319 | match ModuleDef::from(module_def_id).as_assoc_item(db).map(|assoc| assoc.container(db)) | 311 | match ModuleDef::from(module_def_id).as_assoc_item(db).map(|assoc| assoc.container(db)) |
320 | { | 312 | { |
321 | Some(AssocItemContainer::Trait(trait_)) => { | 313 | Some(AssocItemContainer::Trait(trait_)) => { |
@@ -328,7 +320,7 @@ fn import_for_item( | |||
328 | None => (original_item, None), | 320 | None => (original_item, None), |
329 | } | 321 | } |
330 | } | 322 | } |
331 | ItemInNs::Macros(_) => (original_item, None), | 323 | None => (original_item, None), |
332 | }; | 324 | }; |
333 | let import_path_candidate = mod_path(item_candidate)?; | 325 | let import_path_candidate = mod_path(item_candidate)?; |
334 | 326 | ||