on master help sign in
Support goto-declaration LSP command (#5646)
commit4eca4b3079bf53de874959270d0b3471d320debc authorOle Krüger <ole@vprsm.de> dateTue 31 Jan 2023, 11:38 (4 years ago) parentc9b583e
4 files changed+52
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
helix-lsp/src/client.rs+25
@@ -886,6 +886,31 @@ impl Client {
886886 ))
887887 }
888888
889+ pub fn goto_declaration(
890+ &self,
891+ text_document: lsp::TextDocumentIdentifier,
892+ position: lsp::Position,
893+ work_done_token: Option<lsp::ProgressToken>,
894+ ) -> Option<impl Future<Output = Result<Value>>> {
895+ let capabilities = self.capabilities.get().unwrap();
896+
897+ // Return early if the server does not support goto-declaration.
898+ match capabilities.declaration_provider {
899+ Some(
900+ lsp::DeclarationCapability::Simple(true)
901+ | lsp::DeclarationCapability::RegistrationOptions(_)
902+ | lsp::DeclarationCapability::Options(_),
903+ ) => (),
904+ _ => return None,
905+ }
906+
907+ Some(self.goto_request::<lsp::request::GotoDeclaration>(
908+ text_document,
909+ position,
910+ work_done_token,
911+ ))
912+ }
913+
889914 pub fn goto_type_definition(
890915 &self,
891916 text_document: lsp::TextDocumentIdentifier,
helix-term/src/commands.rs+1
@@ -285,6 +285,7 @@ impl MappableCommand {
285285 select_mode, "Enter selection extend mode",
286286 exit_select_mode, "Exit selection mode",
287287 goto_definition, "Goto definition",
288+ goto_declaration, "Goto declaration",
288289 add_newline_above, "Add newline above",
289290 add_newline_below, "Add newline below",
290291 goto_type_definition, "Goto type definition",
helix-term/src/commands/lsp.rs+25
@@ -914,6 +914,31 @@ fn to_locations(definitions: Option<lsp::GotoDefinitionResponse>) -> Vec<lsp::Lo
914914 }
915915}
916916
917+pub fn goto_declaration(cx: &mut Context) {
918+ let (view, doc) = current!(cx.editor);
919+ let language_server = language_server!(cx.editor, doc);
920+ let offset_encoding = language_server.offset_encoding();
921+
922+ let pos = doc.position(view.id, offset_encoding);
923+
924+ let future = match language_server.goto_declaration(doc.identifier(), pos, None) {
925+ Some(future) => future,
926+ None => {
927+ cx.editor
928+ .set_error("Language server does not support goto-declaration");
929+ return;
930+ }
931+ };
932+
933+ cx.callback(
934+ future,
935+ move |editor, compositor, response: Option<lsp::GotoDefinitionResponse>| {
936+ let items = to_locations(response);
937+ goto_impl(editor, compositor, items, offset_encoding);
938+ },
939+ );
940+}
941+
917942pub fn goto_definition(cx: &mut Context) {
918943 let (view, doc) = current!(cx.editor);
919944 let language_server = language_server!(cx.editor, doc);
helix-term/src/keymap/default.rs+1
@@ -44,6 +44,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
4444 "l" => goto_line_end,
4545 "s" => goto_first_nonwhitespace,
4646 "d" => goto_definition,
47+ "D" => goto_declaration,
4748 "y" => goto_type_definition,
4849 "r" => goto_reference,
4950 "i" => goto_implementation,