ui-widgets: SearchIndex::for_host builds the command index without a Ui

This commit is contained in:
bflatastic
2026-11-22 04:31:14 +07:00
parent f8176040a0
commit f8910e8773
2 changed files with 48 additions and 8 deletions
+23 -8
View File
@@ -4,8 +4,9 @@
//! matches however the label is written.
use super::{HelpEntry, Synonyms};
use crate::runtime::env::EnvView;
use crate::runtime::env::{EnvView, Label};
use crate::runtime::words::common::CommonWords;
use craft_host::Host;
use craft_i18n::Folder;
use std::cmp::Reverse;
use std::collections::HashMap;
@@ -269,16 +270,30 @@ impl SearchIndex {
/// label are not stemmed (they may be in another language); a label with none is the English one. The path's words
/// and the description words are stemmed.
pub fn new(env: &EnvView<'_>, entries: &[HelpEntry], synonyms: &Synonyms) -> Self {
let folder = env.folder();
let fold = |text: &str| normalize(&folder, text);
let separator = fold(env.words::<CommonWords>().path_separator.as_str());
let separator = &env.words::<CommonWords>().path_separator;
Self::build(&env.folder(), separator.as_str(), &|label| env.resolve(label), entries, synonyms)
}
/// [`SearchIndex::new`] without a `Ui`, in the interface language of `host` (a command, which runs outside a
/// frame): the words, folding and path separator a scope under `host` reads.
pub fn for_host(host: &Host, entries: &[HelpEntry], synonyms: &Synonyms) -> Self {
let words = host.words();
let separator = &words.set::<CommonWords>().path_separator;
Self::build(&Folder::new(host.language_code()), separator.as_str(), &|label| label.resolved(&words), entries, synonyms)
}
/// The index of `entries` with labels shown through `resolve`, folded with `folder`, the path joined with
/// `separator`.
fn build(folder: &Folder, separator: &str, resolve: &dyn Fn(&Label) -> Label, entries: &[HelpEntry], synonyms: &Synonyms) -> Self {
let fold = |text: &str| normalize(folder, text);
let separator = fold(separator);
let joiner = format!(" {separator} ");
let mut found: Vec<(String, usize, u8)> = Vec::new();
let mut names = Vec::with_capacity(entries.len());
for (at, entry) in entries.iter().enumerate() {
let shown = fold(env.resolve(entry.label()).as_str());
let english = entry.english().map(|label| fold(env.resolve(label).as_str()));
let path = entry.path().iter().map(|label| fold(env.resolve(label).as_str())).collect::<Vec<_>>().join(&joiner);
let shown = fold(resolve(entry.label()).as_str());
let english = entry.english().map(|label| fold(resolve(label).as_str()));
let path = entry.path().iter().map(|label| fold(resolve(label).as_str())).collect::<Vec<_>>().join(&joiner);
add_words(&mut found, &path, at, PATH, Some(PATH_STEMMED));
let labels: Vec<(String, bool)> = std::iter::once((shown, english.is_none())).chain(english.map(|label| (label, true))).collect();
let mut pairs = Vec::with_capacity(labels.len());
@@ -330,7 +345,7 @@ impl SearchIndex {
map.entry(form.into()).or_insert(g);
}
}
SearchIndex { folder, separator, entries: names, terms, synonyms: map }
SearchIndex { folder: *folder, separator, entries: names, terms, synonyms: map }
}
/// The text of the word at place `t` in `terms`.
+25
View File
@@ -104,3 +104,28 @@ fn every_word_of_a_query_must_match_in_any_order_and_the_command_id_is_found() {
assert_eq!(found, vec![0, 1], "the id matches in any case, as does the label that starts with it");
assert_eq!(find(ids(), "slanted format.italic"), vec![0], "a word in the label and a word in the id");
}
/// The index built without a `Ui` (a command's search) ranks as the one a frame under the same host builds.
#[trial]
fn an_index_built_for_a_host_ranks_as_the_frame_under_it_does() {
let host = std::sync::Arc::new(craft_host::Host::builder(craft_host::AppInfo { id: "trial", name: "Trial", version: "0" }).build());
let entries = vec![
entry("t0", "Zip").with_words("copy the layer"),
entry("t1", "Zzz").with_path(["Copy Tools"]),
entry("t2", "Make Copy"),
entry("t3", "Outline Stroke").with_path(["Object", "Path"]),
];
let queries = ["copy", "tools", "object path", "outlines"];
let in_frame: Rc<RefCell<Vec<Vec<usize>>>> = Rc::default();
let (out, listed) = (in_frame.clone(), entries.clone());
let mut fixture = Fixture::new(move |ui| {
let index = SearchIndex::new(&Env::of(ui).view(), &listed, &english());
*out.borrow_mut() = queries.iter().map(|query| rank(&index, query, &[], &|_| 0)).collect();
});
fixture.host(host.clone());
fixture.settle();
let index = SearchIndex::for_host(&host, &entries, &english());
let without_ui: Vec<Vec<usize>> = queries.iter().map(|query| rank(&index, query, &[], &|_| 0)).collect();
assert!(without_ui.iter().all(|found| !found.is_empty()), "every query finds something: {without_ui:?}");
assert_eq!(without_ui, in_frame.take());
}