lsp: Support Goto Declaration (#15785)
Adds support for [Goto Declaration](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_declaration) LSP command. I am particularly interested in [this for Rust projects](https://rust-analyzer.github.io/manual.html#go-to-declaration), to be able to navigate to the place where a trait method is declared, coming from a trait method implementation. I noticed this was something I could do in VSCode before, but was somehow missing is Zed. Thanks to the already existing infrastructure for Goto Definition, I just followed and copy-paste-adapted it for Goto Declaration. As a bonus, I added `ctrl-F12` and `alt-ctrl-F12` as default macOS keybindings for `GoToDeclaration` and `GoToDeclarationSplit`, respectively. They are not keybindings from another editor, but I figured they made sense to be grouped along with the other *F12 commands. ### Release Notes: - Added "Go to declaration" editor action. - vim: Breaking change to keybindings after introduction of the `Go to declaration` editor action. The new keybindings are the following (and can be found [here](https://zed.dev/docs/vim), alongside the other key bindings): - `g d` - Go to definition - `g D` - Go to declaration - `g y` - Go to type definition - `g I` - Go to implementation https://github.com/user-attachments/assets/ee5c10a8-94f0-4e50-afbb-6f71db540c1b --------- Co-authored-by: Thorsten Ball <mrnugget@gmail.com>
This commit is contained in:
co-authored by
Thorsten Ball
parent
82db5dedfb
commit
7b5fdcee7f
@@ -210,6 +210,8 @@ gpui::actions!(
|
||||
Format,
|
||||
GoToDefinition,
|
||||
GoToDefinitionSplit,
|
||||
GoToDeclaration,
|
||||
GoToDeclarationSplit,
|
||||
GoToDiagnostic,
|
||||
GoToHunk,
|
||||
GoToImplementation,
|
||||
|
||||
@@ -1539,6 +1539,7 @@ pub(crate) struct NavigationData {
|
||||
|
||||
enum GotoDefinitionKind {
|
||||
Symbol,
|
||||
Declaration,
|
||||
Type,
|
||||
Implementation,
|
||||
}
|
||||
@@ -8948,6 +8949,22 @@ impl Editor {
|
||||
self.go_to_definition_of_kind(GotoDefinitionKind::Symbol, false, cx)
|
||||
}
|
||||
|
||||
pub fn go_to_declaration(
|
||||
&mut self,
|
||||
_: &GoToDeclaration,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Task<Result<bool>> {
|
||||
self.go_to_definition_of_kind(GotoDefinitionKind::Declaration, false, cx)
|
||||
}
|
||||
|
||||
pub fn go_to_declaration_split(
|
||||
&mut self,
|
||||
_: &GoToDeclaration,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) -> Task<Result<bool>> {
|
||||
self.go_to_definition_of_kind(GotoDefinitionKind::Declaration, true, cx)
|
||||
}
|
||||
|
||||
pub fn go_to_implementation(
|
||||
&mut self,
|
||||
_: &GoToImplementation,
|
||||
@@ -9008,6 +9025,7 @@ impl Editor {
|
||||
let project = workspace.read(cx).project().clone();
|
||||
let definitions = project.update(cx, |project, cx| match kind {
|
||||
GotoDefinitionKind::Symbol => project.definition(&buffer, head, cx),
|
||||
GotoDefinitionKind::Declaration => project.declaration(&buffer, head, cx),
|
||||
GotoDefinitionKind::Type => project.type_definition(&buffer, head, cx),
|
||||
GotoDefinitionKind::Implementation => project.implementation(&buffer, head, cx),
|
||||
});
|
||||
|
||||
@@ -300,6 +300,12 @@ impl EditorElement {
|
||||
register_action(view, cx, |editor, a, cx| {
|
||||
editor.go_to_definition_split(a, cx).detach_and_log_err(cx);
|
||||
});
|
||||
register_action(view, cx, |editor, a, cx| {
|
||||
editor.go_to_declaration(a, cx).detach_and_log_err(cx);
|
||||
});
|
||||
register_action(view, cx, |editor, a, cx| {
|
||||
editor.go_to_declaration_split(a, cx).detach_and_log_err(cx);
|
||||
});
|
||||
register_action(view, cx, |editor, a, cx| {
|
||||
editor.go_to_implementation(a, cx).detach_and_log_err(cx);
|
||||
});
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::ops::Range;
|
||||
|
||||
use crate::GoToDeclaration;
|
||||
use crate::{
|
||||
selections_collection::SelectionsCollection, Copy, CopyPermalinkToLine, Cut, DisplayPoint,
|
||||
DisplaySnapshot, Editor, EditorMode, FindAllReferences, GoToDefinition, GoToImplementation,
|
||||
@@ -163,6 +164,7 @@ pub fn deploy_context_menu(
|
||||
.on_blur_subscription(Subscription::new(|| {}))
|
||||
.action("Rename Symbol", Box::new(Rename))
|
||||
.action("Go to Definition", Box::new(GoToDefinition))
|
||||
.action("Go to Declaration", Box::new(GoToDeclaration))
|
||||
.action("Go to Type Definition", Box::new(GoToTypeDefinition))
|
||||
.action("Go to Implementation", Box::new(GoToImplementation))
|
||||
.action("Find All References", Box::new(FindAllReferences))
|
||||
|
||||
Reference in New Issue
Block a user