From 903478b7e8888f62a3685fcd2ce4227215463d27 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Wed, 25 Nov 2026 08:54:02 -0800 Subject: [PATCH] ui-widgets: command search counts typo edits only for label and path words, and finds an entry's postings by binary search SearchIndex::typos skips a word that no label or path holds before it counts the edits to the typed word. Before, it counted the edits first and checked the fields afterward. The words it returns do not change. SearchIndex::nearest_of and words_of find entry i's postings of a word with a binary search through a new holds, since build sorts each word's postings by entry. Before, they scanned all of the word's postings. The words they return do not change. Carried from storytold/vectorcraft#8208. --- crates/ui-widgets/src/menu/search/index.rs | 22 ++++++++--- crates/ui-widgets/tests/it/menu/search.rs | 43 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/crates/ui-widgets/src/menu/search/index.rs b/crates/ui-widgets/src/menu/search/index.rs index 4e64dee2b..924eea97a 100644 --- a/crates/ui-widgets/src/menu/search/index.rs +++ b/crates/ui-widgets/src/menu/search/index.rs @@ -454,14 +454,16 @@ impl SearchIndex { let mut term: Vec = Vec::new(); let mut near: Vec<(usize, usize)> = Vec::new(); for (t, (text, postings)) in self.terms.iter().enumerate() { - if text.chars().count().abs_diff(word.len()) > bound || text.chars().any(spaceless) { + // A word of no label or path is skipped before its edits are counted. + if text.chars().count().abs_diff(word.len()) > bound + || text.chars().any(spaceless) + || !postings.iter().any(|p| matches!(p.field, LABEL | PATH)) + { continue; } term.clear(); term.extend(text.chars()); - if let Some(edits) = edits_within(&word, &term, bound) - && postings.iter().any(|p| matches!(p.field, LABEL | PATH)) - { + if let Some(edits) = edits_within(&word, &term, bound) { near.push((edits, t)); } } @@ -488,14 +490,22 @@ impl SearchIndex { } } + /// Is the word at place `t` in `terms` a word of entry `i` in one of `fields`? A word's postings are sorted by entry + /// ([`SearchIndex::build`]), so a binary search finds entry `i`'s first one. + fn holds(&self, t: usize, i: usize, fields: &[u8]) -> bool { + let postings = self.postings_of(t); + let from = postings.partition_point(|p| p.entry < i); + postings.get(from..).unwrap_or_default().iter().take_while(|p| p.entry == i).any(|p| fields.contains(&p.field)) + } + /// The first word of `near` that is a word of entry `i` in one of `fields`. fn nearest_of(&self, near: &[usize], i: usize, fields: &[u8]) -> Option<&str> { - near.iter().find(|&&t| self.postings_of(t).iter().any(|p| p.entry == i && fields.contains(&p.field))).map(|&t| self.term(t)) + near.iter().find(|&&t| self.holds(t, i, fields)).map(|&t| self.term(t)) } /// The words of `near` that are words of entry `i` in one of `fields`, in the order of `near`. fn words_of(&self, near: &[usize], i: usize, fields: &[u8]) -> Vec<&str> { - near.iter().filter(|&&t| self.postings_of(t).iter().any(|p| p.entry == i && fields.contains(&p.field))).map(|&t| self.term(t)).collect() + near.iter().filter(|&&t| self.holds(t, i, fields)).map(|&t| self.term(t)).collect() } /// Is `query` (no spaces) the initials of one of entry `i`'s labels? diff --git a/crates/ui-widgets/tests/it/menu/search.rs b/crates/ui-widgets/tests/it/menu/search.rs index 16e7da667..a2d47ac38 100644 --- a/crates/ui-widgets/tests/it/menu/search.rs +++ b/crates/ui-widgets/tests/it/menu/search.rs @@ -57,6 +57,49 @@ fn stems_typos_and_a_substring_find_what_the_words_do_not() { assert!(find(entries(), "cot").is_empty(), "a typo of a short word finds nothing"); } +#[trial] +fn words_only_descriptions_hold_leave_room_for_the_label_word_a_typo_stands_for() { + // 50 description words are 1 edit from the query and the label word is 2 edits from it; only words of labels and + // paths are counted against the typo limit. + let last = ('a'..='z').filter(|&c| c != 'e').map(|c| format!("rectangl{c}")); + let first = ('a'..='z').filter(|&c| c != 'r').map(|c| format!("{c}ectangle")); + let near: Vec = last.chain(first).collect(); + let entries = vec![entry("t0", "Zip").with_words(near.join(" ")), entry("t1", "Rectanqqe")]; + assert_eq!(find(entries, "rectangle"), vec![1]); +} + +/// Entries 0, 3 and 5 start with `word`; 1 and 4 hold it after a shorter word, so their labels are shorter; 2 does not +/// hold it. The word's entries are first, between and last among those that hold it. +fn starts_and_holds(word: &str) -> Vec { + let labels = [format!("{word} Qqq"), format!("Q {word}"), "Other".to_owned(), format!("{word} Www"), format!("W {word}"), format!("{word} Zzz")]; + labels.iter().enumerate().map(|(at, label)| entry(&format!("t{at}"), label)).collect() +} + +/// Asserts that every entry of `starts` and `holds` is in `found` and that each entry of `starts` ranks above each entry +/// of `holds`. +fn starts_rank_first(found: &[usize], starts: &[usize], holds: &[usize], what: &str) { + let place = |e: usize| found.iter().position(|&f| f == e); + for s in starts { + for h in holds { + let (Some(s_at), Some(h_at)) = (place(*s), place(*h)) else { + panic!("{what}: entries {s} and {h} are both found in {found:?}"); + }; + assert!(s_at < h_at, "{what}: entry {s} ranks above entry {h} in {found:?}"); + } + } +} + +#[trial] +fn a_typo_or_a_synonym_is_read_as_the_word_each_entry_holds() { + // The query is read with the word each entry holds, so an entry it starts ranks above one with a shorter label + // that holds it later, wherever the entry is among those that hold the word. + let found = find(starts_and_holds("Shadow"), "shdaow"); + starts_rank_first(&found, &[0, 3, 5], &[1, 4], "a typo"); + assert!(!found.contains(&2), "an entry without the word is not found through a typo"); + let found = find(starts_and_holds("Delete"), "erase"); + starts_rank_first(&found, &[0, 3, 5], &[1, 4], "a synonym"); +} + #[trial] fn a_synonym_group_finds_its_members_and_a_translated_row_adds_more() { let entries = || vec![entry("t0", "Delete Layer"), entry("t1", "Effacer Tout"), entry("t2", "Phrase")];