1 import * as vscode from 'vscode';
2 import * as lc from 'vscode-languageclient';
4 import { Config } from './config';
5 import { createClient } from './client';
9 readonly config: Config,
10 private readonly extCtx: vscode.ExtensionContext,
11 readonly client: lc.LanguageClient
16 static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise<Ctx> {
17 const client = await createClient(config, serverPath);
18 const res = new Ctx(config, extCtx, client);
19 res.pushCleanup(client.start());
20 await client.onReady();
24 get activeRustEditor(): vscode.TextEditor | undefined {
25 const editor = vscode.window.activeTextEditor;
26 return editor && editor.document.languageId === 'rust'
31 registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
32 const fullName = `rust-analyzer.${name}`;
33 const cmd = factory(this);
34 const d = vscode.commands.registerCommand(fullName, cmd);
38 get globalState(): vscode.Memento {
39 return this.extCtx.globalState;
42 get subscriptions(): Disposable[] {
43 return this.extCtx.subscriptions;
46 pushCleanup(d: Disposable) {
47 this.extCtx.subscriptions.push(d);
51 export interface Disposable {
54 export type Cmd = (...args: any[]) => unknown;
56 export async function sendRequestWithRetry<R>(
57 client: lc.LanguageClient,
60 token?: vscode.CancellationToken,
62 for (const delay of [2, 4, 6, 8, 10, null]) {
64 return await (token ? client.sendRequest(method, param, token) : client.sendRequest(method, param));
66 if (delay === null || err.code !== lc.ErrorCodes.ContentModified) {
69 await sleep(10 * (1 << delay));
75 const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));