diff --git a/crates/agent_ui/src/model_search.rs b/crates/agent_ui/src/model_search.rs index 988b220048d..e478838084d 100644 --- a/crates/agent_ui/src/model_search.rs +++ b/crates/agent_ui/src/model_search.rs @@ -1,7 +1,7 @@ use std::cmp::Reverse; use collections::HashSet; -use fuzzy_nucleo::StringMatchCandidate; +use fuzzy_nucleo::{Case, StringMatchCandidate, StringMatchOptions}; use gpui::BackgroundExecutor; use ordered_float::OrderedFloat; @@ -41,24 +41,23 @@ async fn match_names_with_metadata( query: &str, executor: BackgroundExecutor, ) -> Vec { - let cancel_flag = Default::default(); let (mut name_matches, mut metadata_matches) = futures::join!( fuzzy_nucleo::match_strings_async( names, query, - fuzzy_nucleo::Case::Ignore, - fuzzy_nucleo::LengthPenalty::On, - names.len(), - &cancel_flag, + StringMatchOptions { + case: Case::Ignore, + ..Default::default() + }, executor.clone(), ), fuzzy_nucleo::match_strings_async( candidates, query, - fuzzy_nucleo::Case::Ignore, - fuzzy_nucleo::LengthPenalty::On, - candidates.len(), - &cancel_flag, + StringMatchOptions { + case: Case::Ignore, + ..Default::default() + }, executor, ), ); diff --git a/crates/agent_ui_benchmarks/benches/model_search.rs b/crates/agent_ui_benchmarks/benches/model_search.rs index ff039205510..419eab473c0 100644 --- a/crates/agent_ui_benchmarks/benches/model_search.rs +++ b/crates/agent_ui_benchmarks/benches/model_search.rs @@ -1,7 +1,7 @@ use std::{cmp::Reverse, fmt, sync::Arc, time::Duration}; use collections::HashSet; -use fuzzy_nucleo::{Case, LengthPenalty, StringMatch, StringMatchCandidate}; +use fuzzy_nucleo::{Case, StringMatch, StringMatchCandidate, StringMatchOptions}; use gpui::{BenchAppContext, SharedString}; use ordered_float::OrderedFloat; @@ -118,20 +118,12 @@ fn expected_values(options: &[OptionValue], query: &str) -> Vec { .enumerate() .map(|(index, option)| candidates(index, option)) .unzip(); - let mut names = fuzzy_nucleo::match_strings( - &names, - query, - Case::Ignore, - LengthPenalty::On, - options.len(), - ); - let mut metadata = fuzzy_nucleo::match_strings( - &metadata, - query, - Case::Ignore, - LengthPenalty::On, - options.len(), - ); + let match_options = StringMatchOptions { + case: Case::Ignore, + ..Default::default() + }; + let mut names = fuzzy_nucleo::match_strings(&names, query, match_options); + let mut metadata = fuzzy_nucleo::match_strings(&metadata, query, match_options); let sorting_key = |matched: &StringMatch| (Reverse(OrderedFloat(matched.score)), matched.candidate_id); names.sort_unstable_by_key(sorting_key); diff --git a/crates/command_palette/src/command_palette.rs b/crates/command_palette/src/command_palette.rs index 4d4117755d6..f2b4c5851c1 100644 --- a/crates/command_palette/src/command_palette.rs +++ b/crates/command_palette/src/command_palette.rs @@ -618,10 +618,10 @@ impl PickerDelegate for CommandPaletteDelegate { let mut matches = fuzzy_nucleo::match_strings_async( &candidates, &query, - fuzzy_nucleo::Case::Smart, - fuzzy_nucleo::LengthPenalty::On, - 10000, - &Default::default(), + fuzzy_nucleo::StringMatchOptions { + max_results: fuzzy_nucleo::MaxResults::limit::<10_000>(), + ..Default::default() + }, executor, ) .await; diff --git a/crates/file_finder/src/file_finder.rs b/crates/file_finder/src/file_finder.rs index 16dd78c8405..15bf39a6cde 100644 --- a/crates/file_finder/src/file_finder.rs +++ b/crates/file_finder/src/file_finder.rs @@ -801,7 +801,6 @@ fn matching_history_items<'a>( ); let mut matching_history_paths = HashMap::default(); for (worktree, candidates) in history_items_by_worktrees { - let max_results = candidates.len() + 1; let worktree_root_name = worktree_name_by_id .as_ref() .and_then(|w| w.get(&worktree).cloned()); @@ -812,8 +811,7 @@ fn matching_history_items<'a>( worktree.to_usize(), worktree_root_name, query.path_query(), - fuzzy_nucleo::Case::Ignore, - max_results, + Default::default(), path_style, ) .into_iter() @@ -1358,9 +1356,11 @@ impl FileFinderDelegate { candidate_sets.as_slice(), query.path_query(), &relative_to, - fuzzy_nucleo::Case::Ignore, - 100, - &cancel_flag, + fuzzy_nucleo::PathMatchOptions { + max_results: fuzzy_nucleo::MaxResults::limit::<100>(), + cancel_flag: Some(&cancel_flag), + ..Default::default() + }, cx.background_executor().clone(), ) .await diff --git a/crates/fuzzy_nucleo/benches/match_benchmark.rs b/crates/fuzzy_nucleo/benches/match_benchmark.rs index da74af15f5a..c4ab2821d98 100644 --- a/crates/fuzzy_nucleo/benches/match_benchmark.rs +++ b/crates/fuzzy_nucleo/benches/match_benchmark.rs @@ -178,10 +178,11 @@ fn bench_string_matching(criterion: &mut Criterion) { foreground_executor.block_on(fuzzy_nucleo::match_strings_async( &candidates, query, - fuzzy_nucleo::Case::Ignore, - fuzzy_nucleo::LengthPenalty::On, - size, - &cancel, + fuzzy_nucleo::StringMatchOptions { + cancel_flag: Some(&cancel), + case: fuzzy_nucleo::Case::Ignore, + ..Default::default() + }, background_executor.clone(), )) }, @@ -304,8 +305,10 @@ fn bench_path_matching(criterion: &mut Criterion) { 0, None, query, - case, - size, + fuzzy_nucleo::PathMatchOptions { + case, + ..Default::default() + }, PathStyle::Unix, ) }, diff --git a/crates/fuzzy_nucleo/src/fuzzy_nucleo.rs b/crates/fuzzy_nucleo/src/fuzzy_nucleo.rs index 9080d102a2e..79cfe852d8e 100644 --- a/crates/fuzzy_nucleo/src/fuzzy_nucleo.rs +++ b/crates/fuzzy_nucleo/src/fuzzy_nucleo.rs @@ -2,16 +2,29 @@ mod matcher; mod paths; mod strings; +use std::{ + num::NonZeroUsize, + sync::atomic::{self, AtomicBool}, +}; + use fuzzy::CharBag; use nucleo::pattern::{AtomKind, CaseMatching, Normalization, Pattern}; pub use paths::{ - PathMatch, PathMatchCandidate, PathMatchCandidateSet, match_fixed_path_set, match_path_sets, + PathMatch, PathMatchCandidate, PathMatchCandidateSet, PathMatchOptions, match_fixed_path_set, + match_path_sets, +}; +pub use strings::{ + StringMatch, StringMatchCandidate, StringMatchOptions, match_strings, match_strings_async, }; -pub use strings::{StringMatch, StringMatchCandidate, match_strings, match_strings_async}; pub(crate) struct Cancelled; +#[inline] +pub(crate) fn is_cancelled(cancel_flag: Option<&AtomicBool>, ordering: atomic::Ordering) -> bool { + cancel_flag.is_some_and(|flag| flag.load(ordering)) +} + #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum Case { Smart, @@ -32,6 +45,29 @@ impl Case { } } +/// How many of the best matches to return. +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)] +pub enum MaxResults { + /// Every match. + #[default] + All, + /// At most this many. + Limit(NonZeroUsize), +} + +impl MaxResults { + pub const fn limit() -> Self { + Self::Limit(const { NonZeroUsize::new(LIMIT).expect("limit must be non-zero") }) + } + + pub(crate) fn resolve(self, candidate_count: usize) -> usize { + match self { + Self::All => candidate_count, + Self::Limit(limit) => limit.get().min(candidate_count), + } + } +} + #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum LengthPenalty { On, diff --git a/crates/fuzzy_nucleo/src/paths.rs b/crates/fuzzy_nucleo/src/paths.rs index 46c88f797d4..71d8c702380 100644 --- a/crates/fuzzy_nucleo/src/paths.rs +++ b/crates/fuzzy_nucleo/src/paths.rs @@ -14,7 +14,10 @@ use nucleo::pattern::Pattern; use fuzzy::CharBag; use crate::matcher::{self, LENGTH_PENALTY}; -use crate::{Cancelled, Case, Query, case_penalty, count_case_mismatches, positions_from_sorted}; +use crate::{ + Cancelled, Case, MaxResults, Query, case_penalty, count_case_mismatches, is_cancelled, + positions_from_sorted, +}; #[derive(Clone, Debug)] pub struct PathMatchCandidate<'a> { @@ -42,6 +45,27 @@ impl<'a> PathMatchCandidate<'a> { } } +/// Options for [`match_fixed_path_set`] and [`match_path_sets`]. +#[derive(Copy, Clone, Debug)] +pub struct PathMatchOptions<'a> { + /// Defaults to all matches. Prefer a limit when matching whole worktrees. + pub max_results: MaxResults, + /// Return nothing if this gets set while matching. + pub cancel_flag: Option<&'a AtomicBool>, + /// Defaults to [`Case::Ignore`]. + pub case: Case, +} + +impl Default for PathMatchOptions<'_> { + fn default() -> Self { + Self { + max_results: MaxResults::All, + cancel_flag: None, + case: Case::Ignore, + } + } +} + #[derive(Clone, Debug)] pub struct PathMatch { pub score: f64, @@ -157,7 +181,7 @@ fn path_match_helper<'a>( root_is_file: bool, relative_to: &Option>, path_style: PathStyle, - cancel_flag: &AtomicBool, + cancel_flag: Option<&AtomicBool>, ) -> Result<(), Cancelled> { let mut candidate_buf = if !path_prefix.is_empty() && !root_is_file { let mut s = path_prefix.display(path_style).to_string(); @@ -173,7 +197,7 @@ fn path_match_helper<'a>( for candidate in candidates { buf.clear(); matched_chars.clear(); - if cancel_flag.load(atomic::Ordering::Relaxed) { + if is_cancelled(cancel_flag, atomic::Ordering::Relaxed) { return Err(Cancelled); } @@ -240,10 +264,14 @@ pub fn match_fixed_path_set( worktree_id: usize, worktree_root_name: Option>, query: &str, - case: Case, - max_results: usize, + options: PathMatchOptions<'_>, path_style: PathStyle, ) -> Vec { + let PathMatchOptions { + max_results, + cancel_flag, + case, + } = options; let Some(query) = Query::build(query, case) else { return Vec::new(); }; @@ -252,13 +280,14 @@ pub fn match_fixed_path_set( config.set_match_paths(); let mut matcher = matcher::get_matcher(config); + let max_results = max_results.resolve(candidates.len()); let root_is_file = worktree_root_name.is_some() && candidates.iter().all(|c| c.path.is_empty()); let path_prefix = worktree_root_name.unwrap_or_else(|| RelPath::empty_arc()); let mut results = Vec::new(); - path_match_helper( + let outcome = path_match_helper( &mut matcher, &query, candidates.into_iter(), @@ -268,11 +297,13 @@ pub fn match_fixed_path_set( root_is_file, &None, path_style, - &AtomicBool::new(false), - ) - .ok(); - gpui_util::truncate_to_bottom_n_sorted_by(&mut results, max_results, &|a, b| b.cmp(a)); + cancel_flag, + ); matcher::return_matcher(matcher); + if outcome.is_err() || is_cancelled(cancel_flag, atomic::Ordering::Acquire) { + return Vec::new(); + } + gpui_util::truncate_to_bottom_n_sorted_by(&mut results, max_results, &|a, b| b.cmp(a)); results } @@ -280,11 +311,14 @@ pub async fn match_path_sets<'a, Set: PathMatchCandidateSet<'a>>( candidate_sets: &'a [Set], query: &str, relative_to: &Option>, - case: Case, - max_results: usize, - cancel_flag: &AtomicBool, + options: PathMatchOptions<'_>, executor: BackgroundExecutor, ) -> Vec { + let PathMatchOptions { + max_results, + cancel_flag, + case, + } = options; let path_count: usize = candidate_sets.iter().map(|s| s.len()).sum(); if path_count == 0 { return Vec::new(); @@ -302,10 +336,11 @@ pub async fn match_path_sets<'a, Set: PathMatchCandidateSet<'a>>( return Vec::new(); }; + let max_results = max_results.resolve(path_count); let num_cpus = executor.num_cpus().min(path_count); let segment_size = path_count.div_ceil(num_cpus); let mut segment_results = (0..num_cpus) - .map(|_| Vec::with_capacity(max_results)) + .map(|_| Vec::with_capacity(max_results.min(segment_size))) .collect::>(); let mut config = nucleo::Config::DEFAULT; config.set_match_paths(); @@ -361,7 +396,7 @@ pub async fn match_path_sets<'a, Set: PathMatchCandidateSet<'a>>( .await; matcher::return_matchers(matchers); - if cancel_flag.load(atomic::Ordering::Acquire) { + if is_cancelled(cancel_flag, atomic::Ordering::Acquire) { return Vec::new(); } @@ -369,3 +404,111 @@ pub async fn match_path_sets<'a, Set: PathMatchCandidateSet<'a>>( gpui_util::truncate_to_bottom_n_sorted_by(&mut results, max_results, &|a, b| b.cmp(a)); results } + +#[cfg(test)] +mod tests { + use super::*; + + struct TestCandidateSet { + candidates: Vec>, + } + + impl<'a> PathMatchCandidateSet<'a> for TestCandidateSet { + type Candidates = std::iter::Cloned>>; + + fn id(&self) -> usize { + 0 + } + + fn len(&self) -> usize { + self.candidates.len() + } + + fn root_is_file(&self) -> bool { + false + } + + fn prefix(&self) -> Arc { + RelPath::empty_arc() + } + + fn candidates(&'a self, start: usize) -> Self::Candidates { + self.candidates + .get(start..) + .unwrap_or_default() + .iter() + .cloned() + } + + fn path_style(&self) -> PathStyle { + PathStyle::Unix + } + } + + fn candidates(paths: &[&'static str]) -> Vec> { + paths + .iter() + .map(|&path| { + PathMatchCandidate::new(RelPath::from_unix_str(path).unwrap(), false, None) + }) + .collect() + } + + #[test] + fn test_fixed_path_set_cancellation() { + let paths = ["src/main.rs", "src/lib.rs"]; + let uncancelled = match_fixed_path_set( + candidates(&paths), + 0, + None, + "main", + Default::default(), + PathStyle::Unix, + ); + assert_eq!(uncancelled.len(), 1); + + let cancel = AtomicBool::new(true); + let cancelled = match_fixed_path_set( + candidates(&paths), + 0, + None, + "main", + PathMatchOptions { + cancel_flag: Some(&cancel), + ..Default::default() + }, + PathStyle::Unix, + ); + assert!(cancelled.is_empty()); + } + + #[gpui::test] + async fn test_path_sets_cancellation(executor: BackgroundExecutor) { + let candidate_sets = [TestCandidateSet { + candidates: candidates(&["src/main.rs", "src/lib.rs"]), + }]; + let uncancelled = match_path_sets( + &candidate_sets, + "main", + &None, + Default::default(), + executor.clone(), + ) + .await; + assert_eq!(uncancelled.len(), 1); + + let cancel = AtomicBool::new(true); + let cancelled = match_path_sets( + &candidate_sets, + "main", + &None, + PathMatchOptions { + cancel_flag: Some(&cancel), + ..Default::default() + }, + executor, + ) + .await; + assert!(cancelled.is_empty()); + } +} diff --git a/crates/fuzzy_nucleo/src/strings.rs b/crates/fuzzy_nucleo/src/strings.rs index 2486afb7ce0..99f1b6d5f0e 100644 --- a/crates/fuzzy_nucleo/src/strings.rs +++ b/crates/fuzzy_nucleo/src/strings.rs @@ -10,7 +10,8 @@ use gpui::{BackgroundExecutor, SharedString}; use nucleo::Utf32Str; use crate::{ - Cancelled, Case, LengthPenalty, Query, case_penalty, count_case_mismatches, + Cancelled, Case, LengthPenalty, MaxResults, Query, case_penalty, count_case_mismatches, + is_cancelled, matcher::{self, LENGTH_PENALTY}, positions_from_sorted, }; @@ -97,21 +98,49 @@ impl Ord for StringMatch { } } +/// Options for [`match_strings`] and [`match_strings_async`]. +#[derive(Copy, Clone, Debug)] +pub struct StringMatchOptions<'a> { + /// Defaults to all matches. + pub max_results: MaxResults, + /// Return nothing if this gets set while matching. Empty queries ignore it. + pub cancel_flag: Option<&'a AtomicBool>, + /// Defaults to [`Case::Smart`]. + pub case: Case, + /// Defaults to [`LengthPenalty::On`]. + pub length_penalty: LengthPenalty, +} + +impl Default for StringMatchOptions<'_> { + fn default() -> Self { + Self { + max_results: MaxResults::All, + cancel_flag: None, + case: Case::Smart, + length_penalty: LengthPenalty::On, + } + } +} + pub async fn match_strings_async( candidates: &[T], query: &str, - case: Case, - length_penalty: LengthPenalty, - max_results: usize, - cancel_flag: &AtomicBool, + options: StringMatchOptions<'_>, executor: BackgroundExecutor, ) -> Vec where T: Borrow + Sync, { - if candidates.is_empty() || max_results == 0 { + let StringMatchOptions { + max_results, + cancel_flag, + case, + length_penalty, + } = options; + if candidates.is_empty() { return Vec::new(); } + let max_results = max_results.resolve(candidates.len()); let Some(query) = Query::build(query, case) else { return empty_query_results(candidates, max_results); @@ -120,8 +149,9 @@ where let num_cpus = executor.num_cpus().min(candidates.len()); let base_size = candidates.len() / num_cpus; let remainder = candidates.len() % num_cpus; + let segment_capacity = max_results.min(candidates.len().div_ceil(num_cpus)); let mut segment_results = (0..num_cpus) - .map(|_| Vec::with_capacity(max_results.min(candidates.len()))) + .map(|_| Vec::with_capacity(segment_capacity)) .collect::>(); let config = nucleo::Config::DEFAULT; @@ -156,7 +186,7 @@ where matcher::return_matchers(matchers); - if cancel_flag.load(atomic::Ordering::Acquire) { + if is_cancelled(cancel_flag, atomic::Ordering::Acquire) { return Vec::new(); } @@ -168,16 +198,21 @@ where pub fn match_strings( candidates: &[T], query: &str, - case: Case, - length_penalty: LengthPenalty, - max_results: usize, + options: StringMatchOptions<'_>, ) -> Vec where T: Borrow, { - if candidates.is_empty() || max_results == 0 { + let StringMatchOptions { + max_results, + cancel_flag, + case, + length_penalty, + } = options; + if candidates.is_empty() { return Vec::new(); } + let max_results = max_results.resolve(candidates.len()); let Some(query) = Query::build(query, case) else { return empty_query_results(candidates, max_results); @@ -185,19 +220,21 @@ where let config = nucleo::Config::DEFAULT; let mut matcher = matcher::get_matcher(config); - let mut results = Vec::with_capacity(max_results.min(candidates.len())); + let mut results = Vec::with_capacity(max_results); - match_string_helper( + let outcome = match_string_helper( candidates, &query, &mut matcher, length_penalty, &mut results, - &AtomicBool::new(false), - ) - .ok(); + cancel_flag, + ); matcher::return_matcher(matcher); + if outcome.is_err() || is_cancelled(cancel_flag, atomic::Ordering::Acquire) { + return Vec::new(); + } gpui_util::truncate_to_bottom_n_sorted_by(&mut results, max_results, &|a, b| b.cmp(a)); results } @@ -227,7 +264,7 @@ fn match_string_helper( matcher: &mut nucleo::Matcher, length_penalty: LengthPenalty, results: &mut Vec, - cancel_flag: &AtomicBool, + cancel_flag: Option<&AtomicBool>, ) -> Result<(), Cancelled> where T: Borrow, @@ -239,7 +276,7 @@ where for candidate in candidates { buf.clear(); matched_chars.clear(); - if cancel_flag.load(atomic::Ordering::Relaxed) { + if is_cancelled(cancel_flag, atomic::Ordering::Relaxed) { return Err(Cancelled); } @@ -294,6 +331,15 @@ mod tests { use super::*; use gpui::BackgroundExecutor; + fn options() -> StringMatchOptions<'static> { + StringMatchOptions { + max_results: MaxResults::All, + cancel_flag: None, + case: Case::Ignore, + length_penalty: LengthPenalty::Off, + } + } + fn candidates(strings: &[&str]) -> Vec { strings .iter() @@ -305,17 +351,7 @@ mod tests { #[gpui::test] async fn test_basic_match(executor: BackgroundExecutor) { let cs = candidates(&["hello", "world", "help"]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "hel", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, "hel", options(), executor).await; let matched: Vec<&str> = results.iter().map(|m| m.string.as_ref()).collect(); assert!(matched.contains(&"hello")); assert!(matched.contains(&"help")); @@ -329,17 +365,7 @@ mod tests { "src/bin/main.rs", "tests/parser_test.rs", ]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "src parser", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, "src parser", options(), executor).await; assert_eq!(results.len(), 1); assert_eq!(results[0].string, "src/lib/parser.rs"); } @@ -347,17 +373,7 @@ mod tests { #[gpui::test] async fn test_empty_query_returns_all(executor: BackgroundExecutor) { let cs = candidates(&["alpha", "beta", "gamma"]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, "", options(), executor).await; assert_eq!(results.len(), 3); assert!(results.iter().all(|m| m.score == 0.0)); } @@ -365,34 +381,14 @@ mod tests { #[gpui::test] async fn test_whitespace_only_query_returns_all(executor: BackgroundExecutor) { let cs = candidates(&["alpha", "beta", "gamma"]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - " \t\n", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, " \t\n", options(), executor).await; assert_eq!(results.len(), 3); } #[gpui::test] async fn test_empty_candidates(executor: BackgroundExecutor) { let cs: Vec = vec![]; - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "query", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, "query", options(), executor).await; assert!(results.is_empty()); } @@ -403,10 +399,10 @@ mod tests { let results = match_strings_async( &cs, "hel", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, + StringMatchOptions { + cancel_flag: Some(&cancel), + ..options() + }, executor, ) .await; @@ -416,14 +412,13 @@ mod tests { #[gpui::test] async fn test_max_results_limit(executor: BackgroundExecutor) { let cs = candidates(&["ab", "abc", "abcd", "abcde"]); - let cancel = AtomicBool::new(false); let results = match_strings_async( &cs, "ab", - Case::Ignore, - LengthPenalty::Off, - 2, - &cancel, + StringMatchOptions { + max_results: MaxResults::limit::<2>(), + ..options() + }, executor, ) .await; @@ -437,17 +432,7 @@ mod tests { "fuzzy", "a_fuzzy_thing", ]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "fuzzy", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor.clone(), - ) - .await; + let results = match_strings_async(&cs, "fuzzy", options(), executor.clone()).await; let ordered = matches!( ( @@ -466,10 +451,10 @@ mod tests { let results_penalty = match_strings_async( &cs, "fuzzy", - Case::Ignore, - LengthPenalty::On, - 10, - &cancel, + StringMatchOptions { + length_penalty: LengthPenalty::On, + ..options() + }, executor, ) .await; @@ -480,17 +465,7 @@ mod tests { #[gpui::test] async fn test_utf8_positions(executor: BackgroundExecutor) { let cs = candidates(&["café"]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "caf", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, "caf", options(), executor).await; assert_eq!(results.len(), 1); let m = &results[0]; assert_eq!(m.positions, vec![0, 1, 2]); @@ -502,27 +477,18 @@ mod tests { #[gpui::test] async fn test_smart_case(executor: BackgroundExecutor) { let cs = candidates(&["FooBar", "foobar", "FOOBAR"]); - let cancel = AtomicBool::new(false); - let case_insensitive = match_strings_async( - &cs, - "foobar", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor.clone(), - ) - .await; + let case_insensitive = + match_strings_async(&cs, "foobar", options(), executor.clone()).await; assert_eq!(case_insensitive.len(), 3); let smart = match_strings_async( &cs, "FooBar", - Case::Smart, - LengthPenalty::Off, - 10, - &cancel, + StringMatchOptions { + case: Case::Smart, + ..options() + }, executor, ) .await; @@ -545,14 +511,14 @@ mod tests { "aaaaaaaaaaaaaaaaaaaaaaaaaaaa_FooBar", "aaaaaaaaaaaaaaaaaaaaaaaaaaaa_foobar", ]); - let cancel = AtomicBool::new(false); let results = match_strings_async( &cs, "FooBar", - Case::Smart, - LengthPenalty::On, - 10, - &cancel, + StringMatchOptions { + case: Case::Smart, + length_penalty: LengthPenalty::On, + ..options() + }, executor, ) .await; @@ -575,17 +541,7 @@ mod tests { #[gpui::test] async fn test_char_bag_prefilter(executor: BackgroundExecutor) { let cs = candidates(&["abcdef", "abc", "def", "aabbcc"]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "abc", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, "abc", options(), executor).await; let matched: Vec<&str> = results.iter().map(|m| m.string.as_ref()).collect(); assert!(matched.contains(&"abcdef")); assert!(matched.contains(&"abc")); @@ -596,7 +552,7 @@ mod tests { #[test] fn test_sync_basic_match() { let cs = candidates(&["hello", "world", "help"]); - let results = match_strings(&cs, "hel", Case::Ignore, LengthPenalty::Off, 10); + let results = match_strings(&cs, "hel", options()); let matched: Vec<&str> = results.iter().map(|m| m.string.as_ref()).collect(); assert!(matched.contains(&"hello")); assert!(matched.contains(&"help")); @@ -606,35 +562,86 @@ mod tests { #[test] fn test_sync_empty_query_returns_all() { let cs = candidates(&["alpha", "beta", "gamma"]); - let results = match_strings(&cs, "", Case::Ignore, LengthPenalty::Off, 10); + let results = match_strings(&cs, "", options()); assert_eq!(results.len(), 3); } #[test] fn test_sync_whitespace_only_query_returns_all() { let cs = candidates(&["alpha", "beta", "gamma"]); - let results = match_strings(&cs, " ", Case::Ignore, LengthPenalty::Off, 10); + let results = match_strings(&cs, " ", options()); assert_eq!(results.len(), 3); } #[test] fn test_sync_max_results() { let cs = candidates(&["ab", "abc", "abcd", "abcde"]); - let results = match_strings(&cs, "ab", Case::Ignore, LengthPenalty::Off, 2); + let results = match_strings( + &cs, + "ab", + StringMatchOptions { + max_results: MaxResults::limit::<2>(), + ..options() + }, + ); assert_eq!(results.len(), 2); } + #[test] + fn test_sync_cancellation() { + let cs = candidates(&["hello", "world"]); + let cancel = AtomicBool::new(true); + let results = match_strings( + &cs, + "hel", + StringMatchOptions { + cancel_flag: Some(&cancel), + ..options() + }, + ); + assert!(results.is_empty()); + } + + #[test] + fn test_sync_cancellation_during_last_candidate() { + struct CancellingCandidate<'a> { + candidate: StringMatchCandidate, + cancel: &'a AtomicBool, + } + + impl Borrow for CancellingCandidate<'_> { + fn borrow(&self) -> &StringMatchCandidate { + self.cancel.store(true, atomic::Ordering::Release); + &self.candidate + } + } + + let cancel = AtomicBool::new(false); + let cs = [CancellingCandidate { + candidate: StringMatchCandidate::new(0, "hello"), + cancel: &cancel, + }]; + let results = match_strings( + &cs, + "hel", + StringMatchOptions { + cancel_flag: Some(&cancel), + ..options() + }, + ); + assert!(results.is_empty()); + } + #[gpui::test] async fn test_empty_query_respects_max_results(executor: BackgroundExecutor) { let cs = candidates(&["alpha", "beta", "gamma", "delta"]); - let cancel = AtomicBool::new(false); let results = match_strings_async( &cs, "", - Case::Ignore, - LengthPenalty::Off, - 2, - &cancel, + StringMatchOptions { + max_results: MaxResults::limit::<2>(), + ..options() + }, executor, ) .await; @@ -644,17 +651,7 @@ mod tests { #[gpui::test] async fn test_multi_word_with_nonmatching_word(executor: BackgroundExecutor) { let cs = candidates(&["src/parser.rs", "src/main.rs"]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "src xyzzy", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, "src xyzzy", options(), executor).await; assert!( results.is_empty(), "no candidate contains 'xyzzy', so nothing should match" @@ -665,17 +662,7 @@ mod tests { async fn test_segment_size_not_divisible_by_cpus(executor: BackgroundExecutor) { executor.set_num_cpus(4); let cs = candidates(&["alpha", "beta", "gamma", "delta", "epsilon"]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "a", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, "a", options(), executor).await; let matched: Vec<&str> = results.iter().map(|m| m.string.as_ref()).collect(); assert!(matched.contains(&"alpha")); assert!(matched.contains(&"gamma")); @@ -686,17 +673,7 @@ mod tests { async fn test_segment_size_with_many_cpus_few_candidates(executor: BackgroundExecutor) { executor.set_num_cpus(16); let cs = candidates(&["one", "two", "three"]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "o", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, "o", options(), executor).await; let matched: Vec<&str> = results.iter().map(|m| m.string.as_ref()).collect(); assert!(matched.contains(&"one")); assert!(matched.contains(&"two")); @@ -706,17 +683,7 @@ mod tests { async fn test_segment_size_single_candidate(executor: BackgroundExecutor) { executor.set_num_cpus(8); let cs = candidates(&["lonely"]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "lone", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, "lone", options(), executor).await; assert_eq!(results.len(), 1); assert_eq!(results[0].string.as_ref(), "lonely"); } @@ -725,17 +692,7 @@ mod tests { async fn test_segment_size_candidates_equal_cpus(executor: BackgroundExecutor) { executor.set_num_cpus(4); let cs = candidates(&["aaa", "bbb", "ccc", "ddd"]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "a", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, "a", options(), executor).await; assert_eq!(results.len(), 1); assert_eq!(results[0].string.as_ref(), "aaa"); } @@ -744,17 +701,7 @@ mod tests { async fn test_segment_size_candidates_one_more_than_cpus(executor: BackgroundExecutor) { executor.set_num_cpus(3); let cs = candidates(&["ant", "ape", "dog", "axe"]); - let cancel = AtomicBool::new(false); - let results = match_strings_async( - &cs, - "a", - Case::Ignore, - LengthPenalty::Off, - 10, - &cancel, - executor, - ) - .await; + let results = match_strings_async(&cs, "a", options(), executor).await; let matched: Vec<&str> = results.iter().map(|m| m.string.as_ref()).collect(); assert!(matched.contains(&"ant")); assert!(matched.contains(&"ape")); diff --git a/crates/git_ui/src/branch_picker.rs b/crates/git_ui/src/branch_picker.rs index 832e672300d..6df10530e35 100644 --- a/crates/git_ui/src/branch_picker.rs +++ b/crates/git_ui/src/branch_picker.rs @@ -1497,10 +1497,10 @@ impl PickerDelegate for BranchListDelegate { let mut matches: Vec = fuzzy_nucleo::match_strings_async( &candidates, &query, - fuzzy_nucleo::Case::Smart, - fuzzy_nucleo::LengthPenalty::On, - 10000, - &Default::default(), + fuzzy_nucleo::StringMatchOptions { + max_results: fuzzy_nucleo::MaxResults::limit::<10_000>(), + ..Default::default() + }, cx.background_executor().clone(), ) .await diff --git a/crates/language/src/outline.rs b/crates/language/src/outline.rs index 536fd3b91c5..7075ca50169 100644 --- a/crates/language/src/outline.rs +++ b/crates/language/src/outline.rs @@ -1,5 +1,5 @@ use crate::{BufferSnapshot, Language, Point, ToPoint, ToTreeSitterPoint}; -use fuzzy_nucleo::{Case, LengthPenalty, StringMatch, StringMatchCandidate}; +use fuzzy_nucleo::{MaxResults, StringMatch, StringMatchCandidate, StringMatchOptions}; use gpui::{BackgroundExecutor, HighlightStyle, SharedString}; use std::{ops::Range, sync::Arc}; use theme::SyntaxTheme; @@ -215,10 +215,10 @@ impl Outline { let mut matches = fuzzy_nucleo::match_strings_async( &self.candidates, query, - Case::Smart, - LengthPenalty::On, - 100, - &Default::default(), + StringMatchOptions { + max_results: MaxResults::limit::<100>(), + ..Default::default() + }, executor, ) .await; diff --git a/crates/project_bookmarks/src/project_bookmarks.rs b/crates/project_bookmarks/src/project_bookmarks.rs index e86889760d3..aae62ae9f5a 100644 --- a/crates/project_bookmarks/src/project_bookmarks.rs +++ b/crates/project_bookmarks/src/project_bookmarks.rs @@ -1,7 +1,7 @@ use std::{collections::HashSet, sync::Arc}; use futures::future::{FutureExt, Shared}; -use fuzzy_nucleo::{Case, LengthPenalty, StringMatchCandidate, match_strings}; +use fuzzy_nucleo::{StringMatchCandidate, match_strings}; use gpui::{ App, Context, DismissEvent, Entity, ParentElement, SharedString, Task, WeakEntity, Window, actions, @@ -156,13 +156,7 @@ impl PickerDelegate for ProjectBookmarksDelegate { .map(|(i, bookmark)| StringMatchCandidate::new(i, &bookmark.label)) .collect(); - let matches = match_strings( - candidates.as_slice(), - &query, - Case::Smart, - LengthPenalty::On, - candidates.len(), - ); + let matches = match_strings(candidates.as_slice(), &query, Default::default()); picker .update(cx, |picker, cx| { diff --git a/crates/recent_projects/src/recent_projects.rs b/crates/recent_projects/src/recent_projects.rs index c3d1654a95e..b8eb2e3b989 100644 --- a/crates/recent_projects/src/recent_projects.rs +++ b/crates/recent_projects/src/recent_projects.rs @@ -22,7 +22,9 @@ pub use remote_connection::{RemoteConnectionModal, connect, connect_with_modal}; pub use remote_connections::{navigate_to_positions, open_remote_project}; use disconnected_overlay::DisconnectedOverlay; -use fuzzy_nucleo::{StringMatch, StringMatchCandidate, match_strings}; +use fuzzy_nucleo::{ + MaxResults, StringMatch, StringMatchCandidate, StringMatchOptions, match_strings, +}; use gpui::{ Action, AnyElement, App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, Subscription, Task, TaskExt, WeakEntity, Window, actions, px, @@ -970,7 +972,10 @@ impl PickerDelegate for RecentProjectsDelegate { cx: &mut Context>, ) -> gpui::Task<()> { let query = query.trim_start(); - let case = fuzzy_nucleo::Case::smart_if_uppercase_in(query); + let match_options = StringMatchOptions { + max_results: MaxResults::limit::<100>(), + ..Default::default() + }; let is_empty_query = query.is_empty(); let folder_matches = if self.open_folders.is_empty() { @@ -983,13 +988,7 @@ impl PickerDelegate for RecentProjectsDelegate { .map(|(id, folder)| StringMatchCandidate::new(id, folder.name.as_ref())) .collect(); - match_strings( - &candidates, - query, - case, - fuzzy_nucleo::LengthPenalty::On, - 100, - ) + match_strings(&candidates, query, match_options) }; let project_group_candidates: Vec<_> = self @@ -1007,13 +1006,7 @@ impl PickerDelegate for RecentProjectsDelegate { }) .collect(); - let project_group_matches = match_strings( - &project_group_candidates, - query, - case, - fuzzy_nucleo::LengthPenalty::On, - 100, - ); + let project_group_matches = match_strings(&project_group_candidates, query, match_options); // Build candidates for recent projects (not current, not sibling, not open folder) let recent_candidates: Vec<_> = self @@ -1032,13 +1025,7 @@ impl PickerDelegate for RecentProjectsDelegate { }) .collect(); - let recent_matches = match_strings( - &recent_candidates, - query, - case, - fuzzy_nucleo::LengthPenalty::On, - 100, - ); + let recent_matches = match_strings(&recent_candidates, query, match_options); let mut entries = Vec::new(); diff --git a/crates/recent_projects/src/remote_servers/filter.rs b/crates/recent_projects/src/remote_servers/filter.rs index 4472594292b..4da5633ffe3 100644 --- a/crates/recent_projects/src/remote_servers/filter.rs +++ b/crates/recent_projects/src/remote_servers/filter.rs @@ -201,15 +201,15 @@ fn build_server_result( } } +fn match_options() -> fuzzy_nucleo::StringMatchOptions<'static> { + fuzzy_nucleo::StringMatchOptions { + length_penalty: fuzzy_nucleo::LengthPenalty::Off, + ..Default::default() + } +} + pub(super) fn run_sync(data: &FilterData, query: &str) -> Vec { - let case = fuzzy_nucleo::Case::smart_if_uppercase_in(query); - let matches = fuzzy_nucleo::match_strings( - &data.candidates, - query, - case, - fuzzy_nucleo::LengthPenalty::Off, - data.candidates.len(), - ); + let matches = fuzzy_nucleo::match_strings(&data.candidates, query, match_options()); let mut results = build_filter_results(matches, data); results.sort_by(|a, b| b.score.total_cmp(&a.score)); results @@ -221,14 +221,13 @@ pub(super) async fn run_async( cancel: &AtomicBool, executor: BackgroundExecutor, ) -> Option> { - let case = fuzzy_nucleo::Case::smart_if_uppercase_in(query); let matches = fuzzy_nucleo::match_strings_async( &data.candidates, query, - case, - fuzzy_nucleo::LengthPenalty::Off, - data.candidates.len(), - cancel, + fuzzy_nucleo::StringMatchOptions { + cancel_flag: Some(cancel), + ..match_options() + }, executor, ) .await; diff --git a/crates/recent_projects/src/sidebar_recent_projects.rs b/crates/recent_projects/src/sidebar_recent_projects.rs index 0b93ee8aa59..0b124442706 100644 --- a/crates/recent_projects/src/sidebar_recent_projects.rs +++ b/crates/recent_projects/src/sidebar_recent_projects.rs @@ -1,6 +1,8 @@ use std::sync::Arc; -use fuzzy_nucleo::{StringMatch, StringMatchCandidate, match_strings}; +use fuzzy_nucleo::{ + MaxResults, StringMatch, StringMatchCandidate, StringMatchOptions, match_strings, +}; use gpui::{ Action, AnyElement, App, Context, DismissEvent, Entity, EventEmitter, FocusHandle, Focusable, Subscription, Task, TaskExt, WeakEntity, Window, @@ -167,7 +169,6 @@ impl PickerDelegate for SidebarRecentProjectsDelegate { cx: &mut Context>, ) -> Task<()> { let query = query.trim_start(); - let case = fuzzy_nucleo::Case::smart_if_uppercase_in(query); let is_empty_query = query.is_empty(); let current_workspace_id = self @@ -211,9 +212,10 @@ impl PickerDelegate for SidebarRecentProjectsDelegate { self.filtered_workspaces = match_strings( &candidates, query, - case, - fuzzy_nucleo::LengthPenalty::On, - 100, + StringMatchOptions { + max_results: MaxResults::limit::<100>(), + ..Default::default() + }, ); } diff --git a/crates/recent_projects/src/wsl_picker.rs b/crates/recent_projects/src/wsl_picker.rs index 271be7d0f51..9a826da0a49 100644 --- a/crates/recent_projects/src/wsl_picker.rs +++ b/crates/recent_projects/src/wsl_picker.rs @@ -125,13 +125,13 @@ impl picker::PickerDelegate for WslPickerDelegate { .collect::>(); let query = query.trim_start(); - let case = fuzzy_nucleo::Case::smart_if_uppercase_in(query); self.matches = fuzzy_nucleo::match_strings( &candidates, query, - case, - fuzzy_nucleo::LengthPenalty::On, - 100, + fuzzy_nucleo::StringMatchOptions { + max_results: fuzzy_nucleo::MaxResults::limit::<100>(), + ..Default::default() + }, ); self.matches.sort_unstable_by_key(|m| m.candidate_id); diff --git a/crates/tab_switcher/src/tab_switcher.rs b/crates/tab_switcher/src/tab_switcher.rs index aa921dde3d0..ecfedfe1297 100644 --- a/crates/tab_switcher/src/tab_switcher.rs +++ b/crates/tab_switcher/src/tab_switcher.rs @@ -461,9 +461,10 @@ impl TabSwitcherDelegate { fuzzy_nucleo::match_strings( &candidates, &query, - fuzzy_nucleo::Case::Smart, - fuzzy_nucleo::LengthPenalty::On, - 10000, + fuzzy_nucleo::StringMatchOptions { + max_results: fuzzy_nucleo::MaxResults::limit::<10_000>(), + ..Default::default() + }, ) .into_iter() .map(|m| all_items[m.candidate_id].clone())