rust-analyzer/editors/code/src/ctx.ts

36 lines
961 B
TypeScript
Raw Normal View History

2019-12-30 13:42:59 +00:00
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';
import { Server } from './server';
export class Ctx {
2019-12-30 14:11:30 +00:00
private extCtx: vscode.ExtensionContext;
2019-12-30 13:42:59 +00:00
constructor(extCtx: vscode.ExtensionContext) {
2019-12-30 14:11:30 +00:00
this.extCtx = extCtx;
2019-12-30 13:42:59 +00:00
}
get client(): lc.LanguageClient {
2019-12-30 14:11:30 +00:00
return Server.client;
2019-12-30 13:42:59 +00:00
}
2019-12-30 14:20:13 +00:00
get activeRustEditor(): vscode.TextEditor | undefined {
const editor = vscode.window.activeTextEditor;
return editor && editor.document.languageId === 'rust'
? editor
: undefined;
}
2019-12-30 14:11:30 +00:00
registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
const fullName = `rust-analyzer.${name}`;
2019-12-30 13:42:59 +00:00
const cmd = factory(this);
const d = vscode.commands.registerCommand(fullName, cmd);
this.pushCleanup(d);
}
pushCleanup(d: { dispose(): any }) {
2019-12-30 14:11:30 +00:00
this.extCtx.subscriptions.push(d);
2019-12-30 13:42:59 +00:00
}
}
2019-12-30 13:53:43 +00:00
export type Cmd = (...args: any[]) => any;