rust/editors/code/src/ctx.ts

112 lines
3.4 KiB
TypeScript
Raw Normal View History

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