Skip to content

Commit 3ef7676

Browse files
committed
Implement StatusBar
1 parent a03cfa4 commit 3ef7676

File tree

8 files changed

+93
-5
lines changed

8 files changed

+93
-5
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pub struct ClientCapsConfig {
130130
pub code_action_group: bool,
131131
pub resolve_code_action: bool,
132132
pub hover_actions: bool,
133+
pub status_notification: bool,
133134
}
134135

135136
impl Config {
@@ -365,6 +366,7 @@ impl Config {
365366
self.client_caps.code_action_group = get_bool("codeActionGroup");
366367
self.client_caps.resolve_code_action = get_bool("resolveCodeAction");
367368
self.client_caps.hover_actions = get_bool("hoverActions");
369+
self.client_caps.status_notification = get_bool("statusNotification");
368370
}
369371
}
370372
}

crates/rust-analyzer/src/global_state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::{
3131
pub(crate) enum Status {
3232
Loading,
3333
Ready,
34+
Invalid,
3435
}
3536

3637
impl Default for Status {

crates/rust-analyzer/src/lsp_ext.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::{collections::HashMap, path::PathBuf};
44

55
use lsp_types::request::Request;
6-
use lsp_types::{Position, Range, TextDocumentIdentifier};
6+
use lsp_types::{notification::Notification, Position, Range, TextDocumentIdentifier};
77
use serde::{Deserialize, Serialize};
88

99
pub enum AnalyzerStatus {}
@@ -208,6 +208,22 @@ pub struct SsrParams {
208208
pub parse_only: bool,
209209
}
210210

211+
pub enum StatusNotification {}
212+
213+
#[serde(rename_all = "camelCase")]
214+
#[derive(Serialize, Deserialize)]
215+
pub enum Status {
216+
Loading,
217+
Ready,
218+
NeedsReload,
219+
Invalid,
220+
}
221+
222+
impl Notification for StatusNotification {
223+
type Params = Status;
224+
const METHOD: &'static str = "rust-analyzer/status";
225+
}
226+
211227
pub enum CodeActionRequest {}
212228

213229
impl Request for CodeActionRequest {

crates/rust-analyzer/src/main_loop.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,16 @@ impl GlobalState {
169169
}
170170
vfs::loader::Message::Progress { n_total, n_done } => {
171171
if n_total == 0 {
172-
self.status = Status::Ready;
172+
self.transition(Status::Invalid);
173173
} else {
174174
let state = if n_done == 0 {
175-
self.status = Status::Loading;
175+
self.transition(Status::Loading);
176176
Progress::Begin
177177
} else if n_done < n_total {
178178
Progress::Report
179179
} else {
180180
assert_eq!(n_done, n_total);
181-
self.status = Status::Ready;
181+
self.transition(Status::Ready);
182182
Progress::End
183183
};
184184
self.report_progress(
@@ -274,6 +274,18 @@ impl GlobalState {
274274
Ok(())
275275
}
276276

277+
fn transition(&mut self, new_status: Status) {
278+
self.status = Status::Ready;
279+
if self.config.client_caps.status_notification {
280+
let lsp_status = match new_status {
281+
Status::Loading => lsp_ext::Status::Loading,
282+
Status::Ready => lsp_ext::Status::Ready,
283+
Status::Invalid => lsp_ext::Status::Invalid,
284+
};
285+
self.send_notification::<lsp_ext::StatusNotification>(lsp_status);
286+
}
287+
}
288+
277289
fn on_request(&mut self, request_received: Instant, req: Request) -> Result<()> {
278290
self.register_request(&req, request_received);
279291

docs/dev/lsp-extensions.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,18 @@ Returns internal status message, mostly for debugging purposes.
399399

400400
Reloads project information (that is, re-executes `cargo metadata`).
401401

402+
## Status Notification
403+
404+
**Client Capability:** `{ "statusNotification": boolean }`
405+
406+
**Method:** `rust-analyzer/status`
407+
408+
**Notification:** `"loading" | "ready" | "invalid" | "needsReload"`
409+
410+
This notification is sent from server to client.
411+
The client can use it to display persistent status to the user (in modline).
412+
For `needsReload` state, the client can provide a context-menu action to run `rust-analyzer/reloadWorkspace` request.
413+
402414
## Syntax Tree
403415

404416
**Method:** `rust-analyzer/syntaxTree`

editors/code/src/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class ExperimentalFeatures implements lc.StaticFeature {
161161
caps.codeActionGroup = true;
162162
caps.resolveCodeAction = true;
163163
caps.hoverActions = true;
164+
caps.statusNotification = true;
164165
capabilities.experimental = caps;
165166
}
166167
initialize(_capabilities: lc.ServerCapabilities<any>, _documentSelector: lc.DocumentSelector | undefined): void {

editors/code/src/ctx.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import * as vscode from 'vscode';
22
import * as lc from 'vscode-languageclient';
3+
import * as ra from './lsp_ext';
34

45
import { Config } from './config';
56
import { createClient } from './client';
67
import { isRustEditor, RustEditor } from './util';
8+
import { Status } from './lsp_ext';
79

810
export class Ctx {
911
private constructor(
1012
readonly config: Config,
1113
private readonly extCtx: vscode.ExtensionContext,
1214
readonly client: lc.LanguageClient,
1315
readonly serverPath: string,
16+
readonly statusBar: vscode.StatusBarItem,
1417
) {
1518

1619
}
@@ -22,9 +25,18 @@ export class Ctx {
2225
cwd: string,
2326
): Promise<Ctx> {
2427
const client = createClient(serverPath, cwd);
25-
const res = new Ctx(config, extCtx, client, serverPath);
28+
29+
const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
30+
extCtx.subscriptions.push(statusBar);
31+
statusBar.text = "rust-analyzer";
32+
statusBar.tooltip = "ready";
33+
statusBar.show();
34+
35+
const res = new Ctx(config, extCtx, client, serverPath, statusBar);
36+
2637
res.pushCleanup(client.start());
2738
await client.onReady();
39+
client.onNotification(ra.status, (status) => res.setStatus(status));
2840
return res;
2941
}
3042

@@ -54,6 +66,35 @@ export class Ctx {
5466
return this.extCtx.subscriptions;
5567
}
5668

69+
setStatus(status: Status) {
70+
switch (status) {
71+
case "loading":
72+
this.statusBar.text = "$(sync~spin) rust-analyzer";
73+
this.statusBar.tooltip = "Loading the project";
74+
this.statusBar.command = undefined;
75+
this.statusBar.color = undefined;
76+
break;
77+
case "ready":
78+
this.statusBar.text = "rust-analyzer";
79+
this.statusBar.tooltip = "Ready";
80+
this.statusBar.command = undefined;
81+
this.statusBar.color = undefined;
82+
break;
83+
case "invalid":
84+
this.statusBar.text = "$(error) rust-analyzer";
85+
this.statusBar.tooltip = "Failed to load the project";
86+
this.statusBar.command = undefined;
87+
this.statusBar.color = new vscode.ThemeColor("notificationsErrorIcon.foreground");
88+
break;
89+
case "needsReload":
90+
this.statusBar.text = "$(warning) rust-analyzer";
91+
this.statusBar.tooltip = "Click to reload";
92+
this.statusBar.command = "rust-analyzer.reloadWorkspace";
93+
this.statusBar.color = new vscode.ThemeColor("notificationsWarningIcon.foreground");
94+
break;
95+
}
96+
}
97+
5798
pushCleanup(d: Disposable) {
5899
this.extCtx.subscriptions.push(d);
59100
}

editors/code/src/lsp_ext.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import * as lc from "vscode-languageclient";
66

77
export const analyzerStatus = new lc.RequestType<null, string, void>("rust-analyzer/analyzerStatus");
88

9+
export type Status = "loading" | "ready" | "invalid" | "needsReload";
10+
export const status = new lc.NotificationType<Status>("rust-analyzer/status");
11+
912
export const reloadWorkspace = new lc.RequestType<null, null, void>("rust-analyzer/reloadWorkspace");
1013

1114
export interface SyntaxTreeParams {

0 commit comments

Comments
 (0)