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