Skip to content

Commit

Permalink
Include display text for LSP commands in errors (#23012)
Browse files Browse the repository at this point in the history
#23011 adds display of errors
in the UI so it's now more important to contextualize these.

Release Notes:

- N/A
  • Loading branch information
mgsloan authored Jan 11, 2025
1 parent de2e197 commit daaa250
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 5 deletions.
62 changes: 62 additions & 0 deletions crates/project/src/lsp_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
None
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<String> {
Some("Finding references...".to_owned())
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions crates/project/src/lsp_ext_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 15 additions & 5 deletions crates/project/src/lsp_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
};
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit daaa250

Please sign in to comment.