2019-12-30 13:42:59 +00:00
|
|
|
import * as vscode from 'vscode';
|
|
|
|
import * as lc from 'vscode-languageclient';
|
2020-02-02 21:23:01 +00:00
|
|
|
|
2019-12-30 19:46:14 +00:00
|
|
|
import { Config } from './config';
|
2020-03-19 22:42:18 +00:00
|
|
|
import { createClient, configToOptions } from './client';
|
2020-03-07 12:07:44 +00:00
|
|
|
import { isRustEditor, RustEditor } from './util';
|
2019-12-30 13:42:59 +00:00
|
|
|
|
|
|
|
export class Ctx {
|
2020-02-17 13:21:50 +00:00
|
|
|
private constructor(
|
|
|
|
readonly config: Config,
|
|
|
|
private readonly extCtx: vscode.ExtensionContext,
|
2020-03-17 11:44:31 +00:00
|
|
|
readonly client: lc.LanguageClient,
|
|
|
|
readonly serverPath: string,
|
2020-02-17 13:21:50 +00:00
|
|
|
) {
|
|
|
|
|
|
|
|
}
|
2019-12-30 13:42:59 +00:00
|
|
|
|
2020-03-17 11:44:31 +00:00
|
|
|
static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise<Ctx> {
|
2020-02-17 13:11:01 +00:00
|
|
|
const client = await createClient(config, serverPath);
|
2020-03-17 11:44:31 +00:00
|
|
|
const res = new Ctx(config, extCtx, client, serverPath);
|
2020-02-17 13:11:01 +00:00
|
|
|
res.pushCleanup(client.start());
|
2019-12-31 17:14:00 +00:00
|
|
|
await client.onReady();
|
2020-03-19 22:42:18 +00:00
|
|
|
client.onRequest('workspace/configuration', _ => [configToOptions(config)]);
|
2020-02-17 13:11:01 +00:00
|
|
|
return res;
|
|
|
|
}
|
2019-12-31 17:14:00 +00:00
|
|
|
|
2020-03-07 12:07:44 +00:00
|
|
|
get activeRustEditor(): RustEditor | undefined {
|
2019-12-30 14:20:13 +00:00
|
|
|
const editor = vscode.window.activeTextEditor;
|
2020-03-07 12:07:44 +00:00
|
|
|
return editor && isRustEditor(editor)
|
2019-12-30 14:20:13 +00:00
|
|
|
? editor
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
2020-03-07 12:07:44 +00:00
|
|
|
get visibleRustEditors(): RustEditor[] {
|
|
|
|
return vscode.window.visibleTextEditors.filter(isRustEditor);
|
2020-03-02 21:54:29 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-02-16 01:08:36 +00:00
|
|
|
get globalState(): vscode.Memento {
|
|
|
|
return this.extCtx.globalState;
|
|
|
|
}
|
|
|
|
|
2020-02-02 21:23:01 +00:00
|
|
|
get subscriptions(): Disposable[] {
|
2019-12-30 18:05:41 +00:00
|
|
|
return this.extCtx.subscriptions;
|
|
|
|
}
|
|
|
|
|
2020-02-02 21:23:01 +00:00
|
|
|
pushCleanup(d: Disposable) {
|
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
|
|
|
|
2020-02-02 21:23:01 +00:00
|
|
|
export interface Disposable {
|
|
|
|
dispose(): void;
|
|
|
|
}
|
2020-02-02 20:36:12 +00:00
|
|
|
export type Cmd = (...args: any[]) => unknown;
|