From 43309391b30a7312afb90a79f24e11df068f5339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole=20Kr=C3=BCger?= Date: Mon, 23 Jan 2023 15:25:41 +0000 Subject: [PATCH] Support goto-declaration LSP command --- helix-lsp/src/client.rs | 25 +++++++++++++++++++++++++ helix-term/src/commands.rs | 1 + helix-term/src/commands/lsp.rs | 25 +++++++++++++++++++++++++ helix-term/src/keymap/default.rs | 1 + 4 files changed, 52 insertions(+) diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs index dd2581c6d916..6827f568d986 100644 --- a/helix-lsp/src/client.rs +++ b/helix-lsp/src/client.rs @@ -886,6 +886,31 @@ impl Client { )) } + pub fn goto_declaration( + &self, + text_document: lsp::TextDocumentIdentifier, + position: lsp::Position, + work_done_token: Option, + ) -> Option>> { + let capabilities = self.capabilities.get().unwrap(); + + // Return early if the server does not support goto-declaration. + match capabilities.declaration_provider { + Some( + lsp::DeclarationCapability::Simple(true) + | lsp::DeclarationCapability::RegistrationOptions(_) + | lsp::DeclarationCapability::Options(_), + ) => (), + _ => return None, + } + + Some(self.goto_request::( + text_document, + position, + work_done_token, + )) + } + pub fn goto_type_definition( &self, text_document: lsp::TextDocumentIdentifier, diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index e773a2c789d8..197027f9888a 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -285,6 +285,7 @@ impl MappableCommand { select_mode, "Enter selection extend mode", exit_select_mode, "Exit selection mode", goto_definition, "Goto definition", + goto_declaration, "Goto declaration", add_newline_above, "Add newline above", add_newline_below, "Add newline below", goto_type_definition, "Goto type definition", diff --git a/helix-term/src/commands/lsp.rs b/helix-term/src/commands/lsp.rs index 578eb6084196..d12aa436dd98 100644 --- a/helix-term/src/commands/lsp.rs +++ b/helix-term/src/commands/lsp.rs @@ -914,6 +914,31 @@ fn to_locations(definitions: Option) -> Vec future, + None => { + cx.editor + .set_error("Language server does not support goto-declaration"); + return; + } + }; + + cx.callback( + future, + move |editor, compositor, response: Option| { + let items = to_locations(response); + goto_impl(editor, compositor, items, offset_encoding); + }, + ); +} + pub fn goto_definition(cx: &mut Context) { let (view, doc) = current!(cx.editor); let language_server = language_server!(cx.editor, doc); diff --git a/helix-term/src/keymap/default.rs b/helix-term/src/keymap/default.rs index ef93dee08a77..d48e6935ea9b 100644 --- a/helix-term/src/keymap/default.rs +++ b/helix-term/src/keymap/default.rs @@ -44,6 +44,7 @@ pub fn default() -> HashMap { "l" => goto_line_end, "s" => goto_first_nonwhitespace, "d" => goto_definition, + "D" => goto_declaration, "y" => goto_type_definition, "r" => goto_reference, "i" => goto_implementation,