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
This commit is contained in:
1 parent
5b2e2c0d1f
commit
6425cfaacf
1 file changed
+62
-3
+62
-3
@@ -1483,7 +1483,7 @@ impl Command {
|
|||||||
.and_then(|folder| folder.profiles.get(*profile))
|
.and_then(|folder| folder.profiles.get(*profile))
|
||||||
.map(|profile| profile.name.clone())
|
.map(|profile| profile.name.clone())
|
||||||
.unwrap_or_default(),
|
.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
|
commands
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1630,6 +1635,56 @@ fn fuzzy_match(haystack: &str, needle: &str) -> bool {
|
|||||||
.all(|needle| haystack.any(|haystack| haystack == needle))
|
.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.
|
/// Per-window UI state: tabs, menus and tab-strip scrolling.
|
||||||
struct WindowState {
|
struct WindowState {
|
||||||
tabs: Vec<Tab>,
|
tabs: Vec<Tab>,
|
||||||
@@ -13431,6 +13486,10 @@ mod tests {
|
|||||||
super::Command::matching(&state, window, "exit"),
|
super::Command::matching(&state, window, "exit"),
|
||||||
vec![super::Command::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));
|
let _ = update(&mut state, Message::RunSelectedCommand(window));
|
||||||
|
|
||||||
@@ -13632,8 +13691,8 @@ mod tests {
|
|||||||
assert_eq!(
|
assert_eq!(
|
||||||
super::Command::matching(&state, window, "temp 9"),
|
super::Command::matching(&state, window, "temp 9"),
|
||||||
vec![
|
vec![
|
||||||
super::Command::FocusWindow(destination),
|
super::Command::FocusTab(destination, Tab::Blank(9)),
|
||||||
super::Command::FocusTab(destination, Tab::Blank(9))
|
super::Command::FocusWindow(destination)
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user