Skip to content

Commit

Permalink
Add support for searching keymap-style actions in command palette
Browse files Browse the repository at this point in the history
For example, `editor::TabPrev` matches "editor: tab prev".

Co-Authored-By: Peter Tripp <peter@zed.dev>
  • Loading branch information
agu-z and notpeter committed Dec 17, 2024
1 parent 228c89a commit 9ca4d9f
Showing 1 changed file with 44 additions and 10 deletions.
54 changes: 44 additions & 10 deletions crates/command_palette/src/command_palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@ pub struct CommandPalette {
picker: View<Picker<CommandPaletteDelegate>>,
}

fn trim_consecutive_whitespaces(input: &str) -> String {
fn normalize_query(input: &str) -> String {
let mut result = String::with_capacity(input.len());
let mut last_char_was_whitespace = false;
let mut last_char = None;

for char in input.trim().chars() {
if char.is_whitespace() {
if !last_char_was_whitespace {
result.push(char);
match (last_char, char) {
(Some(':'), ':') => continue,
(Some(last_char), char) if last_char.is_whitespace() && char.is_whitespace() => continue,
_ => {
last_char = Some(char);
}
last_char_was_whitespace = true;
} else {
result.push(char);
last_char_was_whitespace = false;
}
result.push(char);
}

result
}

Expand Down Expand Up @@ -271,7 +271,7 @@ impl PickerDelegate for CommandPaletteDelegate {
let mut commands = self.all_commands.clone();
let hit_counts = cx.global::<HitCounts>().clone();
let executor = cx.background_executor().clone();
let query = trim_consecutive_whitespaces(query.as_str());
let query = normalize_query(query.as_str());
async move {
commands.sort_by_key(|action| {
(
Expand Down Expand Up @@ -544,6 +544,40 @@ mod tests {
assert!(palette.delegate.matches.is_empty())
});
}
#[gpui::test]
async fn test_normalized_matches(cx: &mut TestAppContext) {
let app_state = init_test(cx);
let project = Project::test(app_state.fs.clone(), [], cx).await;
let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project.clone(), cx));

let editor = cx.new_view(|cx| {
let mut editor = Editor::single_line(cx);
editor.set_text("abc", cx);
editor
});

workspace.update(cx, |workspace, cx| {
workspace.add_item_to_active_pane(Box::new(editor.clone()), None, true, cx);
editor.update(cx, |editor, cx| editor.focus(cx))
});

// Test normalize (trimming whitespace and double colons)
cx.simulate_keystrokes("cmd-shift-p");

let palette = workspace.update(cx, |workspace, cx| {
workspace
.active_modal::<CommandPalette>(cx)
.unwrap()
.read(cx)
.picker
.clone()
});

cx.simulate_input("Editor:: Backspace");
palette.update(cx, |palette, _| {
assert_eq!(palette.delegate.matches[0].string, "editor: backspace");
});
}

#[gpui::test]
async fn test_go_to_line(cx: &mut TestAppContext) {
Expand Down

0 comments on commit 9ca4d9f

Please sign in to comment.