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

86 lines
2.7 KiB
TypeScript
Raw Normal View History

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';
2019-12-31 17:55:34 +00:00
import { createClient } from './client';
2019-12-30 13:42:59 +00:00
export class Ctx {
2019-12-31 16:34:52 +00:00
readonly config: Config;
2019-12-31 17:14:00 +00:00
// Because we have "reload server" action, various listeners **will** face a
// situation where the client is not ready yet, and should be prepared to
// deal with it.
//
// Ideally, this should be replaced with async getter though.
// FIXME: this actually needs syncronization of some kind (check how
// vscode deals with `deactivate()` call when extension has some work scheduled
// on the event loop to get a better picture of what we can do here)
2020-02-17 13:11:01 +00:00
client: lc.LanguageClient;
2019-12-30 14:11:30 +00:00
private extCtx: vscode.ExtensionContext;
2019-12-30 13:42:59 +00:00
2020-02-17 13:11:01 +00:00
static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise<Ctx> {
const client = await createClient(config, serverPath);
const res = new Ctx(config, extCtx, client);
res.pushCleanup(client.start());
2019-12-31 17:14:00 +00:00
await client.onReady();
2020-02-17 13:11:01 +00:00
return res;
}
2019-12-31 17:14:00 +00:00
2020-02-17 13:11:01 +00:00
private constructor(config: Config, extCtx: vscode.ExtensionContext, client: lc.LanguageClient) {
this.config = config;
this.extCtx = extCtx;
this.client = 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);
}
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;
}
export type Cmd = (...args: any[]) => unknown;
2019-12-30 21:53:21 +00:00
2019-12-31 17:14:00 +00:00
export async function sendRequestWithRetry<R>(
client: lc.LanguageClient,
method: string,
param: unknown,
2019-12-31 17:14:00 +00:00
token?: vscode.CancellationToken,
): Promise<R> {
for (const delay of [2, 4, 6, 8, 10, null]) {
try {
return await (token ? client.sendRequest(method, param, token) : client.sendRequest(method, param));
2020-02-16 23:47:14 +00:00
} catch (err) {
if (delay === null || err.code !== lc.ErrorCodes.ContentModified) {
throw err;
2019-12-31 17:14:00 +00:00
}
2020-02-16 23:47:14 +00:00
await sleep(10 * (1 << delay));
2019-12-31 17:14:00 +00:00
}
}
throw 'unreachable';
}
2019-12-30 22:12:33 +00:00
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));