Remove SearchMode:Contains

Also micro-optimizes fuzzy search.
This commit is contained in:
Ryo Yoshida 2023-06-29 18:53:59 +09:00
parent 8cd4e9f7ec
commit 860628af7c
No known key found for this signature in database
GPG Key ID: E25698A930586171
2 changed files with 21 additions and 46 deletions

View File

@ -288,11 +288,9 @@ fn fst_path(db: &dyn DefDatabase, path: &ImportPath) -> String {
/// A way to match import map contents against the search query. /// A way to match import map contents against the search query.
#[derive(Debug)] #[derive(Debug)]
pub enum SearchMode { enum SearchMode {
/// Import map entry should strictly match the query string. /// Import map entry should strictly match the query string.
Equals, Exact,
/// Import map entry should contain the query string.
Contains,
/// Import map entry should contain all letters from the query string, /// Import map entry should contain all letters from the query string,
/// in the same order, but not necessary adjacent. /// in the same order, but not necessary adjacent.
Fuzzy, Fuzzy,
@ -325,16 +323,16 @@ impl Query {
Self { Self {
query, query,
lowercased, lowercased,
search_mode: SearchMode::Contains, search_mode: SearchMode::Exact,
assoc_mode: AssocSearchMode::Include, assoc_mode: AssocSearchMode::Include,
case_sensitive: false, case_sensitive: false,
limit: usize::max_value(), limit: usize::MAX,
} }
} }
/// Specifies the way to search for the entries using the query. /// Fuzzy finds items instead of exact matching.
pub fn search_mode(self, search_mode: SearchMode) -> Self { pub fn fuzzy(self) -> Self {
Self { search_mode, ..self } Self { search_mode: SearchMode::Fuzzy, ..self }
} }
/// Specifies whether we want to include associated items in the result. /// Specifies whether we want to include associated items in the result.
@ -374,22 +372,15 @@ impl Query {
let query_string = if case_insensitive { &self.lowercased } else { &self.query }; let query_string = if case_insensitive { &self.lowercased } else { &self.query };
match self.search_mode { match self.search_mode {
SearchMode::Equals => &input == query_string, SearchMode::Exact => &input == query_string,
SearchMode::Contains => input.contains(query_string),
SearchMode::Fuzzy => { SearchMode::Fuzzy => {
let mut unchecked_query_chars = query_string.chars(); let mut input_chars = input.chars();
let mut mismatching_query_char = unchecked_query_chars.next(); for query_char in query_string.chars() {
if input_chars.find(|&it| it == query_char).is_none() {
for input_char in input.chars() { return false;
match mismatching_query_char {
None => return true,
Some(matching_query_char) if matching_query_char == input_char => {
mismatching_query_char = unchecked_query_chars.next();
}
_ => (),
} }
} }
mismatching_query_char.is_none() true
} }
} }
} }
@ -824,7 +815,7 @@ mod tests {
check_search( check_search(
ra_fixture, ra_fixture,
"main", "main",
Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy), Query::new("fmt".to_string()).fuzzy(),
expect![[r#" expect![[r#"
dep::fmt (t) dep::fmt (t)
dep::fmt::Display::FMT_CONST (a) dep::fmt::Display::FMT_CONST (a)
@ -854,7 +845,7 @@ mod tests {
ra_fixture, ra_fixture,
"main", "main",
Query::new("fmt".to_string()) Query::new("fmt".to_string())
.search_mode(SearchMode::Fuzzy) .fuzzy()
.assoc_search_mode(AssocSearchMode::AssocItemsOnly), .assoc_search_mode(AssocSearchMode::AssocItemsOnly),
expect![[r#" expect![[r#"
dep::fmt::Display::FMT_CONST (a) dep::fmt::Display::FMT_CONST (a)
@ -866,9 +857,7 @@ mod tests {
check_search( check_search(
ra_fixture, ra_fixture,
"main", "main",
Query::new("fmt".to_string()) Query::new("fmt".to_string()).fuzzy().assoc_search_mode(AssocSearchMode::Exclude),
.search_mode(SearchMode::Fuzzy)
.assoc_search_mode(AssocSearchMode::Exclude),
expect![[r#" expect![[r#"
dep::fmt (t) dep::fmt (t)
"#]], "#]],
@ -904,7 +893,7 @@ mod tests {
check_search( check_search(
ra_fixture, ra_fixture,
"main", "main",
Query::new("fmt".to_string()).search_mode(SearchMode::Fuzzy), Query::new("fmt".to_string()).fuzzy(),
expect![[r#" expect![[r#"
dep::Fmt (m) dep::Fmt (m)
dep::Fmt (t) dep::Fmt (t)
@ -918,20 +907,7 @@ mod tests {
check_search( check_search(
ra_fixture, ra_fixture,
"main", "main",
Query::new("fmt".to_string()).search_mode(SearchMode::Equals), Query::new("fmt".to_string()),
expect![[r#"
dep::Fmt (m)
dep::Fmt (t)
dep::Fmt (v)
dep::fmt (t)
dep::fmt::Display::fmt (a)
"#]],
);
check_search(
ra_fixture,
"main",
Query::new("fmt".to_string()).search_mode(SearchMode::Contains),
expect![[r#" expect![[r#"
dep::Fmt (m) dep::Fmt (m)
dep::Fmt (t) dep::Fmt (t)
@ -1049,7 +1025,7 @@ mod tests {
pub fn no() {} pub fn no() {}
"#, "#,
"main", "main",
Query::new("".to_string()).limit(2), Query::new("".to_string()).fuzzy().limit(2),
expect![[r#" expect![[r#"
dep::Fmt (m) dep::Fmt (m)
dep::Fmt (t) dep::Fmt (t)

View File

@ -36,8 +36,7 @@ pub fn items_with_name<'a>(
let mut local_query = symbol_index::Query::new(exact_name.clone()); let mut local_query = symbol_index::Query::new(exact_name.clone());
local_query.exact(); local_query.exact();
let external_query = let external_query = import_map::Query::new(exact_name);
import_map::Query::new(exact_name).search_mode(import_map::SearchMode::Equals);
( (
local_query, local_query,
@ -48,7 +47,7 @@ pub fn items_with_name<'a>(
let mut local_query = symbol_index::Query::new(fuzzy_search_string.clone()); let mut local_query = symbol_index::Query::new(fuzzy_search_string.clone());
let mut external_query = import_map::Query::new(fuzzy_search_string.clone()) let mut external_query = import_map::Query::new(fuzzy_search_string.clone())
.search_mode(import_map::SearchMode::Fuzzy) .fuzzy()
.assoc_search_mode(assoc_item_search); .assoc_search_mode(assoc_item_search);
if fuzzy_search_string.to_lowercase() != fuzzy_search_string { if fuzzy_search_string.to_lowercase() != fuzzy_search_string {