aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOle Krüger2023-01-31 10:38:53 +0000
committerGitHub2023-01-31 10:38:53 +0000
commit4eca4b3079bf53de874959270d0b3471d320debc (patch)
tree685da9a2ef16df7b0c135a636f2324e5a2248802
parentc9b583ea9b59aa78c12e3831ed9c73f83d1d35d8 (diff)
Support goto-declaration LSP command (#5646)
-rw-r--r--helix-lsp/src/client.rs25
-rw-r--r--helix-term/src/commands.rs1
-rw-r--r--helix-term/src/commands/lsp.rs25
-rw-r--r--helix-term/src/keymap/default.rs1
4 files changed, 52 insertions, 0 deletions
diff --git a/helix-lsp/src/client.rs b/helix-lsp/src/client.rs
index dd2581c6..6827f568 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<lsp::ProgressToken>,
+ ) -> Option<impl Future<Output = Result<Value>>> {
+ 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::<lsp::request::GotoDeclaration>(
+ 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 365d5b65..47ef1ff1 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 578eb608..d12aa436 100644
--- a/helix-term/src/commands/lsp.rs
+++ b/helix-term/src/commands/lsp.rs
@@ -914,6 +914,31 @@ fn to_locations(definitions: Option<lsp::GotoDefinitionResponse>) -> Vec<lsp::Lo
}
}
+pub fn goto_declaration(cx: &mut Context) {
+ let (view, doc) = current!(cx.editor);
+ let language_server = language_server!(cx.editor, doc);
+ let offset_encoding = language_server.offset_encoding();
+
+ let pos = doc.position(view.id, offset_encoding);
+
+ let future = match language_server.goto_declaration(doc.identifier(), pos, None) {
+ Some(future) => future,
+ None => {
+ cx.editor
+ .set_error("Language server does not support goto-declaration");
+ return;
+ }
+ };
+
+ cx.callback(
+ future,
+ move |editor, compositor, response: Option<lsp::GotoDefinitionResponse>| {
+ 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 ef93dee0..d48e6935 100644
--- a/helix-term/src/keymap/default.rs
+++ b/helix-term/src/keymap/default.rs
@@ -44,6 +44,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
"l" => goto_line_end,
"s" => goto_first_nonwhitespace,
"d" => goto_definition,
+ "D" => goto_declaration,
"y" => goto_type_definition,
"r" => goto_reference,
"i" => goto_implementation,