mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
ui-widgets: HelpEntry::with_tie_order orders entries that match a query equally, before the label's length, as production's result kind did
This commit is contained in:
@@ -27,6 +27,7 @@ pub struct HelpEntry {
|
||||
words: Option<String>,
|
||||
icon: Option<IconSpec<'static>>,
|
||||
ids: Vec<String>,
|
||||
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] {
|
||||
|
||||
@@ -78,6 +78,8 @@ struct Names {
|
||||
haystack: String,
|
||||
/// The initials of each label, of two or more letters.
|
||||
initials: Vec<String>,
|
||||
/// 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::<Vec<_>>().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<u32>, 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<u32>, 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<u32>, 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<usize> {
|
||||
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()
|
||||
|
||||
@@ -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 =
|
||||
|
||||
Reference in New Issue
Block a user