Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
fuzzy_nucleo: Add a tail proximity bonus to fix the double hits not sorted higher bonus (#59224)
Finn Eitreim73a6e0ba619a
diff --git a/crates/file_finder/src/file_finder_tests.rs b/crates/file_finder/src/file_finder_tests.rsindex 8c313818ae2..08f99609454 100644--- a/crates/file_finder/src/file_finder_tests.rs+++ b/crates/file_finder/src/file_finder_tests.rs@@ -444,6 +444,52 @@ async fn test_complex_path(cx: &mut TestAppContext) {});}+#[gpui::test]+async fn test_tail_proximity_bonus_prefers_later_match(cx: &mut TestAppContext) {+ let app_state = init_test(cx);++ cx.update(|cx| {+ cx.update_global::<SettingsStore, _>(|store, cx| {+ store.update_user_settings(cx, |settings| {+ settings.project_panel.get_or_insert_default().hide_root = Some(true);+ });+ });+ });++ app_state+ .fs+ .as_fake()+ .insert_tree(+ path!("/root"),+ json!({+ "zzz": { "zed": { "Cargo.toml": "" } },+ "zed": { "zzz": { "Cargo.toml": "" } },+ }),+ )+ .await;++ let project = Project::test(app_state.fs.clone(), [path!("/root").as_ref()], cx).await;+ let (picker, _, cx) = build_find_picker(project, cx);++ picker+ .update_in(cx, |picker, window, cx| {+ picker+ .delegate+ .update_matches("zed cargo".to_string(), window, cx)+ })+ .await;++ picker.update(cx, |picker, _| {+ assert_eq!(+ collect_search_matches(picker).search_paths_only(),+ vec![+ rel_path("zzz/zed/Cargo.toml").into(),+ rel_path("zed/zzz/Cargo.toml").into(),+ ],+ );+ });+}+#[gpui::test]async fn test_row_column_numbers_query_inside_file(cx: &mut TestAppContext) {let app_state = init_test(cx);diff --git a/crates/fuzzy_nucleo/src/paths.rs b/crates/fuzzy_nucleo/src/paths.rsindex b0a0fa5e475..46c88f797d4 100644--- a/crates/fuzzy_nucleo/src/paths.rs+++ b/crates/fuzzy_nucleo/src/paths.rs@@ -134,6 +134,19 @@ fn get_filename_match_bonus(score as f64 / filename.len().max(1) as f64}+const TAIL_REWARD_SCALE: f64 = 1.5;++#[inline]+fn get_tail_proximity_bonus(matched_chars: &[u32], haystack_len: u32) -> f64 {+ debug_assert!(+ haystack_len != 0 && matched_chars.len() != 0,+ "This should only be called on a successful match."+ );+ let trailing: u32 = matched_chars.iter().map(|&i| haystack_len - 1 - i).sum();+ let mean_trailing = trailing as f64 / matched_chars.len() as f64;+ (1.0 / (1.0 + mean_trailing)) * TAIL_REWARD_SCALE+}+fn path_match_helper<'a>(matcher: &mut nucleo::Matcher,query: &Query,@@ -193,7 +206,9 @@ fn path_match_helper<'a>(let length_penalty = candidate_buf.len() as f64 * LENGTH_PENALTY;let filename_bonus = get_filename_match_bonus(&candidate_buf, &query.pattern, matcher);- let positive = (score as f64 + filename_bonus) * case_penalty(case_mismatches);+ let tail_proximity_bonus = get_tail_proximity_bonus(&matched_chars, haystack.len() as u32);+ let positive =+ (score as f64 + filename_bonus + tail_proximity_bonus) * case_penalty(case_mismatches);let adjusted_score = positive - length_penalty;let positions = positions_from_sorted(&candidate_buf, &matched_chars);