From 54112d2a22bfb66e64af235f2ac3ef2e251e4e97 Mon Sep 17 00:00:00 2001 From: MDeiml Date: Fri, 22 Jul 2022 00:40:09 +0200 Subject: [PATCH] Add workspace command picker --- helix-lsp/src/client.rs | 3 +++ helix-term/src/commands.rs | 1 + helix-term/src/commands/lsp.rs | 36 ++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index 0b443ccf402d5..af3c4b57d17b1 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -298,6 +298,9 @@ impl Client { dynamic_registration: Some(false), ..Default::default() }), + execute_command: Some(lsp::DynamicRegistrationClientCapabilities { + dynamic_registration: Some(false), + }), ..Default::default() }), text_document: Some(lsp::TextDocumentClientCapabilities { diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 48bd9e572a631..084b4838719ad 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -268,6 +268,7 @@ impl MappableCommand { file_picker, "Open file picker", file_picker_in_current_directory, "Open file picker at current working directory", code_action, "Perform code action", + workspace_command_picker, "Open workspace command picker", buffer_picker, "Open buffer picker", jumplist_picker, "Open jumplist picker", symbol_picker, "Open symbol picker", diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 3c72cd2a54422..c45c6b25859be 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -603,6 +603,42 @@ 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 workspace_command_picker(cx: &mut Context) { + let (_, doc) = current!(cx.editor); + + let language_server = language_server!(cx.editor, doc); + + let options = match &language_server.capabilities().execute_command_provider { + Some(options) => options, + None => return, + }; + let commands = options + .commands + .iter() + .map(|command| lsp::Command { + title: command.clone(), + command: command.clone(), + arguments: None, + }) + .collect::>(); + cx.callback = Some(Box::new( + move |compositor: &mut Compositor, _cx: &mut compositor::Context| { + let picker = ui::Picker::new(commands, (), move |cx, command, _action| { + execute_lsp_command(cx.editor, command.clone()); + }); + compositor.push(Box::new(overlayed(picker))) + }, + )); +} + pub fn execute_lsp_command(editor: &mut Editor, cmd: lsp::Command) { let doc = doc!(editor); let language_server = language_server!(editor, doc);