From 81d6e385f85ab3ec3e088c191cf427ff9c8b7e88 Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Thu, 12 Dec 2019 08:38:21 -0500 Subject: [PATCH] Go to declaration This piggy backs off of find_all_refs which is not the most efficient. Optimizing this would be a good first issue for someone. --- crates/ra_lsp_server/src/caps.rs | 2 +- crates/ra_lsp_server/src/main_loop.rs | 1 + crates/ra_lsp_server/src/main_loop/handlers.rs | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/crates/ra_lsp_server/src/caps.rs b/crates/ra_lsp_server/src/caps.rs index 4cb259360bd5..f992bf45373a 100644 --- a/crates/ra_lsp_server/src/caps.rs +++ b/crates/ra_lsp_server/src/caps.rs @@ -28,7 +28,7 @@ pub fn server_capabilities() -> ServerCapabilities { retrigger_characters: None, work_done_progress_options: WorkDoneProgressOptions { work_done_progress: None }, }), - declaration_provider: None, + declaration_provider: Some(true), definition_provider: Some(true), type_definition_provider: Some(TypeDefinitionProviderCapability::Simple(true)), implementation_provider: Some(ImplementationProviderCapability::Simple(true)), diff --git a/crates/ra_lsp_server/src/main_loop.rs b/crates/ra_lsp_server/src/main_loop.rs index 158cac0be172..8cb7752a06d5 100644 --- a/crates/ra_lsp_server/src/main_loop.rs +++ b/crates/ra_lsp_server/src/main_loop.rs @@ -441,6 +441,7 @@ fn on_request( .on::(handlers::handle_on_type_formatting)? .on::(handlers::handle_document_symbol)? .on::(handlers::handle_workspace_symbol)? + .on::(handlers::handle_goto_declaration)? .on::(handlers::handle_goto_definition)? .on::(handlers::handle_goto_implementation)? .on::(handlers::handle_goto_type_definition)? diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 5b64b27cd2e2..5b4482e04c82 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -271,6 +271,22 @@ pub fn handle_workspace_symbol( } } +pub fn handle_goto_declaration( + world: WorldSnapshot, + params: req::TextDocumentPositionParams, +) -> Result> { + let _p = profile("handle_goto_declaration"); + let position = params.try_conv_with(&world)?; + + // FIXME: We should really only look for the declaration here. + let refs = match world.analysis().find_all_refs(position, None)? { + None => return Ok(None), + Some(refs) => refs, + }; + + Ok(Some(req::GotoDefinitionResponse::Scalar(refs.declaration().try_conv_with(&world)?))) +} + pub fn handle_goto_definition( world: WorldSnapshot, params: req::TextDocumentPositionParams,