1 import * as vscode from "vscode";
2 import * as lc from "vscode-languageclient/node";
3 import * as ra from "./lsp_ext";
5 import { Config } from "./config";
6 import { createClient } from "./client";
7 import { isRustEditor, RustEditor } from "./util";
8 import { ServerStatusParams } from "./lsp_ext";
10 export type Workspace =
12 kind: "Workspace Folder";
15 kind: "Detached Files";
16 files: vscode.TextDocument[];
21 readonly config: Config,
22 private readonly extCtx: vscode.ExtensionContext,
23 readonly client: lc.LanguageClient,
24 readonly serverPath: string,
25 readonly statusBar: vscode.StatusBarItem
30 extCtx: vscode.ExtensionContext,
34 const client = await createClient(serverPath, workspace, config.serverExtraEnv);
36 const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
37 extCtx.subscriptions.push(statusBar);
38 statusBar.text = "rust-analyzer";
39 statusBar.tooltip = "ready";
40 statusBar.command = "rust-analyzer.analyzerStatus";
43 const res = new Ctx(config, extCtx, client, serverPath, statusBar);
45 res.pushCleanup(client.start());
46 await client.onReady();
47 client.onNotification(ra.serverStatus, (params) => res.setServerStatus(params));
51 get activeRustEditor(): RustEditor | undefined {
52 const editor = vscode.window.activeTextEditor;
53 return editor && isRustEditor(editor) ? editor : undefined;
56 get visibleRustEditors(): RustEditor[] {
57 return vscode.window.visibleTextEditors.filter(isRustEditor);
60 registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
61 const fullName = `rust-analyzer.${name}`;
62 const cmd = factory(this);
63 const d = vscode.commands.registerCommand(fullName, cmd);
67 get extensionPath(): string {
68 return this.extCtx.extensionPath;
71 get globalState(): vscode.Memento {
72 return this.extCtx.globalState;
75 get subscriptions(): Disposable[] {
76 return this.extCtx.subscriptions;
79 setServerStatus(status: ServerStatusParams) {
81 const statusBar = this.statusBar;
82 switch (status.health) {
84 statusBar.tooltip = status.message ?? "Ready";
85 statusBar.command = undefined;
86 statusBar.color = undefined;
87 statusBar.backgroundColor = undefined;
91 (status.message ? status.message + "\n" : "") + "Click to reload.";
93 statusBar.command = "rust-analyzer.reloadWorkspace";
94 statusBar.color = new vscode.ThemeColor("statusBarItem.warningForeground");
95 statusBar.backgroundColor = new vscode.ThemeColor(
96 "statusBarItem.warningBackground"
102 (status.message ? status.message + "\n" : "") + "Click to reload.";
104 statusBar.command = "rust-analyzer.reloadWorkspace";
105 statusBar.color = new vscode.ThemeColor("statusBarItem.errorForeground");
106 statusBar.backgroundColor = new vscode.ThemeColor("statusBarItem.errorBackground");
110 if (!status.quiescent) icon = "$(sync~spin) ";
111 statusBar.text = `${icon}rust-analyzer`;
114 pushCleanup(d: Disposable) {
115 this.extCtx.subscriptions.push(d);
119 export interface Disposable {
122 export type Cmd = (...args: any[]) => unknown;