]> git.lizzy.rs Git - rust.git/blob - editors/code/src/ctx.ts
Enable the SemanticTokensFeature by default
[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(
19         config: Config,
20         extCtx: vscode.ExtensionContext,
21         serverPath: string,
22         cwd: string,
23     ): Promise<Ctx> {
24         const client = await createClient(serverPath, cwd);
25         const res = new Ctx(config, extCtx, client, serverPath);
26         res.pushCleanup(client.start());
27         await client.onReady();
28         return res;
29     }
30
31     get activeRustEditor(): RustEditor | undefined {
32         const editor = vscode.window.activeTextEditor;
33         return editor && isRustEditor(editor)
34             ? editor
35             : undefined;
36     }
37
38     get visibleRustEditors(): RustEditor[] {
39         return vscode.window.visibleTextEditors.filter(isRustEditor);
40     }
41
42     registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
43         const fullName = `rust-analyzer.${name}`;
44         const cmd = factory(this);
45         const d = vscode.commands.registerCommand(fullName, cmd);
46         this.pushCleanup(d);
47     }
48
49     get globalState(): vscode.Memento {
50         return this.extCtx.globalState;
51     }
52
53     get subscriptions(): Disposable[] {
54         return this.extCtx.subscriptions;
55     }
56
57     pushCleanup(d: Disposable) {
58         this.extCtx.subscriptions.push(d);
59     }
60 }
61
62 export interface Disposable {
63     dispose(): void;
64 }
65 export type Cmd = (...args: any[]) => unknown;