1 import * as vscode from 'vscode';
2 import * as lc from 'vscode-languageclient';
4 import { Config } from './config';
5 import { createClient } from './client';
8 readonly config: Config;
9 // Because we have "reload server" action, various listeners **will** face a
10 // situation where the client is not ready yet, and should be prepared to
13 // Ideally, this should be replaced with async getter though.
14 // FIXME: this actually needs syncronization of some kind (check how
15 // vscode deals with `deactivate()` call when extension has some work scheduled
16 // on the event loop to get a better picture of what we can do here)
17 client: lc.LanguageClient;
18 private extCtx: vscode.ExtensionContext;
20 static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise<Ctx> {
21 const client = await createClient(config, serverPath);
22 const res = new Ctx(config, extCtx, client);
23 res.pushCleanup(client.start());
24 await client.onReady();
28 private constructor(config: Config, extCtx: vscode.ExtensionContext, client: lc.LanguageClient) {
34 get activeRustEditor(): vscode.TextEditor | undefined {
35 const editor = vscode.window.activeTextEditor;
36 return editor && editor.document.languageId === 'rust'
41 registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
42 const fullName = `rust-analyzer.${name}`;
43 const cmd = factory(this);
44 const d = vscode.commands.registerCommand(fullName, cmd);
48 get globalState(): vscode.Memento {
49 return this.extCtx.globalState;
52 get subscriptions(): Disposable[] {
53 return this.extCtx.subscriptions;
56 pushCleanup(d: Disposable) {
57 this.extCtx.subscriptions.push(d);
61 export interface Disposable {
64 export type Cmd = (...args: any[]) => unknown;
66 export async function sendRequestWithRetry<R>(
67 client: lc.LanguageClient,
70 token?: vscode.CancellationToken,
72 for (const delay of [2, 4, 6, 8, 10, null]) {
74 return await (token ? client.sendRequest(method, param, token) : client.sendRequest(method, param));
76 if (delay === null || err.code !== lc.ErrorCodes.ContentModified) {
79 await sleep(10 * (1 << delay));
85 const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));