From d6b9b77de60b1215aeb2256c3f78f3b98dfbcc2f Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 10:11:21 +0700 Subject: [PATCH] ui-widgets: HelpEntry::with_tie_order orders entries that match a query equally, before the label's length, as production's result kind did --- crates/ui-widgets/src/menu/search.rs | 10 ++++++++++ crates/ui-widgets/src/menu/search/index.rs | 13 ++++++++----- crates/ui-widgets/tests/it/menu/search.rs | 10 ++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/crates/ui-widgets/src/menu/search.rs b/crates/ui-widgets/src/menu/search.rs index 1bd6aea23..f5eac59aa 100644 --- a/crates/ui-widgets/src/menu/search.rs +++ b/crates/ui-widgets/src/menu/search.rs @@ -27,6 +27,7 @@ pub struct HelpEntry { words: Option, icon: Option>, ids: Vec, + tie_order: u8, } impl HelpEntry { @@ -41,6 +42,7 @@ impl HelpEntry { words: None, icon: None, ids: Vec::new(), + tie_order: 0, } } @@ -98,6 +100,14 @@ impl HelpEntry { self } + /// The entry's place among entries that match a query as well and were picked as often and as recently, lower + /// first, before the label's length decides (an app's tools before its menu commands, say). 0 when not given. + #[must_use] + pub fn with_tie_order(mut self, order: u8) -> Self { + self.tie_order = order; + self + } + /// The ids searched in the ID and exact-id tiers. #[must_use] pub fn ids(&self) -> &[String] { diff --git a/crates/ui-widgets/src/menu/search/index.rs b/crates/ui-widgets/src/menu/search/index.rs index 88f41e84b..4e64dee2b 100644 --- a/crates/ui-widgets/src/menu/search/index.rs +++ b/crates/ui-widgets/src/menu/search/index.rs @@ -78,6 +78,8 @@ struct Names { haystack: String, /// The initials of each label, of two or more letters. initials: Vec, + /// The entry's [`HelpEntry::with_tie_order`]. + tie_order: u8, /// The characters of the shown label. label_len: usize, } @@ -320,7 +322,7 @@ impl SearchIndex { } let haystack = pairs.iter().map(|(_, full)| full.as_str()).chain(ids.iter().map(String::as_str)).collect::>().join("\n"); let label_len = pairs.first().map_or(0, |(label, _)| label.chars().count()); - names.push(Names { names: pairs, ids, haystack, initials, label_len }); + names.push(Names { names: pairs, ids, haystack, initials, tie_order: entry.tie_order, label_len }); } found.sort_unstable(); found.dedup(); @@ -524,8 +526,8 @@ impl SearchIndex { /// What entries are ordered by, compared field by field: whether a word matched only through a description, whether a /// word matched only through a typo, how the whole query matches, the worst grade a word gets, the sum of the grades, /// how the whole query matches when read with the words of the same meaning it matched through, how often the entry -/// was picked (higher grade first), the entry's place among the recent picks and the label's length. -type RankKey = (bool, bool, u8, u8, u32, u8, Reverse, usize, usize); +/// was picked (higher grade first), the entry's place among the recent picks, its tie order and the label's length. +type RankKey = (bool, bool, u8, u8, u32, u8, Reverse, usize, u8, usize); /// The entries of `index` matching `query`, best first, as indexes into the entries it was built from: tiers, grades /// from `uses` (`ilog2(min(count, 999) + 1)`), `recent` places and a substring fallback for long queries. It does not @@ -538,7 +540,8 @@ type RankKey = (bool, bool, u8, u8, u32, u8, Reverse, usize, usize); /// a word matched only through a description come after every other entry, and before them those where a word matched /// only through a typo. The rest are ordered by how the whole query matches, the worst grade a word gets, the sum of /// the grades, how the whole query matches when read through the words of the same meaning, the use grade of the -/// entry (highest first), its place in `recent`, the label's length and the entry's place. So an entry picked often, or +/// entry (highest first), its place in `recent`, its [`HelpEntry::with_tie_order`], the label's length and the entry's +/// place. So an entry picked often, or /// recently, ranks above the entries that match as well as it does and below every entry that matches better. pub fn rank(index: &SearchIndex, query: &str, recent: &[usize], uses: &dyn Fn(usize) -> u32) -> Vec { let query = normalize(&index.folder, query); @@ -679,7 +682,7 @@ pub fn rank(index: &SearchIndex, query: &str, recent: &[usize], uses: &dyn Fn(us let meant_tier = if synonym { best_reading(&query, &starts, &tokens, &meant, |read| index.phrase(e, read, false)) } else { tier }; let picked = recent.iter().position(|&r| r == i).unwrap_or(recent.len()); let used = Reverse((uses(i).min(999) + 1).ilog2()); - ranked.push(((described, typo, tier, worst, sum, meant_tier, used, picked, e.label_len), i)); + ranked.push(((described, typo, tier, worst, sum, meant_tier, used, picked, e.tie_order, e.label_len), i)); } ranked.sort_by_key(|&(key, place)| (key, place)); ranked.into_iter().map(|(_, place)| place).collect() diff --git a/crates/ui-widgets/tests/it/menu/search.rs b/crates/ui-widgets/tests/it/menu/search.rs index 3ba0b95e8..16e7da667 100644 --- a/crates/ui-widgets/tests/it/menu/search.rs +++ b/crates/ui-widgets/tests/it/menu/search.rs @@ -78,6 +78,16 @@ fn use_counts_lift_a_command_but_never_past_a_better_match() { assert_eq!(found, vec![3, 2, 0, 1]); } +/// An app's tie order (a tool before an effect) decides between entries that match as well, before the label's length, +/// and never lifts a worse match. +#[trial] +fn a_tie_order_puts_an_equal_match_first_but_never_past_a_better_one() { + let entries = + vec![entry("t0", "Rect Shape").with_tie_order(3), entry("t1", "Rect Tool Long").with_tie_order(0), entry("t2", "Rect").with_tie_order(5)]; + // The exact label first whatever its order, then the lower order though its label is longer. + assert_eq!(find(entries, "rect"), vec![2, 1, 0]); +} + #[trial] fn a_recent_place_lifts_an_entry_with_that_path() { let entries =