2019-12-30 13:42:59 +00:00
|
|
|
import * as vscode from "vscode";
|
2020-09-01 16:53:07 +00:00
|
|
|
import * as lc from "vscode-languageclient/node";
|
2020-07-02 10:37:04 +00:00
|
|
|
import * as ra from "./lsp_ext";
|
2020-02-02 21:23:01 +00:00
|
|
|
|
2019-12-30 19:46:14 +00:00
|
|
|
import { Config } from "./config";
|
2020-04-02 10:47:58 +00:00
|
|
|
import { createClient } from "./client";
|
2020-03-07 12:07:44 +00:00
|
|
|
import { isRustEditor, RustEditor } from "./util";
|
2021-04-06 11:16:35 +00:00
|
|
|
import { ServerStatusParams } from "./lsp_ext";
|
2019-12-30 13:42:59 +00:00
|
|
|
|
2021-05-23 13:22:13 +00:00
|
|
|
export type Workspace =
|
|
|
|
| {
|
2021-05-23 20:47:58 +00:00
|
|
|
kind: "Workspace Folder";
|
2022-05-17 17:15:06 +00:00
|
|
|
}
|
|
|
|
| {
|
2021-05-23 20:47:58 +00:00
|
|
|
kind: "Detached Files";
|
|
|
|
files: vscode.TextDocument[];
|
2021-05-23 13:22:13 +00:00
|
|
|
};
|
|
|
|
|
2019-12-30 13:42:59 +00:00
|
|
|
export class Ctx {
|
2020-02-17 13:21:50 +00:00
|
|
|
private constructor(
|
|
|
|
readonly config: Config,
|
|
|
|
private readonly extCtx: vscode.ExtensionContext,
|
2020-03-17 11:44:31 +00:00
|
|
|
readonly client: lc.LanguageClient,
|
|
|
|
readonly serverPath: string,
|
2020-07-02 10:37:04 +00:00
|
|
|
readonly statusBar: vscode.StatusBarItem
|
2020-02-17 13:21:50 +00:00
|
|
|
) {}
|
2019-12-30 13:42:59 +00:00
|
|
|
|
2020-03-30 17:12:22 +00:00
|
|
|
static async create(
|
|
|
|
config: Config,
|
|
|
|
extCtx: vscode.ExtensionContext,
|
|
|
|
serverPath: string,
|
2021-05-23 13:22:13 +00:00
|
|
|
workspace: Workspace
|
2020-03-30 17:12:22 +00:00
|
|
|
): Promise<Ctx> {
|
2022-04-29 11:34:03 +00:00
|
|
|
const client = await createClient(serverPath, workspace, config.serverExtraEnv);
|
2020-07-02 10:37:04 +00:00
|
|
|
|
|
|
|
const statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
|
|
|
|
extCtx.subscriptions.push(statusBar);
|
|
|
|
statusBar.text = "rust-analyzer";
|
|
|
|
statusBar.tooltip = "ready";
|
2021-06-07 16:56:40 +00:00
|
|
|
statusBar.command = "rust-analyzer.analyzerStatus";
|
2020-07-02 10:37:04 +00:00
|
|
|
statusBar.show();
|
|
|
|
|
|
|
|
const res = new Ctx(config, extCtx, client, serverPath, statusBar);
|
|
|
|
|
2022-07-22 22:14:34 +00:00
|
|
|
res.pushCleanup(client.start());
|
|
|
|
await client.onReady();
|
2021-04-06 11:16:35 +00:00
|
|
|
client.onNotification(ra.serverStatus, (params) => res.setServerStatus(params));
|
2020-02-17 13:11:01 +00:00
|
|
|
return res;
|
|
|
|
}
|
2019-12-31 17:14:00 +00:00
|
|
|
|
2020-03-07 12:07:44 +00:00
|
|
|
get activeRustEditor(): RustEditor | undefined {
|
2019-12-30 14:20:13 +00:00
|
|
|
const editor = vscode.window.activeTextEditor;
|
2020-03-07 12:07:44 +00:00
|
|
|
return editor && isRustEditor(editor) ? editor : undefined;
|
2019-12-30 14:20:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 12:07:44 +00:00
|
|
|
get visibleRustEditors(): RustEditor[] {
|
|
|
|
return vscode.window.visibleTextEditors.filter(isRustEditor);
|
2020-03-02 21:54:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 14:11:30 +00:00
|
|
|
registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
|
|
|
|
const fullName = `rust-analyzer.${name}`;
|
2019-12-30 13:42:59 +00:00
|
|
|
const cmd = factory(this);
|
|
|
|
const d = vscode.commands.registerCommand(fullName, cmd);
|
|
|
|
this.pushCleanup(d);
|
|
|
|
}
|
|
|
|
|
2021-08-10 13:35:37 +00:00
|
|
|
get extensionPath(): string {
|
|
|
|
return this.extCtx.extensionPath;
|
|
|
|
}
|
|
|
|
|
2020-02-16 01:08:36 +00:00
|
|
|
get globalState(): vscode.Memento {
|
|
|
|
return this.extCtx.globalState;
|
|
|
|
}
|
|
|
|
|
2020-02-02 21:23:01 +00:00
|
|
|
get subscriptions(): Disposable[] {
|
2019-12-30 18:05:41 +00:00
|
|
|
return this.extCtx.subscriptions;
|
|
|
|
}
|
|
|
|
|
2021-04-06 11:16:35 +00:00
|
|
|
setServerStatus(status: ServerStatusParams) {
|
|
|
|
let icon = "";
|
2022-07-13 11:16:35 +00:00
|
|
|
const statusBar = this.statusBar;
|
2021-04-06 11:16:35 +00:00
|
|
|
switch (status.health) {
|
|
|
|
case "ok":
|
2022-07-13 11:16:35 +00:00
|
|
|
statusBar.tooltip = status.message ?? "Ready";
|
|
|
|
statusBar.command = undefined;
|
|
|
|
statusBar.color = undefined;
|
|
|
|
statusBar.backgroundColor = undefined;
|
2020-07-02 10:37:04 +00:00
|
|
|
break;
|
2021-04-06 11:16:35 +00:00
|
|
|
case "warning":
|
2022-07-13 11:16:35 +00:00
|
|
|
statusBar.tooltip =
|
|
|
|
(status.message ? status.message + "\n" : "") + "Click to reload.";
|
|
|
|
|
|
|
|
statusBar.command = "rust-analyzer.reloadWorkspace";
|
|
|
|
statusBar.color = new vscode.ThemeColor("statusBarItem.warningForeground");
|
|
|
|
statusBar.backgroundColor = new vscode.ThemeColor(
|
2022-05-26 10:31:07 +00:00
|
|
|
"statusBarItem.warningBackground"
|
|
|
|
);
|
2021-04-06 11:16:35 +00:00
|
|
|
icon = "$(warning) ";
|
|
|
|
break;
|
|
|
|
case "error":
|
2022-07-22 22:52:12 +00:00
|
|
|
statusBar.tooltip =
|
|
|
|
(status.message ? status.message + "\n" : "") + "Click to reload.";
|
|
|
|
|
2022-07-13 11:16:35 +00:00
|
|
|
statusBar.command = "rust-analyzer.reloadWorkspace";
|
|
|
|
statusBar.color = new vscode.ThemeColor("statusBarItem.errorForeground");
|
|
|
|
statusBar.backgroundColor = new vscode.ThemeColor("statusBarItem.errorBackground");
|
2021-04-06 11:16:35 +00:00
|
|
|
icon = "$(error) ";
|
2020-07-02 10:37:04 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-04-06 11:16:35 +00:00
|
|
|
if (!status.quiescent) icon = "$(sync~spin) ";
|
2022-07-13 11:16:35 +00:00
|
|
|
statusBar.text = `${icon}rust-analyzer`;
|
2020-07-02 10:37:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 21:23:01 +00:00
|
|
|
pushCleanup(d: Disposable) {
|
2019-12-30 14:11:30 +00:00
|
|
|
this.extCtx.subscriptions.push(d);
|
2019-12-30 13:42:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-30 13:53:43 +00:00
|
|
|
|
2020-02-02 21:23:01 +00:00
|
|
|
export interface Disposable {
|
|
|
|
dispose(): void;
|
|
|
|
}
|
2020-02-02 20:36:12 +00:00
|
|
|
export type Cmd = (...args: any[]) => unknown;
|