Skip to content

Commit c395c33

Browse files
Filter out path items by the qualifier
1 parent f08c0cd commit c395c33

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed

crates/ide_db/src/helpers/import_assets.rs

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Look up accessible paths for items.
22
use either::Either;
33
use hir::{
4-
AsAssocItem, AssocItem, Crate, ItemInNs, MacroDef, ModPath, Module, ModuleDef, PathResolution,
5-
PrefixKind, Semantics,
4+
AsAssocItem, AssocItem, Crate, ItemInNs, MacroDef, ModPath, Module, ModuleDef, PrefixKind,
5+
Semantics,
66
};
77
use rustc_hash::FxHashSet;
88
use syntax::{ast, AstNode};
@@ -192,7 +192,7 @@ impl ImportAssets {
192192
let db = sema.db;
193193

194194
match &self.import_candidate {
195-
ImportCandidate::Path(path_candidate) => Box::new(path_applicable_defs(
195+
ImportCandidate::Path(path_candidate) => Box::new(path_applicable_items(
196196
sema,
197197
path_candidate,
198198
unfiltered_defs
@@ -223,37 +223,28 @@ impl ImportAssets {
223223
}
224224
}
225225

226-
fn path_applicable_defs<'a>(
226+
fn path_applicable_items<'a>(
227227
sema: &'a Semantics<RootDatabase>,
228228
path_candidate: &PathImportCandidate,
229229
unfiltered_defs: impl Iterator<Item = (ModPath, ItemInNs)> + 'a,
230-
) -> impl Iterator<Item = (ModPath, ItemInNs)> + 'a {
230+
) -> Box<dyn Iterator<Item = (ModPath, ItemInNs)> + 'a> {
231231
let unresolved_qualifier = match &path_candidate.unresolved_qualifier {
232232
Some(qualifier) => qualifier,
233233
None => {
234-
return unfiltered_defs;
234+
return Box::new(unfiltered_defs);
235235
}
236236
};
237237

238-
// TODO kb filter out items: found path should end with `qualifier::Name` or `qualifier::Something` for fuzzy search case.
238+
let qualifier_string = unresolved_qualifier.to_string();
239+
Box::new(unfiltered_defs.filter(move |(candidate_path, _)| {
240+
let mut candidate_qualifier = candidate_path.clone();
241+
candidate_qualifier.pop_segment();
239242

240-
// TODO kb find a way to turn a qualifier into the corresponding ModuleDef. Maybe through the unfiltered data?
241-
if let Some(qualifier_start_resolution) = resolve_qualifier_start(sema, unresolved_qualifier) {
242-
// TODO kb ascend until an unresolved segment part appears
243-
} else {
244-
// first segment is already unresolved, need to turn it into ModuleDef somehow
245-
}
246-
247-
return unfiltered_defs;
248-
}
249-
250-
fn resolve_qualifier_start(
251-
sema: &Semantics<RootDatabase>,
252-
qualifier: &ast::Path,
253-
) -> Option<PathResolution> {
254-
let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?;
255-
let qualifier_start_path = qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?;
256-
sema.resolve_path(&qualifier_start_path)
243+
// TODO kb
244+
// * take 1st segment of `unresolved_qualifier` and return it instead of the original `ItemInNs`
245+
// * Update `ModPath`: pop until 1st segment of `unresolved_qualifier` reached (do not rely on name comparison, nested mod names can repeat)
246+
candidate_qualifier.to_string().ends_with(&qualifier_string)
247+
}))
257248
}
258249

259250
fn trait_applicable_defs<'a>(

0 commit comments

Comments
 (0)