mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-28 22:13:39 +00:00
65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
import * as vscode from 'vscode';
|
|
import * as lc from 'vscode-languageclient';
|
|
|
|
import { Config } from './config';
|
|
import { createClient } from './client';
|
|
import { isRustEditor, RustEditor } from './util';
|
|
|
|
export class Ctx {
|
|
private constructor(
|
|
readonly config: Config,
|
|
private readonly extCtx: vscode.ExtensionContext,
|
|
readonly client: lc.LanguageClient,
|
|
readonly serverPath: string,
|
|
) {
|
|
|
|
}
|
|
|
|
static async create(
|
|
config: Config,
|
|
extCtx: vscode.ExtensionContext,
|
|
serverPath: string,
|
|
cwd: string,
|
|
): Promise<Ctx> {
|
|
const client = createClient(serverPath, cwd);
|
|
const res = new Ctx(config, extCtx, client, serverPath);
|
|
res.pushCleanup(client.start());
|
|
await client.onReady();
|
|
return res;
|
|
}
|
|
|
|
get activeRustEditor(): RustEditor | undefined {
|
|
const editor = vscode.window.activeTextEditor;
|
|
return editor && isRustEditor(editor)
|
|
? editor
|
|
: undefined;
|
|
}
|
|
|
|
get visibleRustEditors(): RustEditor[] {
|
|
return vscode.window.visibleTextEditors.filter(isRustEditor);
|
|
}
|
|
|
|
registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
|
|
const fullName = `rust-analyzer.${name}`;
|
|
const cmd = factory(this);
|
|
const d = vscode.commands.registerCommand(fullName, cmd);
|
|
this.pushCleanup(d);
|
|
}
|
|
|
|
get globalState(): vscode.Memento {
|
|
return this.extCtx.globalState;
|
|
}
|
|
|
|
get subscriptions(): Disposable[] {
|
|
return this.extCtx.subscriptions;
|
|
}
|
|
|
|
pushCleanup(d: Disposable) {
|
|
this.extCtx.subscriptions.push(d);
|
|
}
|
|
}
|
|
|
|
export interface Disposable {
|
|
dispose(): void;
|
|
}
|
|
export type Cmd = (...args: any[]) => unknown;
|