Skip to content

Commit 84c575a

Browse files
Restrict fuzzy qualifiers for now
1 parent 6ca6f10 commit 84c575a

File tree

3 files changed

+26
-47
lines changed

3 files changed

+26
-47
lines changed

crates/hir_def/src/import_map.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,27 +1094,4 @@ mod tests {
10941094
expect![[r#""#]],
10951095
);
10961096
}
1097-
1098-
#[test]
1099-
fn search_with_path() {
1100-
check_search(
1101-
r#"
1102-
//- /main.rs crate:main deps:dep
1103-
//- /dep.rs crate:dep
1104-
pub mod foo {
1105-
pub mod bar {
1106-
pub mod baz {
1107-
pub trait Display {
1108-
fn fmt();
1109-
}
1110-
}
1111-
}
1112-
}"#,
1113-
"main",
1114-
Query::new("baz::fmt".to_string()).search_mode(SearchMode::Fuzzy),
1115-
expect![[r#"
1116-
dep::foo::bar::baz::Display::fmt (a)
1117-
"#]],
1118-
);
1119-
}
11201097
}

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
//! ```
2222
//!
2323
//! Also completes associated items, that require trait imports.
24-
//! If any unresolved and/or partially-qualified path predeces the input, it will be taken into account: only the items with import string
25-
//! containing this whole path will be considered and the corresponding path import will be added:
24+
//! If any unresolved and/or partially-qualified path predeces the input, it will be taken into account.
25+
//! Currently, only the imports with their import path ending with the whole qialifier will be proposed
26+
//! (no fuzzy matching for qualifier).
2627
//!
2728
//! ```
2829
//! mod foo {
@@ -187,7 +188,6 @@ fn import_assets<'a>(ctx: &'a CompletionContext, fuzzy_name: String) -> Option<I
187188
ctx.scope.clone(),
188189
)?;
189190

190-
// TODO kb bad: with the path prefix, the "min 3 symbols" limit applies. Fix in a separate PR on the symbol_index level
191191
if matches!(assets_for_path.import_candidate(), ImportCandidate::Path(_))
192192
&& fuzzy_name_length < 2
193193
{
@@ -937,7 +937,6 @@ mod foo {
937937
}
938938
939939
fn main() {
940-
let zz = "sdsd";
941940
bar::Ass$0
942941
}"#,
943942
expect![[]],

crates/ide_db/src/helpers/import_assets.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -314,19 +314,21 @@ fn import_for_item(
314314
let import_path_candidate = mod_path(original_item_candidate)?;
315315
let import_path_string = import_path_candidate.to_string();
316316

317+
let expected_import_end = if item_as_assoc(db, original_item).is_some() {
318+
unresolved_qualifier.to_string()
319+
} else {
320+
format!("{}::{}", unresolved_qualifier, item_name(db, original_item)?)
321+
};
317322
if !import_path_string.contains(unresolved_first_segment)
318-
|| !import_path_string.contains(unresolved_qualifier)
323+
|| !import_path_string.ends_with(&expected_import_end)
319324
{
320325
return None;
321326
}
322327

323328
let segment_import =
324329
find_import_for_segment(db, original_item_candidate, &unresolved_first_segment)?;
325-
let trait_item_to_import = original_item
326-
.as_module_def_id()
327-
.and_then(|module_def_id| {
328-
ModuleDef::from(module_def_id).as_assoc_item(db)?.containing_trait(db)
329-
})
330+
let trait_item_to_import = item_as_assoc(db, original_item)
331+
.and_then(|assoc| assoc.containing_trait(db))
330332
.map(|trait_| ItemInNs::from(ModuleDef::from(trait_)));
331333
Some(match (segment_import == original_item_candidate, trait_item_to_import) {
332334
(true, Some(_)) => {
@@ -358,19 +360,15 @@ fn import_for_item(
358360

359361
fn item_for_path_search(db: &RootDatabase, item: ItemInNs) -> Option<ItemInNs> {
360362
Some(match item {
361-
ItemInNs::Types(module_def_id) | ItemInNs::Values(module_def_id) => {
362-
let module_def = ModuleDef::from(module_def_id);
363-
364-
match module_def.as_assoc_item(db) {
365-
Some(assoc_item) => match assoc_item.container(db) {
366-
AssocItemContainer::Trait(trait_) => ItemInNs::from(ModuleDef::from(trait_)),
367-
AssocItemContainer::Impl(impl_) => {
368-
ItemInNs::from(ModuleDef::from(impl_.target_ty(db).as_adt()?))
369-
}
370-
},
371-
None => item,
372-
}
373-
}
363+
ItemInNs::Types(_) | ItemInNs::Values(_) => match item_as_assoc(db, item) {
364+
Some(assoc_item) => match assoc_item.container(db) {
365+
AssocItemContainer::Trait(trait_) => ItemInNs::from(ModuleDef::from(trait_)),
366+
AssocItemContainer::Impl(impl_) => {
367+
ItemInNs::from(ModuleDef::from(impl_.target_ty(db).as_adt()?))
368+
}
369+
},
370+
None => item,
371+
},
374372
ItemInNs::Macros(_) => item,
375373
})
376374
}
@@ -427,7 +425,7 @@ fn trait_applicable_items(
427425

428426
let trait_candidates = items_with_candidate_name
429427
.into_iter()
430-
.filter_map(|input| ModuleDef::from(input.as_module_def_id()?).as_assoc_item(db))
428+
.filter_map(|input| item_as_assoc(db, input))
431429
.filter_map(|assoc| {
432430
let assoc_item_trait = assoc.containing_trait(db)?;
433431
required_assoc_items.insert(assoc);
@@ -583,3 +581,8 @@ fn path_import_candidate(
583581
None => ImportCandidate::Path(PathImportCandidate { qualifier: Qualifier::Absent, name }),
584582
})
585583
}
584+
585+
fn item_as_assoc(db: &RootDatabase, item: ItemInNs) -> Option<AssocItem> {
586+
item.as_module_def_id()
587+
.and_then(|module_def_id| ModuleDef::from(module_def_id).as_assoc_item(db))
588+
}

0 commit comments

Comments
 (0)