Skip to content

Commit 070f8f8

Browse files
committed
2 parents 1b5bba4 + c03775e commit 070f8f8

File tree

5 files changed

+68
-45
lines changed

5 files changed

+68
-45
lines changed

crates/rust-analyzer/src/main_loop.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,7 @@ impl fmt::Debug for Event {
111111

112112
impl GlobalState {
113113
fn run(mut self, inbox: Receiver<lsp_server::Message>) -> Result<()> {
114-
if self.config.linked_projects().is_empty()
115-
&& self.config.detached_files().is_empty()
116-
&& self.config.notifications().cargo_toml_not_found
117-
{
118-
self.show_and_log_error("rust-analyzer failed to discover workspace".to_string(), None);
119-
};
114+
self.update_status_or_notify();
120115

121116
if self.config.did_save_text_document_dynamic_registration() {
122117
let save_registration_options = lsp_types::TextDocumentSaveRegistrationOptions {
@@ -394,25 +389,28 @@ impl GlobalState {
394389
});
395390
}
396391

392+
self.update_status_or_notify();
393+
394+
let loop_duration = loop_start.elapsed();
395+
if loop_duration > Duration::from_millis(100) && was_quiescent {
396+
tracing::warn!("overly long loop turn: {:?}", loop_duration);
397+
self.poke_rust_analyzer_developer(format!("overly long loop turn: {loop_duration:?}"));
398+
}
399+
Ok(())
400+
}
401+
402+
fn update_status_or_notify(&mut self) {
397403
let status = self.current_status();
398404
if self.last_reported_status.as_ref() != Some(&status) {
399405
self.last_reported_status = Some(status.clone());
400406

401407
if self.config.server_status_notification() {
402408
self.send_notification::<lsp_ext::ServerStatusNotification>(status);
403-
} else {
404-
if let (lsp_ext::Health::Error, Some(message)) = (status.health, &status.message) {
405-
self.show_message(lsp_types::MessageType::ERROR, message.clone());
406-
}
409+
} else if let (lsp_ext::Health::Error, Some(message)) = (status.health, &status.message)
410+
{
411+
self.show_and_log_error(message.clone(), None);
407412
}
408413
}
409-
410-
let loop_duration = loop_start.elapsed();
411-
if loop_duration > Duration::from_millis(100) && was_quiescent {
412-
tracing::warn!("overly long loop turn: {:?}", loop_duration);
413-
self.poke_rust_analyzer_developer(format!("overly long loop turn: {loop_duration:?}"));
414-
}
415-
Ok(())
416414
}
417415

418416
fn handle_task(&mut self, prime_caches_progress: &mut Vec<PrimeCachesProgress>, task: Task) {
@@ -445,6 +443,9 @@ impl GlobalState {
445443
ProjectWorkspaceProgress::Report(msg) => (Progress::Report, Some(msg)),
446444
ProjectWorkspaceProgress::End(workspaces) => {
447445
self.fetch_workspaces_queue.op_completed(Some(workspaces));
446+
if let Err(e) = self.fetch_workspace_error() {
447+
tracing::error!("FetchWorkspaceError:\n{e}");
448+
}
448449

449450
let old = Arc::clone(&self.workspaces);
450451
self.switch_workspaces("fetched workspace".to_string());
@@ -466,6 +467,9 @@ impl GlobalState {
466467
BuildDataProgress::Report(msg) => (Some(Progress::Report), Some(msg)),
467468
BuildDataProgress::End(build_data_result) => {
468469
self.fetch_build_data_queue.op_completed(build_data_result);
470+
if let Err(e) = self.fetch_build_data_error() {
471+
tracing::error!("FetchBuildDataError:\n{e}");
472+
}
469473

470474
self.switch_workspaces("fetched build data".to_string());
471475

@@ -498,6 +502,7 @@ impl GlobalState {
498502
self.vfs_progress_n_total = n_total;
499503
self.vfs_progress_n_done = n_done;
500504

505+
// if n_total != 0 {
501506
let state = if n_done == 0 {
502507
Progress::Begin
503508
} else if n_done < n_total {
@@ -512,7 +517,8 @@ impl GlobalState {
512517
Some(format!("{n_done}/{n_total}")),
513518
Some(Progress::fraction(n_done, n_total)),
514519
None,
515-
)
520+
);
521+
// }
516522
}
517523
}
518524
}
@@ -554,7 +560,10 @@ impl GlobalState {
554560
flycheck::Progress::DidCheckCrate(target) => (Progress::Report, Some(target)),
555561
flycheck::Progress::DidCancel => (Progress::End, None),
556562
flycheck::Progress::DidFailToRestart(err) => {
557-
self.show_and_log_error("cargo check failed".to_string(), Some(err));
563+
self.show_and_log_error(
564+
"cargo check failed to start".to_string(),
565+
Some(err),
566+
);
558567
return;
559568
}
560569
flycheck::Progress::DidFinish(result) => {

crates/rust-analyzer/src/reload.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ pub(crate) enum BuildDataProgress {
5656

5757
impl GlobalState {
5858
pub(crate) fn is_quiescent(&self) -> bool {
59-
!(self.fetch_workspaces_queue.op_in_progress()
59+
!(self.last_reported_status.is_none()
60+
|| self.fetch_workspaces_queue.op_in_progress()
6061
|| self.fetch_build_data_queue.op_in_progress()
6162
|| self.vfs_progress_config_version < self.vfs_config_version
6263
|| self.vfs_progress_n_done < self.vfs_progress_n_total)
@@ -108,18 +109,19 @@ impl GlobalState {
108109
status.message = Some("Workspace reload required".to_string())
109110
}
110111

111-
if let Err(error) = self.fetch_workspace_error() {
112+
if let Err(_) = self.fetch_workspace_error() {
112113
status.health = lsp_ext::Health::Error;
113-
status.message = Some(error)
114+
status.message = Some("Failed to load workspaces".to_string())
114115
}
115116

116117
if self.config.linked_projects().is_empty()
117118
&& self.config.detached_files().is_empty()
118119
&& self.config.notifications().cargo_toml_not_found
119120
{
120121
status.health = lsp_ext::Health::Warning;
121-
status.message = Some("Workspace reload required".to_string())
122+
status.message = Some("Failed to discover workspace".to_string())
122123
}
124+
123125
status
124126
}
125127

@@ -201,21 +203,14 @@ impl GlobalState {
201203
let _p = profile::span("GlobalState::switch_workspaces");
202204
tracing::info!(%cause, "will switch workspaces");
203205

204-
if let Err(error_message) = self.fetch_workspace_error() {
205-
if !self.config.server_status_notification() {
206-
self.show_and_log_error(error_message, None);
207-
}
206+
if let Err(_) = self.fetch_workspace_error() {
208207
if !self.workspaces.is_empty() {
209208
// It only makes sense to switch to a partially broken workspace
210209
// if we don't have any workspace at all yet.
211210
return;
212211
}
213212
}
214213

215-
if let Err(error) = self.fetch_build_data_error() {
216-
self.show_and_log_error("failed to run build scripts".to_string(), Some(error));
217-
}
218-
219214
let Some(workspaces) = self.fetch_workspaces_queue.last_op_result() else { return; };
220215
let workspaces =
221216
workspaces.iter().filter_map(|res| res.as_ref().ok().cloned()).collect::<Vec<_>>();
@@ -394,7 +389,7 @@ impl GlobalState {
394389
tracing::info!("did switch workspaces");
395390
}
396391

397-
fn fetch_workspace_error(&self) -> Result<(), String> {
392+
pub(super) fn fetch_workspace_error(&self) -> Result<(), String> {
398393
let mut buf = String::new();
399394

400395
let Some(last_op_result) = self.fetch_workspaces_queue.last_op_result() else { return Ok(()) };
@@ -415,7 +410,7 @@ impl GlobalState {
415410
Err(buf)
416411
}
417412

418-
fn fetch_build_data_error(&self) -> Result<(), String> {
413+
pub(super) fn fetch_build_data_error(&self) -> Result<(), String> {
419414
let mut buf = String::new();
420415

421416
for ws in &self.fetch_build_data_queue.last_op_result().1 {

editors/code/src/commands.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ export function triggerParameterHints(_: CtxInit): Cmd {
9393
};
9494
}
9595

96+
export function openLogs(ctx: CtxInit): Cmd {
97+
return async () => {
98+
if (ctx.client.outputChannel) {
99+
ctx.client.outputChannel.show();
100+
}
101+
};
102+
}
103+
96104
export function matchingBrace(ctx: CtxInit): Cmd {
97105
return async () => {
98106
const editor = ctx.activeRustEditor;

editors/code/src/ctx.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,41 +282,51 @@ export class Ctx {
282282
setServerStatus(status: ServerStatusParams | { health: "stopped" }) {
283283
let icon = "";
284284
const statusBar = this.statusBar;
285+
statusBar.tooltip = new vscode.MarkdownString("", true);
286+
statusBar.tooltip.isTrusted = true;
285287
switch (status.health) {
286288
case "ok":
287-
statusBar.tooltip = (status.message ?? "Ready") + "\nClick to stop server.";
288-
statusBar.command = "rust-analyzer.stopServer";
289+
statusBar.tooltip.appendText(status.message ?? "Ready");
289290
statusBar.color = undefined;
290291
statusBar.backgroundColor = undefined;
291292
break;
292293
case "warning":
293-
statusBar.tooltip =
294-
(status.message ? status.message + "\n" : "") + "Click to reload.";
295-
296-
statusBar.command = "rust-analyzer.reloadWorkspace";
294+
if (status.message) {
295+
statusBar.tooltip.appendText(status.message);
296+
}
297297
statusBar.color = new vscode.ThemeColor("statusBarItem.warningForeground");
298298
statusBar.backgroundColor = new vscode.ThemeColor(
299299
"statusBarItem.warningBackground"
300300
);
301301
icon = "$(warning) ";
302302
break;
303303
case "error":
304-
statusBar.tooltip =
305-
(status.message ? status.message + "\n" : "") + "Click to reload.";
306-
307-
statusBar.command = "rust-analyzer.reloadWorkspace";
304+
if (status.message) {
305+
statusBar.tooltip.appendText(status.message);
306+
}
308307
statusBar.color = new vscode.ThemeColor("statusBarItem.errorForeground");
309308
statusBar.backgroundColor = new vscode.ThemeColor("statusBarItem.errorBackground");
310309
icon = "$(error) ";
311310
break;
312311
case "stopped":
313-
statusBar.tooltip = "Server is stopped.\nClick to start.";
314-
statusBar.command = "rust-analyzer.startServer";
312+
statusBar.tooltip.appendText("Server is stopped");
313+
statusBar.tooltip.appendMarkdown(
314+
"\n\n[Start server](command:rust-analyzer.startServer)"
315+
);
315316
statusBar.color = undefined;
316317
statusBar.backgroundColor = undefined;
317318
statusBar.text = `$(stop-circle) rust-analyzer`;
318319
return;
319320
}
321+
if (statusBar.tooltip.value) {
322+
statusBar.tooltip.appendText("\n\n");
323+
}
324+
statusBar.tooltip.appendMarkdown("[Stop server](command:rust-analyzer.stopServer)");
325+
statusBar.tooltip.appendMarkdown(
326+
"\n\n[Reload Workspace](command:rust-analyzer.reloadWorkspace)"
327+
);
328+
statusBar.tooltip.appendMarkdown("\n\n[Restart server](command:rust-analyzer.startServer)");
329+
statusBar.tooltip.appendMarkdown("\n\n[Open logs](command:rust-analyzer.openLogs)");
320330
if (!status.quiescent) icon = "$(sync~spin) ";
321331
statusBar.text = `${icon}rust-analyzer`;
322332
}

editors/code/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,5 +188,6 @@ function createCommands(): Record<string, CommandFactory> {
188188
runSingle: { enabled: commands.runSingle },
189189
showReferences: { enabled: commands.showReferences },
190190
triggerParameterHints: { enabled: commands.triggerParameterHints },
191+
openLogs: { enabled: commands.openLogs },
191192
};
192193
}

0 commit comments

Comments
 (0)