1 import * as vscode from 'vscode';
2 import * as lc from 'vscode-languageclient';
4 import { Config } from './config';
5 import { createClient } from './client';
6 import { isRustEditor, RustEditor } from './util';
7 import { PersistentState } from './persistent_state';
11 readonly config: Config,
12 readonly state: PersistentState,
13 private readonly extCtx: vscode.ExtensionContext,
14 readonly client: lc.LanguageClient
19 static async create(config: Config, state: PersistentState, extCtx: vscode.ExtensionContext, serverPath: string): Promise<Ctx> {
20 const client = await createClient(config, serverPath);
21 const res = new Ctx(config, state, extCtx, client);
22 res.pushCleanup(client.start());
23 await client.onReady();
27 get activeRustEditor(): RustEditor | undefined {
28 const editor = vscode.window.activeTextEditor;
29 return editor && isRustEditor(editor)
34 get visibleRustEditors(): RustEditor[] {
35 return vscode.window.visibleTextEditors.filter(isRustEditor);
38 registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
39 const fullName = `rust-analyzer.${name}`;
40 const cmd = factory(this);
41 const d = vscode.commands.registerCommand(fullName, cmd);
45 get globalState(): vscode.Memento {
46 return this.extCtx.globalState;
49 get subscriptions(): Disposable[] {
50 return this.extCtx.subscriptions;
53 pushCleanup(d: Disposable) {
54 this.extCtx.subscriptions.push(d);
58 export interface Disposable {
61 export type Cmd = (...args: any[]) => unknown;