Skip to content

Commit

Permalink
Add normalize_query test and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
agu-z committed Dec 17, 2024
1 parent 9ca4d9f commit 14fb9c5
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion crates/command_palette/src/command_palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,19 @@ pub struct CommandPalette {
picker: View<Picker<CommandPaletteDelegate>>,
}

/// Removes subsequent whitespace characters and double colons from the query.
///
/// This improves the likelihood of a match by either humanized name or keymap-style name.
fn normalize_query(input: &str) -> String {
let mut result = String::with_capacity(input.len());
let mut last_char = None;

for char in input.trim().chars() {
match (last_char, char) {
(Some(':'), ':') => continue,
(Some(last_char), char) if last_char.is_whitespace() && char.is_whitespace() => continue,
(Some(last_char), char) if last_char.is_whitespace() && char.is_whitespace() => {
continue
}
_ => {
last_char = Some(char);
}
Expand Down Expand Up @@ -474,6 +479,25 @@ mod tests {
);
}

#[test]
fn test_normalize_query() {
assert_eq!(normalize_query("editor: backspace"), "editor: backspace");
assert_eq!(normalize_query("editor: backspace"), "editor: backspace");
assert_eq!(normalize_query("editor: backspace"), "editor: backspace");
assert_eq!(
normalize_query("editor::GoToDefinition"),
"editor:GoToDefinition"
);
assert_eq!(
normalize_query("editor::::GoToDefinition"),
"editor:GoToDefinition"
);
assert_eq!(
normalize_query("editor: :GoToDefinition"),
"editor: :GoToDefinition"
);
}

#[gpui::test]
async fn test_command_palette(cx: &mut TestAppContext) {
let app_state = init_test(cx);
Expand Down

0 comments on commit 14fb9c5

Please sign in to comment.