From dee5b2a983f6a334753be48730868e8dd651b505 Mon Sep 17 00:00:00 2001 From: Matthias Deiml Date: Wed, 9 Nov 2022 10:17:09 +0100 Subject: Add LSP workspace command picker (#3140) * Add workspace command picker * Make command typable * Add optional argument to lsp-workspace-command--- helix-term/src/commands/lsp.rs | 8 +++++ helix-term/src/commands/typed.rs | 78 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) (limited to 'helix-term/src/commands') diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 5498fc83..c149e62b 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -603,6 +603,14 @@ pub fn code_action(cx: &mut Context) { }, ) } + +impl ui::menu::Item for lsp::Command { + type Data = (); + fn label(&self, _data: &Self::Data) -> Spans { + self.title.as_str().into() + } +} + pub fn execute_lsp_command(editor: &mut Editor, cmd: lsp::Command) { let doc = doc!(editor); let language_server = language_server!(editor, doc); diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 2f387bfd..c6810f05 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -1052,6 +1052,77 @@ fn update( } } +fn lsp_workspace_command( + cx: &mut compositor::Context, + args: &[Cow], + event: PromptEvent, +) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + + let (_, doc) = current!(cx.editor); + + let language_server = match doc.language_server() { + Some(language_server) => language_server, + None => { + cx.editor + .set_status("Language server not active for current buffer"); + return Ok(()); + } + }; + + let options = match &language_server.capabilities().execute_command_provider { + Some(options) => options, + None => { + cx.editor + .set_status("Workspace commands are not supported for this language server"); + return Ok(()); + } + }; + if args.is_empty() { + let commands = options + .commands + .iter() + .map(|command| helix_lsp::lsp::Command { + title: command.clone(), + command: command.clone(), + arguments: None, + }) + .collect::>(); + let callback = async move { + let call: job::Callback = Callback::EditorCompositor(Box::new( + move |_editor: &mut Editor, compositor: &mut Compositor| { + let picker = ui::Picker::new(commands, (), |cx, command, _action| { + execute_lsp_command(cx.editor, command.clone()); + }); + compositor.push(Box::new(overlayed(picker))) + }, + )); + Ok(call) + }; + cx.jobs.callback(callback); + } else { + let command = args.join(" "); + if options.commands.iter().any(|c| c == &command) { + execute_lsp_command( + cx.editor, + helix_lsp::lsp::Command { + title: command.clone(), + arguments: None, + command, + }, + ); + } else { + cx.editor.set_status(format!( + "`{command}` is not supported for this language server" + )); + return Ok(()); + } + } + Ok(()) +} + fn lsp_restart( cx: &mut compositor::Context, _args: &[Cow], @@ -1987,6 +2058,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ fun: update, completer: None, }, + TypableCommand { + name: "lsp-workspace-command", + aliases: &[], + doc: "Open workspace command picker", + fun: lsp_workspace_command, + completer: Some(completers::lsp_workspace_command), + }, TypableCommand { name: "lsp-restart", aliases: &[], -- cgit v1.2.3-70-g09d2