Skip to content

Commit e2ab0ff

Browse files
committed
Aggregate errors and warnings in the status message
1 parent 8b8cd04 commit e2ab0ff

File tree

3 files changed

+62
-20
lines changed

3 files changed

+62
-20
lines changed

crates/rust-analyzer/src/lsp_utils.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,41 @@ impl Progress {
3636
}
3737

3838
impl GlobalState {
39-
pub(crate) fn show_message(&mut self, typ: lsp_types::MessageType, message: String) {
40-
let message = message;
41-
self.send_notification::<lsp_types::notification::ShowMessage>(
42-
lsp_types::ShowMessageParams { typ, message },
43-
)
39+
pub(crate) fn show_message(
40+
&mut self,
41+
typ: lsp_types::MessageType,
42+
message: String,
43+
show_open_log_button: bool,
44+
) {
45+
match self.config.open_server_logs() && show_open_log_button {
46+
true => self.send_request::<lsp_types::request::ShowMessageRequest>(
47+
lsp_types::ShowMessageRequestParams {
48+
typ,
49+
message,
50+
actions: Some(vec![lsp_types::MessageActionItem {
51+
title: "Open server logs".to_owned(),
52+
properties: Default::default(),
53+
}]),
54+
},
55+
|this, resp| {
56+
let lsp_server::Response { error: None, result: Some(result), .. } = resp
57+
else { return };
58+
if let Ok(Some(_item)) = crate::from_json::<
59+
<lsp_types::request::ShowMessageRequest as lsp_types::request::Request>::Result,
60+
>(
61+
lsp_types::request::ShowMessageRequest::METHOD, &result
62+
) {
63+
this.send_notification::<lsp_ext::OpenServerLogs>(());
64+
}
65+
},
66+
),
67+
false => self.send_notification::<lsp_types::notification::ShowMessage>(
68+
lsp_types::ShowMessageParams {
69+
typ,
70+
message,
71+
},
72+
),
73+
}
4474
}
4575

4676
/// Sends a notification to the client containing the error `message`.

crates/rust-analyzer/src/main_loop.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,19 @@ impl GlobalState {
406406

407407
if self.config.server_status_notification() {
408408
self.send_notification::<lsp_ext::ServerStatusNotification>(status);
409-
} else if let (lsp_ext::Health::Error, Some(message)) = (status.health, &status.message)
410-
{
411-
self.show_and_log_error(message.clone(), None);
409+
} else if let (health, Some(message)) = (status.health, &status.message) {
410+
let open_log_button = tracing::enabled!(tracing::Level::ERROR)
411+
&& (self.fetch_build_data_error().is_err()
412+
|| self.fetch_workspace_error().is_err());
413+
self.show_message(
414+
match health {
415+
lsp_ext::Health::Ok => lsp_types::MessageType::INFO,
416+
lsp_ext::Health::Warning => lsp_types::MessageType::WARNING,
417+
lsp_ext::Health::Error => lsp_types::MessageType::ERROR,
418+
},
419+
message.clone(),
420+
open_log_button,
421+
);
412422
}
413423
}
414424
}
@@ -919,6 +929,7 @@ impl GlobalState {
919929
this.show_message(
920930
lsp_types::MessageType::WARNING,
921931
error.to_string(),
932+
false,
922933
);
923934
}
924935
this.update_configuration(config);

crates/rust-analyzer/src/reload.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,38 +90,39 @@ impl GlobalState {
9090
quiescent: self.is_quiescent(),
9191
message: None,
9292
};
93+
let mut message = String::new();
9394

9495
if self.proc_macro_changed {
9596
status.health = lsp_ext::Health::Warning;
96-
status.message =
97-
Some("Reload required due to source changes of a procedural macro.".into())
97+
message.push_str("Reload required due to source changes of a procedural macro.\n\n");
9898
}
9999
if let Err(_) = self.fetch_build_data_error() {
100100
status.health = lsp_ext::Health::Warning;
101-
status.message =
102-
Some("Failed to run build scripts of some packages, check the logs.".to_string());
101+
message.push_str("Failed to run build scripts of some packages.\n\n");
103102
}
104103
if !self.config.cargo_autoreload()
105104
&& self.is_quiescent()
106105
&& self.fetch_workspaces_queue.op_requested()
107106
{
108107
status.health = lsp_ext::Health::Warning;
109-
status.message = Some("Workspace reload required".to_string())
108+
message.push_str("Auto-reloading is disabled, a workspace reload required.\n\n");
110109
}
111-
112-
if let Err(_) = self.fetch_workspace_error() {
113-
status.health = lsp_ext::Health::Error;
114-
status.message = Some("Failed to load workspaces".to_string())
115-
}
116-
117110
if self.config.linked_projects().is_empty()
118111
&& self.config.detached_files().is_empty()
119112
&& self.config.notifications().cargo_toml_not_found
120113
{
121114
status.health = lsp_ext::Health::Warning;
122-
status.message = Some("Failed to discover workspace".to_string())
115+
message.push_str("Failed to discover workspace.\n\n");
123116
}
124117

118+
if let Err(_) = self.fetch_workspace_error() {
119+
status.health = lsp_ext::Health::Error;
120+
message.push_str("Failed to load workspaces\n\n");
121+
}
122+
123+
if !message.is_empty() {
124+
status.message = Some(message.trim_end().to_owned());
125+
}
125126
status
126127
}
127128

0 commit comments

Comments
 (0)