]> git.lizzy.rs Git - rust.git/blob - editors/code/src/ctx.ts
Rewrite auto-update
[rust.git] / editors / code / src / ctx.ts
1 import * as vscode from 'vscode';
2 import * as lc from 'vscode-languageclient';
3
4 import { Config } from './config';
5 import { createClient } from './client';
6 import { isRustEditor, RustEditor } from './util';
7
8 export class Ctx {
9     private constructor(
10         readonly config: Config,
11         private readonly extCtx: vscode.ExtensionContext,
12         readonly client: lc.LanguageClient,
13         readonly serverPath: string,
14     ) {
15
16     }
17
18     static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise<Ctx> {
19         const client = await createClient(config, serverPath);
20         const res = new Ctx(config, extCtx, client, serverPath);
21         res.pushCleanup(client.start());
22         await client.onReady();
23         return res;
24     }
25
26     get activeRustEditor(): RustEditor | undefined {
27         const editor = vscode.window.activeTextEditor;
28         return editor && isRustEditor(editor)
29             ? editor
30             : undefined;
31     }
32
33     get visibleRustEditors(): RustEditor[] {
34         return vscode.window.visibleTextEditors.filter(isRustEditor);
35     }
36
37     registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
38         const fullName = `rust-analyzer.${name}`;
39         const cmd = factory(this);
40         const d = vscode.commands.registerCommand(fullName, cmd);
41         this.pushCleanup(d);
42     }
43
44     get globalState(): vscode.Memento {
45         return this.extCtx.globalState;
46     }
47
48     get subscriptions(): Disposable[] {
49         return this.extCtx.subscriptions;
50     }
51
52     pushCleanup(d: Disposable) {
53         this.extCtx.subscriptions.push(d);
54     }
55 }
56
57 export interface Disposable {
58     dispose(): void;
59 }
60 export type Cmd = (...args: any[]) => unknown;