Further improve /tabs command and slash arguments completion (#16216)

* renames `/tabs` to `/tab`
* allows to insert multiple tabs when fuzzy matching by the names
* improve slash command completion API, introduce a notion of multiple
arguments
* properly fire off commands on arguments' completions with
`run_command: true`

Release Notes:

- N/A

---------

Co-authored-by: Marshall Bowers <marshall@zed.dev>
This commit is contained in:
Kirill Bulatov 2024-08-14 17:11:51 +03:00 committed by GitHub
parent 88a12b60a9
commit 8fe2de1737
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 332 additions and 263 deletions

View file

@ -42,21 +42,26 @@ impl SlashCommand for TerminalSlashCommand {
fn complete_argument(
self: Arc<Self>,
_query: String,
arguments: &[String],
_cancel: Arc<AtomicBool>,
_workspace: Option<WeakView<Workspace>>,
_cx: &mut WindowContext,
) -> Task<Result<Vec<ArgumentCompletion>>> {
Task::ready(Ok(vec![ArgumentCompletion {
label: LINE_COUNT_ARG.into(),
new_text: LINE_COUNT_ARG.to_string(),
run_command: true,
}]))
let completions = if arguments.iter().any(|arg| arg == LINE_COUNT_ARG) {
Vec::new()
} else {
vec![ArgumentCompletion {
label: LINE_COUNT_ARG.into(),
new_text: LINE_COUNT_ARG.to_string(),
run_command: false,
}]
};
Task::ready(Ok(completions))
}
fn run(
self: Arc<Self>,
argument: Option<&str>,
arguments: &[String],
workspace: WeakView<Workspace>,
_delegate: Option<Arc<dyn LspAdapterDelegate>>,
cx: &mut WindowContext,
@ -75,9 +80,13 @@ impl SlashCommand for TerminalSlashCommand {
return Task::ready(Err(anyhow::anyhow!("no active terminal")));
};
let line_count = argument
.and_then(|a| parse_argument(a))
.unwrap_or(DEFAULT_CONTEXT_LINES);
let mut line_count = DEFAULT_CONTEXT_LINES;
if arguments.get(0).map(|s| s.as_str()) == Some(LINE_COUNT_ARG) {
if let Some(parsed_line_count) = arguments.get(1).and_then(|s| s.parse::<usize>().ok())
{
line_count = parsed_line_count;
}
}
let lines = active_terminal
.read(cx)
@ -101,13 +110,3 @@ impl SlashCommand for TerminalSlashCommand {
}))
}
}
fn parse_argument(argument: &str) -> Option<usize> {
let mut args = argument.split(' ');
if args.next() == Some(LINE_COUNT_ARG) {
if let Some(line_count) = args.next().and_then(|s| s.parse::<usize>().ok()) {
return Some(line_count);
}
}
None
}