Improve matches on command palette (#8515)
Release Notes: - Fixed consecutive spaces in command palette influencing selection. #8184 Optionally, include screenshots / media showcasing your addition that can be included in the release notes. https://github.com/zed-industries/zed/assets/25414681/a4682247-f52c-4ab9-a32a-51ab5cf3dbcc
This commit is contained in:
@@ -37,6 +37,24 @@ pub struct CommandPalette {
|
|||||||
picker: View<Picker<CommandPaletteDelegate>>,
|
picker: View<Picker<CommandPaletteDelegate>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn trim_consecutive_whitespaces(input: &str) -> String {
|
||||||
|
let mut result = String::with_capacity(input.len());
|
||||||
|
let mut last_char_was_whitespace = false;
|
||||||
|
|
||||||
|
for char in input.trim().chars() {
|
||||||
|
if char.is_whitespace() {
|
||||||
|
if !last_char_was_whitespace {
|
||||||
|
result.push(char);
|
||||||
|
}
|
||||||
|
last_char_was_whitespace = true;
|
||||||
|
} else {
|
||||||
|
result.push(char);
|
||||||
|
last_char_was_whitespace = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
impl CommandPalette {
|
impl CommandPalette {
|
||||||
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
|
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
|
||||||
workspace.register_action(|workspace, _: &Toggle, cx| {
|
workspace.register_action(|workspace, _: &Toggle, cx| {
|
||||||
@@ -247,7 +265,7 @@ impl PickerDelegate for CommandPaletteDelegate {
|
|||||||
let mut commands = self.all_commands.clone();
|
let mut commands = self.all_commands.clone();
|
||||||
let hit_counts = cx.global::<HitCounts>().clone();
|
let hit_counts = cx.global::<HitCounts>().clone();
|
||||||
let executor = cx.background_executor().clone();
|
let executor = cx.background_executor().clone();
|
||||||
let query = query.clone();
|
let query = trim_consecutive_whitespaces(&query.as_str());
|
||||||
async move {
|
async move {
|
||||||
commands.sort_by_key(|action| {
|
commands.sort_by_key(|action| {
|
||||||
(
|
(
|
||||||
@@ -265,7 +283,6 @@ impl PickerDelegate for CommandPaletteDelegate {
|
|||||||
char_bag: command.name.chars().collect(),
|
char_bag: command.name.chars().collect(),
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let matches = if query.is_empty() {
|
let matches = if query.is_empty() {
|
||||||
candidates
|
candidates
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
|||||||
Reference in New Issue
Block a user