aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/completions
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2021-02-28 08:32:15 +0000
committerKirill Bulatov <[email protected]>2021-03-08 21:59:20 +0000
commit89d410cef571f5fa7631b17e2fbe52a8f8f03990 (patch)
tree6c40354741dda2072ef08f1bab9920547277bdd0 /crates/ide_completion/src/completions
parent9482353fa8e1e88cb720a029b9bb6304819c7399 (diff)
Do not propose already imported imports
Diffstat (limited to 'crates/ide_completion/src/completions')
-rw-r--r--crates/ide_completion/src/completions/flyimport.rs23
1 files changed, 7 insertions, 16 deletions
diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs
index af49fdd26..d6adf70b1 100644
--- a/crates/ide_completion/src/completions/flyimport.rs
+++ b/crates/ide_completion/src/completions/flyimport.rs
@@ -48,12 +48,11 @@
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
51use hir::{AsAssocItem, ModPath, ScopeDef}; 51use hir::{AsAssocItem, ItemInNs, ModPath, ScopeDef};
52use ide_db::helpers::{ 52use ide_db::helpers::{
53 import_assets::{ImportAssets, ImportCandidate}, 53 import_assets::{ImportAssets, ImportCandidate},
54 insert_use::ImportScope, 54 insert_use::ImportScope,
55}; 55};
56use rustc_hash::FxHashSet;
57use syntax::{AstNode, SyntaxNode, T}; 56use syntax::{AstNode, SyntaxNode, T};
58 57
59use crate::{ 58use crate::{
@@ -92,19 +91,17 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
92 &ctx.sema, 91 &ctx.sema,
93 )?; 92 )?;
94 93
95 let scope_definitions = scope_definitions(ctx);
96 let mut all_mod_paths = import_assets 94 let mut all_mod_paths = import_assets
97 .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind) 95 .search_for_imports(&ctx.sema, ctx.config.insert_use.prefix_kind)
98 .into_iter() 96 .into_iter()
99 .map(|import| { 97 .map(|import| {
100 let proposed_def = match import.item_to_display() { 98 let proposed_def = match import.item_to_display() {
101 hir::ItemInNs::Types(id) => ScopeDef::ModuleDef(id.into()), 99 ItemInNs::Types(id) => ScopeDef::ModuleDef(id.into()),
102 hir::ItemInNs::Values(id) => ScopeDef::ModuleDef(id.into()), 100 ItemInNs::Values(id) => ScopeDef::ModuleDef(id.into()),
103 hir::ItemInNs::Macros(id) => ScopeDef::MacroDef(id.into()), 101 ItemInNs::Macros(id) => ScopeDef::MacroDef(id.into()),
104 }; 102 };
105 (import, proposed_def) 103 (import, proposed_def)
106 }) 104 })
107 .filter(|(_, proposed_def)| !scope_definitions.contains(proposed_def))
108 .collect::<Vec<_>>(); 105 .collect::<Vec<_>>();
109 all_mod_paths.sort_by_cached_key(|(import, _)| { 106 all_mod_paths.sort_by_cached_key(|(import, _)| {
110 compute_fuzzy_completion_order_key(import.display_path(), &user_input_lowercased) 107 compute_fuzzy_completion_order_key(import.display_path(), &user_input_lowercased)
@@ -125,14 +122,6 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
125 Some(()) 122 Some(())
126} 123}
127 124
128fn scope_definitions(ctx: &CompletionContext) -> FxHashSet<ScopeDef> {
129 let mut scope_definitions = FxHashSet::default();
130 ctx.scope.process_all_names(&mut |_, scope_def| {
131 scope_definitions.insert(scope_def);
132 });
133 scope_definitions
134}
135
136pub(crate) fn position_for_import<'a>( 125pub(crate) fn position_for_import<'a>(
137 ctx: &'a CompletionContext, 126 ctx: &'a CompletionContext,
138 import_candidate: Option<&ImportCandidate>, 127 import_candidate: Option<&ImportCandidate>,
@@ -150,13 +139,14 @@ pub(crate) fn position_for_import<'a>(
150 }) 139 })
151} 140}
152 141
153fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option<ImportAssets> { 142fn import_assets<'a>(ctx: &'a CompletionContext, fuzzy_name: String) -> Option<ImportAssets<'a>> {
154 let current_module = ctx.scope.module()?; 143 let current_module = ctx.scope.module()?;
155 if let Some(dot_receiver) = &ctx.dot_receiver { 144 if let Some(dot_receiver) = &ctx.dot_receiver {
156 ImportAssets::for_fuzzy_method_call( 145 ImportAssets::for_fuzzy_method_call(
157 current_module, 146 current_module,
158 ctx.sema.type_of_expr(dot_receiver)?, 147 ctx.sema.type_of_expr(dot_receiver)?,
159 fuzzy_name, 148 fuzzy_name,
149 ctx.scope.clone(),
160 ) 150 )
161 } else { 151 } else {
162 let fuzzy_name_length = fuzzy_name.len(); 152 let fuzzy_name_length = fuzzy_name.len();
@@ -165,6 +155,7 @@ fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option<ImportAs
165 ctx.path_qual.clone(), 155 ctx.path_qual.clone(),
166 fuzzy_name, 156 fuzzy_name,
167 &ctx.sema, 157 &ctx.sema,
158 ctx.scope.clone(),
168 )?; 159 )?;
169 160
170 if matches!(assets_for_path.import_candidate(), ImportCandidate::Path(_)) 161 if matches!(assets_for_path.import_candidate(), ImportCandidate::Path(_))