1 import * as vscode from 'vscode';
2 import * as lc from 'vscode-languageclient';
3 import { strict as assert } from "assert";
5 import { Config } from './config';
6 import { createClient } from './client';
9 readonly config: Config;
10 // Because we have "reload server" action, various listeners **will** face a
11 // situation where the client is not ready yet, and should be prepared to
14 // Ideally, this should be replaced with async getter though.
15 // FIXME: this actually needs syncronization of some kind (check how
16 // vscode deals with `deactivate()` call when extension has some work scheduled
17 // on the event loop to get a better picture of what we can do here)
18 client: lc.LanguageClient | null = null;
19 private extCtx: vscode.ExtensionContext;
21 constructor(extCtx: vscode.ExtensionContext) {
22 this.config = new Config(extCtx);
27 assert(this.client == null);
29 const client = await createClient(this.config);
32 "Rust Analyzer Language Server is not available. " +
33 "Please, ensure its [proper installation](https://github.com/rust-analyzer/rust-analyzer/tree/master/docs/user#vs-code)."
37 this.pushCleanup(client.start());
38 await client.onReady();
43 get activeRustEditor(): vscode.TextEditor | undefined {
44 const editor = vscode.window.activeTextEditor;
45 return editor && editor.document.languageId === 'rust'
50 registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
51 const fullName = `rust-analyzer.${name}`;
52 const cmd = factory(this);
53 const d = vscode.commands.registerCommand(fullName, cmd);
57 get globalState(): vscode.Memento {
58 return this.extCtx.globalState;
61 get subscriptions(): Disposable[] {
62 return this.extCtx.subscriptions;
65 pushCleanup(d: Disposable) {
66 this.extCtx.subscriptions.push(d);
70 export interface Disposable {
73 export type Cmd = (...args: any[]) => unknown;
75 export async function sendRequestWithRetry<R>(
76 client: lc.LanguageClient,
79 token?: vscode.CancellationToken,
81 for (const delay of [2, 4, 6, 8, 10, null]) {
83 return await (token ? client.sendRequest(method, param, token) : client.sendRequest(method, param));
85 if (delay === null || err.code !== lc.ErrorCodes.ContentModified) {
88 await sleep(10 * (1 << delay));
94 const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));