Skip to content

Commit 8cd4e9f

Browse files
committed
Merge assoc_items_only and exclude_import_kinds into assoc_mode
1 parent 97b725e commit 8cd4e9f

File tree

5 files changed

+49
-143
lines changed

5 files changed

+49
-143
lines changed

crates/hir-def/src/import_map.rs

Lines changed: 26 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -286,22 +286,6 @@ fn fst_path(db: &dyn DefDatabase, path: &ImportPath) -> String {
286286
s
287287
}
288288

289-
#[derive(Debug, Eq, PartialEq, Hash)]
290-
pub enum ImportKind {
291-
Module,
292-
Function,
293-
Adt,
294-
EnumVariant,
295-
Const,
296-
Static,
297-
Trait,
298-
TraitAlias,
299-
TypeAlias,
300-
BuiltinType,
301-
AssociatedItem,
302-
Macro,
303-
}
304-
305289
/// A way to match import map contents against the search query.
306290
#[derive(Debug)]
307291
pub enum SearchMode {
@@ -314,15 +298,25 @@ pub enum SearchMode {
314298
Fuzzy,
315299
}
316300

301+
/// Three possible ways to search for the name in associated and/or other items.
302+
#[derive(Debug, Clone, Copy)]
303+
pub enum AssocSearchMode {
304+
/// Search for the name in both associated and other items.
305+
Include,
306+
/// Search for the name in other items only.
307+
Exclude,
308+
/// Search for the name in the associated items only.
309+
AssocItemsOnly,
310+
}
311+
317312
#[derive(Debug)]
318313
pub struct Query {
319314
query: String,
320315
lowercased: String,
321-
assoc_items_only: bool,
322316
search_mode: SearchMode,
317+
assoc_mode: AssocSearchMode,
323318
case_sensitive: bool,
324319
limit: usize,
325-
exclude_import_kinds: FxHashSet<ImportKind>,
326320
}
327321

328322
impl Query {
@@ -331,24 +325,23 @@ impl Query {
331325
Self {
332326
query,
333327
lowercased,
334-
assoc_items_only: false,
335328
search_mode: SearchMode::Contains,
329+
assoc_mode: AssocSearchMode::Include,
336330
case_sensitive: false,
337331
limit: usize::max_value(),
338-
exclude_import_kinds: FxHashSet::default(),
339332
}
340333
}
341334

342-
/// Matches only the entries that are associated items, ignoring the rest.
343-
pub fn assoc_items_only(self) -> Self {
344-
Self { assoc_items_only: true, ..self }
345-
}
346-
347335
/// Specifies the way to search for the entries using the query.
348336
pub fn search_mode(self, search_mode: SearchMode) -> Self {
349337
Self { search_mode, ..self }
350338
}
351339

340+
/// Specifies whether we want to include associated items in the result.
341+
pub fn assoc_search_mode(self, assoc_mode: AssocSearchMode) -> Self {
342+
Self { assoc_mode, ..self }
343+
}
344+
352345
/// Limits the returned number of items to `limit`.
353346
pub fn limit(self, limit: usize) -> Self {
354347
Self { limit, ..self }
@@ -359,25 +352,17 @@ impl Query {
359352
Self { case_sensitive: true, ..self }
360353
}
361354

362-
/// Do not include imports of the specified kind in the search results.
363-
pub fn exclude_import_kind(mut self, import_kind: ImportKind) -> Self {
364-
self.exclude_import_kinds.insert(import_kind);
365-
self
366-
}
367-
368355
fn import_matches(
369356
&self,
370357
db: &dyn DefDatabase,
371358
import: &ImportInfo,
372359
enforce_lowercase: bool,
373360
) -> bool {
374361
let _p = profile::span("import_map::Query::import_matches");
375-
if import.is_trait_assoc_item {
376-
if self.exclude_import_kinds.contains(&ImportKind::AssociatedItem) {
377-
return false;
378-
}
379-
} else if self.assoc_items_only {
380-
return false;
362+
match (import.is_trait_assoc_item, self.assoc_mode) {
363+
(true, AssocSearchMode::Exclude) => return false,
364+
(false, AssocSearchMode::AssocItemsOnly) => return false,
365+
_ => {}
381366
}
382367

383368
let mut input = import.path.segments.last().unwrap().display(db.upcast()).to_string();
@@ -458,10 +443,6 @@ pub fn search_dependencies(
458443
.take_while(|item| {
459444
common_importables_path_fst == fst_path(db, &import_map.map[item].path)
460445
})
461-
.filter(|&item| match item_import_kind(item) {
462-
Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind),
463-
None => true,
464-
})
465446
.filter(|item| {
466447
!query.case_sensitive // we've already checked the common importables path case-insensitively
467448
|| query.import_matches(db, &import_map.map[item], false)
@@ -476,22 +457,6 @@ pub fn search_dependencies(
476457
res
477458
}
478459

479-
fn item_import_kind(item: ItemInNs) -> Option<ImportKind> {
480-
Some(match item.as_module_def_id()? {
481-
ModuleDefId::ModuleId(_) => ImportKind::Module,
482-
ModuleDefId::FunctionId(_) => ImportKind::Function,
483-
ModuleDefId::AdtId(_) => ImportKind::Adt,
484-
ModuleDefId::EnumVariantId(_) => ImportKind::EnumVariant,
485-
ModuleDefId::ConstId(_) => ImportKind::Const,
486-
ModuleDefId::StaticId(_) => ImportKind::Static,
487-
ModuleDefId::TraitId(_) => ImportKind::Trait,
488-
ModuleDefId::TraitAliasId(_) => ImportKind::TraitAlias,
489-
ModuleDefId::TypeAliasId(_) => ImportKind::TypeAlias,
490-
ModuleDefId::BuiltinType(_) => ImportKind::BuiltinType,
491-
ModuleDefId::MacroId(_) => ImportKind::Macro,
492-
})
493-
}
494-
495460
#[cfg(test)]
496461
mod tests {
497462
use base_db::{fixture::WithFixture, SourceDatabase, Upcast};
@@ -888,7 +853,9 @@ mod tests {
888853
check_search(
889854
ra_fixture,
890855
"main",
891-
Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy).assoc_items_only(),
856+
Query::new("fmt".to_string())
857+
.search_mode(SearchMode::Fuzzy)
858+
.assoc_search_mode(AssocSearchMode::AssocItemsOnly),
892859
expect![[r#"
893860
dep::fmt::Display::FMT_CONST (a)
894861
dep::fmt::Display::format_function (a)
@@ -901,21 +868,11 @@ mod tests {
901868
"main",
902869
Query::new("fmt".to_string())
903870
.search_mode(SearchMode::Fuzzy)
904-
.exclude_import_kind(ImportKind::AssociatedItem),
871+
.assoc_search_mode(AssocSearchMode::Exclude),
905872
expect![[r#"
906873
dep::fmt (t)
907874
"#]],
908875
);
909-
910-
check_search(
911-
ra_fixture,
912-
"main",
913-
Query::new("fmt".to_string())
914-
.search_mode(SearchMode::Fuzzy)
915-
.assoc_items_only()
916-
.exclude_import_kind(ImportKind::AssociatedItem),
917-
expect![[r#""#]],
918-
);
919876
}
920877

921878
#[test]
@@ -1101,34 +1058,4 @@ mod tests {
11011058
"#]],
11021059
);
11031060
}
1104-
1105-
#[test]
1106-
fn search_exclusions() {
1107-
let ra_fixture = r#"
1108-
//- /main.rs crate:main deps:dep
1109-
//- /dep.rs crate:dep
1110-
1111-
pub struct fmt;
1112-
pub struct FMT;
1113-
"#;
1114-
1115-
check_search(
1116-
ra_fixture,
1117-
"main",
1118-
Query::new("FMT".to_string()),
1119-
expect![[r#"
1120-
dep::FMT (t)
1121-
dep::FMT (v)
1122-
dep::fmt (t)
1123-
dep::fmt (v)
1124-
"#]],
1125-
);
1126-
1127-
check_search(
1128-
ra_fixture,
1129-
"main",
1130-
Query::new("FMT".to_string()).exclude_import_kind(ImportKind::Adt),
1131-
expect![[r#""#]],
1132-
);
1133-
}
11341061
}

crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub(crate) fn replace_derive_with_manual_impl(
7373
&ctx.sema,
7474
current_crate,
7575
NameToImport::exact_case_sensitive(path.segments().last()?.to_string()),
76-
items_locator::AssocItemSearch::Exclude,
76+
items_locator::AssocSearchMode::Exclude,
7777
Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT.inner()),
7878
)
7979
.filter_map(|item| match item.as_module_def()? {

crates/ide-completion/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ pub fn resolve_completion_edits(
231231
&sema,
232232
current_crate,
233233
NameToImport::exact_case_sensitive(imported_name),
234-
items_locator::AssocItemSearch::Include,
234+
items_locator::AssocSearchMode::Include,
235235
Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT.inner()),
236236
);
237237
let import = items_with_name

crates/ide-db/src/imports/import_assets.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use syntax::{
1313

1414
use crate::{
1515
helpers::item_name,
16-
items_locator::{self, AssocItemSearch, DEFAULT_QUERY_SEARCH_LIMIT},
16+
items_locator::{self, AssocSearchMode, DEFAULT_QUERY_SEARCH_LIMIT},
1717
RootDatabase,
1818
};
1919

@@ -317,7 +317,7 @@ fn path_applicable_imports(
317317
// * improve the associated completion item matching and/or scoring to ensure no noisy completions appear
318318
//
319319
// see also an ignored test under FIXME comment in the qualify_path.rs module
320-
AssocItemSearch::Exclude,
320+
AssocSearchMode::Exclude,
321321
Some(DEFAULT_QUERY_SEARCH_LIMIT.inner()),
322322
)
323323
.filter_map(|item| {
@@ -334,7 +334,7 @@ fn path_applicable_imports(
334334
sema,
335335
current_crate,
336336
path_candidate.name.clone(),
337-
AssocItemSearch::Include,
337+
AssocSearchMode::Include,
338338
Some(DEFAULT_QUERY_SEARCH_LIMIT.inner()),
339339
)
340340
.filter_map(|item| {
@@ -483,7 +483,7 @@ fn trait_applicable_items(
483483
sema,
484484
current_crate,
485485
trait_candidate.assoc_item_name.clone(),
486-
AssocItemSearch::AssocItemsOnly,
486+
AssocSearchMode::AssocItemsOnly,
487487
Some(DEFAULT_QUERY_SEARCH_LIMIT.inner()),
488488
)
489489
.filter_map(|input| item_as_assoc(db, input))

crates/ide-db/src/items_locator.rs

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,22 @@
33
//! The main reason for this module to exist is the fact that project's items and dependencies' items
44
//! are located in different caches, with different APIs.
55
use either::Either;
6-
use hir::{
7-
import_map::{self, ImportKind},
8-
AsAssocItem, Crate, ItemInNs, Semantics,
9-
};
6+
use hir::{import_map, AsAssocItem, Crate, ItemInNs, Semantics};
107
use limit::Limit;
118

129
use crate::{imports::import_assets::NameToImport, symbol_index, RootDatabase};
1310

1411
/// A value to use, when uncertain which limit to pick.
1512
pub static DEFAULT_QUERY_SEARCH_LIMIT: Limit = Limit::new(40);
1613

17-
/// Three possible ways to search for the name in associated and/or other items.
18-
#[derive(Debug, Clone, Copy)]
19-
pub enum AssocItemSearch {
20-
/// Search for the name in both associated and other items.
21-
Include,
22-
/// Search for the name in other items only.
23-
Exclude,
24-
/// Search for the name in the associated items only.
25-
AssocItemsOnly,
26-
}
14+
pub use import_map::AssocSearchMode;
2715

2816
/// Searches for importable items with the given name in the crate and its dependencies.
2917
pub fn items_with_name<'a>(
3018
sema: &'a Semantics<'_, RootDatabase>,
3119
krate: Crate,
3220
name: NameToImport,
33-
assoc_item_search: AssocItemSearch,
21+
assoc_item_search: AssocSearchMode,
3422
limit: Option<usize>,
3523
) -> impl Iterator<Item = ItemInNs> + 'a {
3624
let _p = profile::span("items_with_name").detail(|| {
@@ -60,16 +48,8 @@ pub fn items_with_name<'a>(
6048
let mut local_query = symbol_index::Query::new(fuzzy_search_string.clone());
6149

6250
let mut external_query = import_map::Query::new(fuzzy_search_string.clone())
63-
.search_mode(import_map::SearchMode::Fuzzy);
64-
match assoc_item_search {
65-
AssocItemSearch::Include => {}
66-
AssocItemSearch::Exclude => {
67-
external_query = external_query.exclude_import_kind(ImportKind::AssociatedItem);
68-
}
69-
AssocItemSearch::AssocItemsOnly => {
70-
external_query = external_query.assoc_items_only();
71-
}
72-
}
51+
.search_mode(import_map::SearchMode::Fuzzy)
52+
.assoc_search_mode(assoc_item_search);
7353

7454
if fuzzy_search_string.to_lowercase() != fuzzy_search_string {
7555
local_query.case_sensitive();
@@ -91,13 +71,15 @@ pub fn items_with_name<'a>(
9171
fn find_items<'a>(
9272
sema: &'a Semantics<'_, RootDatabase>,
9373
krate: Crate,
94-
assoc_item_search: AssocItemSearch,
74+
assoc_item_search: AssocSearchMode,
9575
local_query: symbol_index::Query,
9676
external_query: import_map::Query,
9777
) -> impl Iterator<Item = ItemInNs> + 'a {
9878
let _p = profile::span("find_items");
9979
let db = sema.db;
10080

81+
// NOTE: `external_query` includes `assoc_item_search`, so we don't need to
82+
// filter on our own.
10183
let external_importables =
10284
krate.query_external_importables(db, external_query).map(|external_importable| {
10385
match external_importable {
@@ -110,18 +92,15 @@ fn find_items<'a>(
11092
let local_results = local_query
11193
.search(&symbol_index::crate_symbols(db, krate))
11294
.into_iter()
113-
.filter_map(|local_candidate| match local_candidate.def {
114-
hir::ModuleDef::Macro(macro_def) => Some(ItemInNs::Macros(macro_def)),
115-
def => Some(ItemInNs::from(def)),
95+
.filter(move |candidate| match assoc_item_search {
96+
AssocSearchMode::Include => true,
97+
AssocSearchMode::Exclude => candidate.def.as_assoc_item(db).is_none(),
98+
AssocSearchMode::AssocItemsOnly => candidate.def.as_assoc_item(db).is_some(),
99+
})
100+
.map(|local_candidate| match local_candidate.def {
101+
hir::ModuleDef::Macro(macro_def) => ItemInNs::Macros(macro_def),
102+
def => ItemInNs::from(def),
116103
});
117104

118-
external_importables.chain(local_results).filter(move |&item| match assoc_item_search {
119-
AssocItemSearch::Include => true,
120-
AssocItemSearch::Exclude => !is_assoc_item(item, sema.db),
121-
AssocItemSearch::AssocItemsOnly => is_assoc_item(item, sema.db),
122-
})
123-
}
124-
125-
fn is_assoc_item(item: ItemInNs, db: &RootDatabase) -> bool {
126-
item.as_module_def().and_then(|module_def| module_def.as_assoc_item(db)).is_some()
105+
external_importables.chain(local_results)
127106
}

0 commit comments

Comments
 (0)