switch all fuzzy matches to nucleo

Centralize the thread local state management into the selector module
to make it a bit more convenient to consume from the various other
locations.

closes: https://github.com/wez/wezterm/issues/5532
This commit is contained in:
Wez Furlong committed 2024-07-14 11:03:54 -07:00
1 parent 6b5edad360
commit b20c61926e
7 files changed
+50 -65

No files matched your search

Generated
-20
View File
@@ -1871,15 +1871,6 @@ dependencies = [
"slab",
]
[[package]]
name = "fuzzy-matcher"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54614a3312934d066701a80f20f15fa3b56d67ac7722b39eea5b4c9dd1d66c94"
dependencies = [
"thread_local",
]
[[package]]
name = "generate-bidi"
version = "0.1.0"
@@ -5378,16 +5369,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bf63baf9f5039dadc247375c29eb13706706cfde997d0330d05aa63a77d8820"
[[package]]
name = "thread_local"
version = "1.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c"
dependencies = [
"cfg-if",
"once_cell",
]
[[package]]
name = "tiff"
version = "0.9.1"
@@ -6319,7 +6300,6 @@ dependencies = [
"finl_unicode",
"frecency",
"futures",
"fuzzy-matcher",
"hdrhistogram",
"http_req",
"image",
+2 -1
View File
@@ -35,7 +35,8 @@ As features stabilize some brief notes about them will accumulate here.
[custom_block_glyphs](config/lua/config/custom_block_glyphs.md) for more
details. Thanks to @stribor14! #5051 #5169
* Switched to the [nucleo](https://github.com/helix-editor/nucleo) fuzzy
matcher for `CharSelect`. #5532
matcher which produces matches that more closely match the popular `fzf`
program. #5532
#### New
* [wezterm.serde](config/lua/wezterm.serde/index.md) module for serialization
-1
View File
@@ -53,7 +53,6 @@ filedescriptor = { version="0.8", path = "../filedescriptor" }
finl_unicode = "1.2"
frecency = { path = "../frecency" }
futures = "0.3"
fuzzy-matcher = "0.3"
nucleo-matcher = "0.3"
hdrhistogram = "7.1"
http_req = "0.11"
+6 -6
View File
@@ -7,16 +7,16 @@
//! menus.
use crate::commands::derive_command_from_key_assignment;
use crate::inputmap::InputMap;
use crate::overlay::selector::{matcher_pattern, matcher_score};
use crate::termwindow::TermWindowNotif;
use config::configuration;
use config::keyassignment::{KeyAssignment, SpawnCommand, SpawnTabDomain};
use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher;
use mux::domain::{DomainId, DomainState};
use mux::pane::PaneId;
use mux::termwiztermtab::TermWizTerminal;
use mux::window::WindowId;
use mux::Mux;
use rayon::prelude::*;
use std::collections::BTreeMap;
use termwiz::cell::{AttributeChange, CellAttributes};
use termwiz::color::ColorAttribute;
@@ -183,19 +183,19 @@ impl LauncherState {
self.filtered_entries.clear();
let matcher = SkimMatcherV2::default();
let pattern = matcher_pattern(&self.filter_term);
struct MatchResult {
row_idx: usize,
score: i64,
score: u32,
}
let mut scores: Vec<MatchResult> = self
.entries
.iter()
.par_iter()
.enumerate()
.filter_map(|(row_idx, entry)| {
let score = matcher.fuzzy_match(&entry.label, &self.filter_term)?;
let score = matcher_score(&pattern, &entry.label)?;
Some(MatchResult { row_idx, score })
})
.collect();
+28 -7
View File
@@ -1,10 +1,12 @@
use super::quickselect;
use crate::scripting::guiwin::GuiWin;
use config::keyassignment::{InputSelector, InputSelectorEntry, KeyAssignment};
use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher;
use mux::termwiztermtab::TermWizTerminal;
use mux_lua::MuxPane;
use nucleo_matcher::pattern::Pattern;
use nucleo_matcher::{Matcher, Utf32Str};
use rayon::prelude::*;
use std::cell::RefCell;
use std::rc::Rc;
use termwiz::cell::{AttributeChange, CellAttributes};
use termwiz::color::ColorAttribute;
@@ -15,6 +17,25 @@ use termwiz_funcs::truncate_right;
const ROW_OVERHEAD: usize = 3;
thread_local! {
pub static MATCHER: RefCell<Matcher> = RefCell::new(Matcher::new(nucleo_matcher::Config::DEFAULT));
}
pub fn matcher_score(pattern: &Pattern, s: &str) -> Option<u32> {
MATCHER.with_borrow_mut(|matcher| {
let mut buf = vec![];
pattern.score(Utf32Str::new(s, &mut buf), matcher)
})
}
pub fn matcher_pattern(s: &str) -> Pattern {
nucleo_matcher::pattern::Pattern::parse(
s,
nucleo_matcher::pattern::CaseMatching::Ignore,
nucleo_matcher::pattern::Normalization::Smart,
)
}
struct SelectorState {
active_idx: usize,
max_items: usize,
@@ -40,20 +61,20 @@ impl SelectorState {
self.filtered_entries.clear();
let matcher = SkimMatcherV2::default();
struct MatchResult {
row_idx: usize,
score: i64,
score: u32,
}
let pattern = matcher_pattern(&self.filter_term);
let mut scores: Vec<MatchResult> = self
.args
.choices
.iter()
.par_iter()
.enumerate()
.filter_map(|(row_idx, entry)| {
let score = matcher.fuzzy_match(&entry.label, &self.filter_term)?;
let score = matcher_score(&pattern, &entry.label)?;
Some(MatchResult { row_idx, score })
})
.collect();
+6 -21
View File
@@ -1,3 +1,4 @@
use crate::overlay::selector::{matcher_pattern, matcher_score};
use crate::termwindow::box_model::*;
use crate::termwindow::modal::Modal;
use crate::termwindow::render::corners::{
@@ -13,7 +14,6 @@ use config::keyassignment::{
use config::Dimension;
use emojis::{Emoji, Group};
use frecency::Frecency;
use nucleo_matcher::{Matcher, Utf32Str};
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
@@ -271,11 +271,7 @@ fn compute_matches(selection: &str, aliases: &[Alias], group: CharSelectGroup) -
.map(|(idx, _a)| idx)
.collect()
} else {
let pattern = nucleo_matcher::pattern::Pattern::parse(
selection,
nucleo_matcher::pattern::CaseMatching::Ignore,
nucleo_matcher::pattern::Normalization::Smart,
);
let pattern = matcher_pattern(selection);
let numeric_selection = if selection.chars().all(|c| c.is_ascii_hexdigit()) {
// Make this uppercase so that eg: `e1` matches `U+E1` rather
@@ -293,18 +289,10 @@ fn compute_matches(selection: &str, aliases: &[Alias], group: CharSelectGroup) -
.par_iter()
.enumerate()
.filter_map(|(row_idx, entry)| {
thread_local! {
static MATCHER: RefCell<Matcher> = RefCell::new(Matcher::new(nucleo_matcher::Config::DEFAULT));
}
let glyph = entry.glyph();
let alias_result = MATCHER.with_borrow_mut(|matcher| {
let mut buf = vec![];
pattern
.score(Utf32Str::new(&entry.name, &mut buf), matcher)
.map(|score| MatchResult::new(row_idx, score , selection, aliases))
});
let alias_result = matcher_score(&pattern, &entry.name)
.map(|score| MatchResult::new(row_idx, score, selection, aliases));
match &numeric_selection {
Some(sel) => {
@@ -318,11 +306,8 @@ fn compute_matches(selection: &str, aliases: &[Alias], group: CharSelectGroup) -
},
))
} else {
let number_result = MATCHER.with_borrow_mut(|matcher| {
let mut buf = vec![];
pattern
.score(Utf32Str::new(&codepoints, &mut buf), matcher)
.map(|score| MatchResult::new(row_idx, score , selection, aliases))});
let number_result = matcher_score(&pattern, &codepoints)
.map(|score| MatchResult::new(row_idx, score, selection, aliases));
match (alias_result, number_result) {
(
+8 -9
View File
@@ -1,4 +1,5 @@
use crate::commands::{CommandDef, ExpandedCommand};
use crate::overlay::selector::{matcher_pattern, matcher_score};
use crate::termwindow::box_model::*;
use crate::termwindow::modal::Modal;
use crate::termwindow::render::corners::{
@@ -10,10 +11,9 @@ use crate::utilsprites::RenderMetrics;
use config::keyassignment::KeyAssignment;
use config::Dimension;
use frecency::Frecency;
use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher;
use luahelper::{from_lua_value_dynamic, impl_lua_conversion_dynamic};
use mux_lua::MuxPane;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::cell::{Ref, RefCell};
@@ -172,18 +172,18 @@ fn build_commands(
#[derive(Debug)]
struct MatchResult {
row_idx: usize,
score: i64,
score: u32,
}
impl MatchResult {
fn new(row_idx: usize, score: i64, selection: &str, commands: &[ExpandedCommand]) -> Self {
fn new(row_idx: usize, score: u32, selection: &str, commands: &[ExpandedCommand]) -> Self {
Self {
row_idx,
score: if commands[row_idx].brief == selection {
// Pump up the score for an exact match, otherwise
// the order may be undesirable if there are a lot
// of candidates with the same score
i64::max_value()
u32::max_value()
} else {
score
},
@@ -195,17 +195,16 @@ fn compute_matches(selection: &str, commands: &[ExpandedCommand]) -> Vec<usize>
if selection.is_empty() {
commands.iter().enumerate().map(|(idx, _)| idx).collect()
} else {
let matcher = SkimMatcherV2::default();
let pattern = matcher_pattern(selection);
let start = std::time::Instant::now();
let mut scores: Vec<MatchResult> = commands
.iter()
.par_iter()
.enumerate()
.filter_map(|(row_idx, entry)| {
let group = entry.menubar.join(" ");
let text = format!("{group}: {}. {} {:?}", entry.brief, entry.doc, entry.action);
matcher
.fuzzy_match(&text, selection)
matcher_score(&pattern, &text)
.map(|score| MatchResult::new(row_idx, score, selection, commands))
})
.collect();