]> git.lizzy.rs Git - rust.git/blob - editors/code/src/ctx.ts
Move matching brace to new Ctx
[rust.git] / editors / code / src / ctx.ts
1 import * as vscode from 'vscode';
2 import * as lc from 'vscode-languageclient';
3 import { Server } from './server';
4
5 export class Ctx {
6     private extCtx: vscode.ExtensionContext;
7
8     constructor(extCtx: vscode.ExtensionContext) {
9         this.extCtx = extCtx;
10     }
11
12     get client(): lc.LanguageClient {
13         return Server.client;
14     }
15
16     get activeRustEditor(): vscode.TextEditor | undefined {
17         const editor = vscode.window.activeTextEditor;
18         return editor && editor.document.languageId === 'rust'
19             ? editor
20             : undefined;
21     }
22
23     registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
24         const fullName = `rust-analyzer.${name}`;
25         const cmd = factory(this);
26         const d = vscode.commands.registerCommand(fullName, cmd);
27         this.pushCleanup(d);
28     }
29
30     pushCleanup(d: { dispose(): any }) {
31         this.extCtx.subscriptions.push(d);
32     }
33 }
34
35 export type Cmd = (...args: any[]) => any;