From daaa250109b05e77989452012659c5ba83da4d92 Mon Sep 17 00:00:00 2001 From: Michael Sloan Date: Sat, 11 Jan 2025 14:59:06 -0700 Subject: [PATCH] Include display text for LSP commands in errors (#23012) https://github.com/zed-industries/zed/pull/23011 adds display of errors in the UI so it's now more important to contextualize these. Release Notes: - N/A --- crates/project/src/lsp_command.rs | 62 +++++++++++++++++++++++++++ crates/project/src/lsp_ext_command.rs | 12 ++++++ crates/project/src/lsp_store.rs | 20 ++++++--- 3 files changed, 89 insertions(+), 5 deletions(-) diff --git a/crates/project/src/lsp_command.rs b/crates/project/src/lsp_command.rs index 4d998631ba1b35e85889cb68324ce5283f421728..c58fcb1351e1fef7c1404934aa4bb635ceccb33d 100644 --- a/crates/project/src/lsp_command.rs +++ b/crates/project/src/lsp_command.rs @@ -76,6 +76,8 @@ pub trait LspCommand: 'static + Sized + Send + std::fmt::Debug { type LspRequest: 'static + Send + lsp::request::Request; type ProtoRequest: 'static + Send + proto::RequestMessage; + fn display_name(&self) -> &str; + fn status(&self) -> Option { None } @@ -244,6 +246,10 @@ impl LspCommand for PrepareRename { type LspRequest = lsp::request::PrepareRenameRequest; type ProtoRequest = proto::PrepareRename; + fn display_name(&self) -> &str { + "Prepare rename" + } + fn to_lsp_params_or_response( &self, path: &Path, @@ -423,6 +429,10 @@ impl LspCommand for PerformRename { type LspRequest = lsp::request::Rename; type ProtoRequest = proto::PerformRename; + fn display_name(&self) -> &str { + "Rename" + } + fn to_lsp( &self, path: &Path, @@ -541,6 +551,10 @@ impl LspCommand for GetDefinition { type LspRequest = lsp::request::GotoDefinition; type ProtoRequest = proto::GetDefinition; + fn display_name(&self) -> &str { + "Get definition" + } + fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool { capabilities .server_capabilities @@ -636,6 +650,10 @@ impl LspCommand for GetDeclaration { type LspRequest = lsp::request::GotoDeclaration; type ProtoRequest = proto::GetDeclaration; + fn display_name(&self) -> &str { + "Get declaration" + } + fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool { capabilities .server_capabilities @@ -731,6 +749,10 @@ impl LspCommand for GetImplementation { type LspRequest = lsp::request::GotoImplementation; type ProtoRequest = proto::GetImplementation; + fn display_name(&self) -> &str { + "Get implementation" + } + fn to_lsp( &self, path: &Path, @@ -819,6 +841,10 @@ impl LspCommand for GetTypeDefinition { type LspRequest = lsp::request::GotoTypeDefinition; type ProtoRequest = proto::GetTypeDefinition; + fn display_name(&self) -> &str { + "Get type definition" + } + fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool { !matches!( &capabilities.server_capabilities.type_definition_provider, @@ -1122,6 +1148,10 @@ impl LspCommand for GetReferences { type LspRequest = lsp::request::References; type ProtoRequest = proto::GetReferences; + fn display_name(&self) -> &str { + "Find all references" + } + fn status(&self) -> Option { Some("Finding references...".to_owned()) } @@ -1298,6 +1328,10 @@ impl LspCommand for GetDocumentHighlights { type LspRequest = lsp::request::DocumentHighlightRequest; type ProtoRequest = proto::GetDocumentHighlights; + fn display_name(&self) -> &str { + "Get document highlights" + } + fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool { capabilities .server_capabilities @@ -1447,6 +1481,10 @@ impl LspCommand for GetSignatureHelp { type LspRequest = lsp::SignatureHelpRequest; type ProtoRequest = proto::GetSignatureHelp; + fn display_name(&self) -> &str { + "Get signature help" + } + fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool { capabilities .server_capabilities @@ -1550,6 +1588,10 @@ impl LspCommand for GetHover { type LspRequest = lsp::request::HoverRequest; type ProtoRequest = proto::GetHover; + fn display_name(&self) -> &str { + "Get hover" + } + fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool { match capabilities.server_capabilities.hover_provider { Some(lsp::HoverProviderCapability::Simple(enabled)) => enabled, @@ -1776,6 +1818,10 @@ impl LspCommand for GetCompletions { type LspRequest = lsp::request::Completion; type ProtoRequest = proto::GetCompletions; + fn display_name(&self) -> &str { + "Get completion" + } + fn to_lsp( &self, path: &Path, @@ -2098,6 +2144,10 @@ impl LspCommand for GetCodeActions { type LspRequest = lsp::request::CodeActionRequest; type ProtoRequest = proto::GetCodeActions; + fn display_name(&self) -> &str { + "Get code actions" + } + fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool { match &capabilities.server_capabilities.code_action_provider { None => false, @@ -2314,6 +2364,10 @@ impl LspCommand for OnTypeFormatting { type LspRequest = lsp::request::OnTypeFormatting; type ProtoRequest = proto::OnTypeFormatting; + fn display_name(&self) -> &str { + "Formatting on typing" + } + fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool { let Some(on_type_formatting_options) = &capabilities .server_capabilities @@ -2820,6 +2874,10 @@ impl LspCommand for InlayHints { type LspRequest = lsp::InlayHintRequest; type ProtoRequest = proto::InlayHints; + fn display_name(&self) -> &str { + "Inlay hints" + } + fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool { let Some(inlay_hint_provider) = &capabilities.server_capabilities.inlay_hint_provider else { @@ -2978,6 +3036,10 @@ impl LspCommand for LinkedEditingRange { type LspRequest = lsp::request::LinkedEditingRange; type ProtoRequest = proto::LinkedEditingRange; + fn display_name(&self) -> &str { + "Linked editing range" + } + fn check_capabilities(&self, capabilities: AdapterServerCapabilities) -> bool { let Some(linked_editing_options) = &capabilities .server_capabilities diff --git a/crates/project/src/lsp_ext_command.rs b/crates/project/src/lsp_ext_command.rs index 2d2610bc314357a31b0eca698a073fa5dd0e2bec..1d329a84b006b6b7b2f4b8505e4a8ab4833d9343 100644 --- a/crates/project/src/lsp_ext_command.rs +++ b/crates/project/src/lsp_ext_command.rs @@ -47,6 +47,10 @@ impl LspCommand for ExpandMacro { type LspRequest = LspExpandMacro; type ProtoRequest = proto::LspExtExpandMacro; + fn display_name(&self) -> &str { + "Expand macro" + } + fn to_lsp( &self, path: &Path, @@ -171,6 +175,10 @@ impl LspCommand for OpenDocs { type LspRequest = LspOpenDocs; type ProtoRequest = proto::LspExtOpenDocs; + fn display_name(&self) -> &str { + "Open docs" + } + fn to_lsp( &self, path: &Path, @@ -284,6 +292,10 @@ impl LspCommand for SwitchSourceHeader { type LspRequest = LspSwitchSourceHeader; type ProtoRequest = proto::LspExtSwitchSourceHeader; + fn display_name(&self) -> &str { + "Switch source header" + } + fn to_lsp( &self, path: &Path, diff --git a/crates/project/src/lsp_store.rs b/crates/project/src/lsp_store.rs index 01643cf1fe6269ee729e67b970deddd6396b7462..c4c01214a056e3b69744e2502c010d35d4dcfb79 100644 --- a/crates/project/src/lsp_store.rs +++ b/crates/project/src/lsp_store.rs @@ -3509,9 +3509,13 @@ impl LspStore { Ok(LspParamsOrResponse::Params(lsp_params)) => lsp_params, Ok(LspParamsOrResponse::Response(response)) => return Task::ready(Ok(response)), Err(err) => { - let message = - format!("LSP request to {} failed: {}", language_server.name(), err); - log::error!("{}", message); + let message = format!( + "{} via {} failed: {}", + request.display_name(), + language_server.name(), + err + ); + log::warn!("{}", message); return Task::ready(Err(anyhow!(message))); } }; @@ -3562,8 +3566,14 @@ impl LspStore { let result = lsp_request.await; let response = result.map_err(|err| { - log::warn!("LSP request to {} failed: {}", language_server.name(), err); - err + let message = format!( + "{} via {} failed: {}", + request.display_name(), + language_server.name(), + err + ); + log::warn!("{}", message); + anyhow!(message) })?; let response = request