Changed command palette to have exit/quit as command, and the fuzzy matching ranks matching text, so quit makes the command higher then quick terminal
Moorly CI / windows-build (push) Successful in 3m6s
Moorly CI / linux-build (push) Successful in 4m16s

This commit is contained in:
Squid committed 2026-11-12 19:52:09 +01:00
1 parent 5b2e2c0d1f
commit 6425cfaacf
1 file changed
+62 -3
+62 -3
View File
@@ -1483,7 +1483,7 @@ impl Command {
.and_then(|folder| folder.profiles.get(*profile))
.map(|profile| profile.name.clone())
.unwrap_or_default(),
Self::Exit => "Exit".to_owned(),
Self::Exit => "Exit / Quit".to_owned(),
}
}
@@ -1600,6 +1600,11 @@ impl Command {
);
}
// Best matches first; ties keep the original command order.
commands.sort_by_key(|command| {
std::cmp::Reverse(fuzzy_score(&command.label(state), query))
});
commands
}
}
@@ -1630,6 +1635,56 @@ fn fuzzy_match(haystack: &str, needle: &str) -> bool {
.all(|needle| haystack.any(|haystack| haystack == needle))
}
/// Rank for a fuzzy match: exact and contiguous matches beat scattered
/// subsequences, so `quit` favours "Exit / Quit" over "Quick Terminal".
fn fuzzy_score(haystack: &str, needle: &str) -> i32 {
let needle = needle.trim().to_lowercase();
if needle.is_empty() {
return 0;
}
let haystack = haystack.to_lowercase();
if haystack == needle {
return 1000;
}
if haystack.starts_with(&needle) {
return 900;
}
if let Some(index) = haystack.find(&needle) {
let at_word_start = index == 0
|| !haystack[..index]
.chars()
.last()
.is_some_and(char::is_alphanumeric);
return if at_word_start { 800 } else { 700 };
}
// Scattered subsequence: prefer the most compact span.
let characters: Vec<char> = haystack.chars().collect();
let mut needle = needle.chars();
let mut next = needle.next();
let mut first = None;
let mut last = 0;
for (index, character) in characters.iter().enumerate() {
if next == Some(*character) {
first.get_or_insert(index);
last = index;
next = needle.next();
}
}
if next.is_some() {
return 0;
}
100 - (last - first.unwrap_or(0)).min(100) as i32
}
/// Per-window UI state: tabs, menus and tab-strip scrolling.
struct WindowState {
tabs: Vec<Tab>,
@@ -13431,6 +13486,10 @@ mod tests {
super::Command::matching(&state, window, "exit"),
vec![super::Command::Exit]
);
assert_eq!(
super::Command::matching(&state, window, "quit").first(),
Some(&super::Command::Exit)
);
let _ = update(&mut state, Message::RunSelectedCommand(window));
@@ -13632,8 +13691,8 @@ mod tests {
assert_eq!(
super::Command::matching(&state, window, "temp 9"),
vec![
super::Command::FocusWindow(destination),
super::Command::FocusTab(destination, Tab::Blank(9))
super::Command::FocusTab(destination, Tab::Blank(9)),
super::Command::FocusWindow(destination)
]
);