Skip to content

Commit 860628a

Browse files
committed
Remove SearchMode:Contains
Also micro-optimizes fuzzy search.
1 parent 8cd4e9f commit 860628a

File tree

2 files changed

+21
-46
lines changed

2 files changed

+21
-46
lines changed

crates/hir-def/src/import_map.rs

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,9 @@ fn fst_path(db: &dyn DefDatabase, path: &ImportPath) -> String {
288288

289289
/// A way to match import map contents against the search query.
290290
#[derive(Debug)]
291-
pub enum SearchMode {
291+
enum SearchMode {
292292
/// Import map entry should strictly match the query string.
293-
Equals,
294-
/// Import map entry should contain the query string.
295-
Contains,
293+
Exact,
296294
/// Import map entry should contain all letters from the query string,
297295
/// in the same order, but not necessary adjacent.
298296
Fuzzy,
@@ -325,16 +323,16 @@ impl Query {
325323
Self {
326324
query,
327325
lowercased,
328-
search_mode: SearchMode::Contains,
326+
search_mode: SearchMode::Exact,
329327
assoc_mode: AssocSearchMode::Include,
330328
case_sensitive: false,
331-
limit: usize::max_value(),
329+
limit: usize::MAX,
332330
}
333331
}
334332

335-
/// Specifies the way to search for the entries using the query.
336-
pub fn search_mode(self, search_mode: SearchMode) -> Self {
337-
Self { search_mode, ..self }
333+
/// Fuzzy finds items instead of exact matching.
334+
pub fn fuzzy(self) -> Self {
335+
Self { search_mode: SearchMode::Fuzzy, ..self }
338336
}
339337

340338
/// Specifies whether we want to include associated items in the result.
@@ -374,22 +372,15 @@ impl Query {
374372
let query_string = if case_insensitive { &self.lowercased } else { &self.query };
375373

376374
match self.search_mode {
377-
SearchMode::Equals => &input == query_string,
378-
SearchMode::Contains => input.contains(query_string),
375+
SearchMode::Exact => &input == query_string,
379376
SearchMode::Fuzzy => {
380-
let mut unchecked_query_chars = query_string.chars();
381-
let mut mismatching_query_char = unchecked_query_chars.next();
382-
383-
for input_char in input.chars() {
384-
match mismatching_query_char {
385-
None => return true,
386-
Some(matching_query_char) if matching_query_char == input_char => {
387-
mismatching_query_char = unchecked_query_chars.next();
388-
}
389-
_ => (),
377+
let mut input_chars = input.chars();
378+
for query_char in query_string.chars() {
379+
if input_chars.find(|&it| it == query_char).is_none() {
380+
return false;
390381
}
391382
}
392-
mismatching_query_char.is_none()
383+
true
393384
}
394385
}
395386
}
@@ -824,7 +815,7 @@ mod tests {
824815
check_search(
825816
ra_fixture,
826817
"main",
827-
Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
818+
Query::new("fmt".to_string()).fuzzy(),
828819
expect![[r#"
829820
dep::fmt (t)
830821
dep::fmt::Display::FMT_CONST (a)
@@ -854,7 +845,7 @@ mod tests {
854845
ra_fixture,
855846
"main",
856847
Query::new("fmt".to_string())
857-
.search_mode(SearchMode::Fuzzy)
848+
.fuzzy()
858849
.assoc_search_mode(AssocSearchMode::AssocItemsOnly),
859850
expect![[r#"
860851
dep::fmt::Display::FMT_CONST (a)
@@ -866,9 +857,7 @@ mod tests {
866857
check_search(
867858
ra_fixture,
868859
"main",
869-
Query::new("fmt".to_string())
870-
.search_mode(SearchMode::Fuzzy)
871-
.assoc_search_mode(AssocSearchMode::Exclude),
860+
Query::new("fmt".to_string()).fuzzy().assoc_search_mode(AssocSearchMode::Exclude),
872861
expect![[r#"
873862
dep::fmt (t)
874863
"#]],
@@ -904,7 +893,7 @@ mod tests {
904893
check_search(
905894
ra_fixture,
906895
"main",
907-
Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy),
896+
Query::new("fmt".to_string()).fuzzy(),
908897
expect![[r#"
909898
dep::Fmt (m)
910899
dep::Fmt (t)
@@ -918,20 +907,7 @@ mod tests {
918907
check_search(
919908
ra_fixture,
920909
"main",
921-
Query::new("fmt".to_string()).search_mode(SearchMode::Equals),
922-
expect![[r#"
923-
dep::Fmt (m)
924-
dep::Fmt (t)
925-
dep::Fmt (v)
926-
dep::fmt (t)
927-
dep::fmt::Display::fmt (a)
928-
"#]],
929-
);
930-
931-
check_search(
932-
ra_fixture,
933-
"main",
934-
Query::new("fmt".to_string()).search_mode(SearchMode::Contains),
910+
Query::new("fmt".to_string()),
935911
expect![[r#"
936912
dep::Fmt (m)
937913
dep::Fmt (t)
@@ -1049,7 +1025,7 @@ mod tests {
10491025
pub fn no() {}
10501026
"#,
10511027
"main",
1052-
Query::new("".to_string()).limit(2),
1028+
Query::new("".to_string()).fuzzy().limit(2),
10531029
expect![[r#"
10541030
dep::Fmt (m)
10551031
dep::Fmt (t)

crates/ide-db/src/items_locator.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ pub fn items_with_name<'a>(
3636
let mut local_query = symbol_index::Query::new(exact_name.clone());
3737
local_query.exact();
3838

39-
let external_query =
40-
import_map::Query::new(exact_name).search_mode(import_map::SearchMode::Equals);
39+
let external_query = import_map::Query::new(exact_name);
4140

4241
(
4342
local_query,
@@ -48,7 +47,7 @@ pub fn items_with_name<'a>(
4847
let mut local_query = symbol_index::Query::new(fuzzy_search_string.clone());
4948

5049
let mut external_query = import_map::Query::new(fuzzy_search_string.clone())
51-
.search_mode(import_map::SearchMode::Fuzzy)
50+
.fuzzy()
5251
.assoc_search_mode(assoc_item_search);
5352

5453
if fuzzy_search_string.to_lowercase() != fuzzy_search_string {

0 commit comments

Comments
 (0)