Skip to content

Commit 09412d8

Browse files
Merge #8123
8123: Do not display unqualified assoc item completions r=SomeoneToIgnore a=SomeoneToIgnore Part of https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/autoimport.20weirdness Removes all flyimport completions for any unqualified associated type, effectively reverting #8095 I've explained the reasoning in the corresponding FIXME and open to discussions. As an alternative way, we could add yet another parameter in the method that's used by the `qualify_path` and enable it for the qualify assists only. Co-authored-by: Kirill Bulatov <[email protected]>
2 parents 2280f62 + 56a7d24 commit 09412d8

File tree

6 files changed

+250
-224
lines changed

6 files changed

+250
-224
lines changed

crates/ide_assists/src/handlers/qualify_path.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ fn main() {
536536
}
537537

538538
#[test]
539+
#[ignore = "FIXME: non-trait assoc items completion is unsupported yet, see FIXME in the import_assets.rs for more details"]
539540
fn associated_struct_const_unqualified() {
540541
check_assist(
541542
qualify_path,

crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use hir::ModuleDef;
2-
use ide_db::helpers::mod_path_to_ast;
2+
use ide_db::helpers::{import_assets::NameToImport, mod_path_to_ast};
33
use ide_db::items_locator;
44
use itertools::Itertools;
55
use syntax::{
@@ -65,20 +65,25 @@ pub(crate) fn replace_derive_with_manual_impl(
6565
let current_module = ctx.sema.scope(annotated_name.syntax()).module()?;
6666
let current_crate = current_module.krate();
6767

68-
let found_traits =
69-
items_locator::with_exact_name(&ctx.sema, current_crate, trait_token.text().to_string())
70-
.into_iter()
71-
.filter_map(|item| match ModuleDef::from(item.as_module_def_id()?) {
72-
ModuleDef::Trait(trait_) => Some(trait_),
73-
_ => None,
74-
})
75-
.flat_map(|trait_| {
76-
current_module
77-
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
78-
.as_ref()
79-
.map(mod_path_to_ast)
80-
.zip(Some(trait_))
81-
});
68+
let found_traits = items_locator::items_with_name(
69+
&ctx.sema,
70+
current_crate,
71+
NameToImport::Exact(trait_token.text().to_string()),
72+
items_locator::AssocItemSearch::Exclude,
73+
Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT),
74+
)
75+
.into_iter()
76+
.filter_map(|item| match ModuleDef::from(item.as_module_def_id()?) {
77+
ModuleDef::Trait(trait_) => Some(trait_),
78+
_ => None,
79+
})
80+
.flat_map(|trait_| {
81+
current_module
82+
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
83+
.as_ref()
84+
.map(mod_path_to_ast)
85+
.zip(Some(trait_))
86+
});
8287

8388
let mut no_traits_found = true;
8489
for (trait_path, trait_) in found_traits.inspect(|_| no_traits_found = false) {

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,38 @@ mod foo {
943943
944944
fn main() {
945945
bar::Ass$0
946+
}"#,
947+
expect![[]],
948+
)
949+
}
950+
951+
#[test]
952+
fn unqualified_assoc_items_are_omitted() {
953+
check(
954+
r#"
955+
mod something {
956+
pub trait BaseTrait {
957+
fn test_function() -> i32;
958+
}
959+
960+
pub struct Item1;
961+
pub struct Item2;
962+
963+
impl BaseTrait for Item1 {
964+
fn test_function() -> i32 {
965+
1
966+
}
967+
}
968+
969+
impl BaseTrait for Item2 {
970+
fn test_function() -> i32 {
971+
2
972+
}
973+
}
974+
}
975+
976+
fn main() {
977+
test_f$0
946978
}"#,
947979
expect![[]],
948980
)

crates/ide_completion/src/lib.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ mod completions;
1414
use completions::flyimport::position_for_import;
1515
use ide_db::{
1616
base_db::FilePosition,
17-
helpers::{import_assets::LocatedImport, insert_use::ImportScope},
17+
helpers::{
18+
import_assets::{LocatedImport, NameToImport},
19+
insert_use::ImportScope,
20+
},
1821
items_locator, RootDatabase,
1922
};
2023
use text_edit::TextEdit;
@@ -151,15 +154,20 @@ pub fn resolve_completion_edits(
151154
let current_module = ctx.sema.scope(position_for_import).module()?;
152155
let current_crate = current_module.krate();
153156

154-
let (import_path, item_to_import) =
155-
items_locator::with_exact_name(&ctx.sema, current_crate, imported_name)
156-
.into_iter()
157-
.filter_map(|candidate| {
158-
current_module
159-
.find_use_path_prefixed(db, candidate, config.insert_use.prefix_kind)
160-
.zip(Some(candidate))
161-
})
162-
.find(|(mod_path, _)| mod_path.to_string() == full_import_path)?;
157+
let (import_path, item_to_import) = items_locator::items_with_name(
158+
&ctx.sema,
159+
current_crate,
160+
NameToImport::Exact(imported_name),
161+
items_locator::AssocItemSearch::Include,
162+
Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT),
163+
)
164+
.into_iter()
165+
.filter_map(|candidate| {
166+
current_module
167+
.find_use_path_prefixed(db, candidate, config.insert_use.prefix_kind)
168+
.zip(Some(candidate))
169+
})
170+
.find(|(mod_path, _)| mod_path.to_string() == full_import_path)?;
163171
let import =
164172
LocatedImport::new(import_path.clone(), item_to_import, item_to_import, Some(import_path));
165173

0 commit comments

Comments
 (0)