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 | null = null;
18 private extCtx: vscode.ExtensionContext;
19 private onDidRestartHooks: Array<(client: lc.LanguageClient) => void> = [];
21 constructor(extCtx: vscode.ExtensionContext) {
22 this.config = new Config(extCtx);
26 async restartServer() {
27 const old = this.client;
32 const client = await createClient(this.config);
35 "Rust Analyzer Language Server is not available. " +
36 "Please, ensure its [proper installation](https://github.com/rust-analyzer/rust-analyzer/tree/master/docs/user#vs-code)."
40 this.pushCleanup(client.start());
41 await client.onReady();
44 for (const hook of this.onDidRestartHooks) {
49 get activeRustEditor(): vscode.TextEditor | undefined {
50 const editor = vscode.window.activeTextEditor;
51 return editor && editor.document.languageId === 'rust'
56 registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
57 const fullName = `rust-analyzer.${name}`;
58 const cmd = factory(this);
59 const d = vscode.commands.registerCommand(fullName, cmd);
63 get globalState(): vscode.Memento {
64 return this.extCtx.globalState;
67 get subscriptions(): Disposable[] {
68 return this.extCtx.subscriptions;
71 pushCleanup(d: Disposable) {
72 this.extCtx.subscriptions.push(d);
75 onDidRestart(hook: (client: lc.LanguageClient) => void) {
76 this.onDidRestartHooks.push(hook);
80 export interface Disposable {
83 export type Cmd = (...args: any[]) => unknown;
85 export async function sendRequestWithRetry<R>(
86 client: lc.LanguageClient,
89 token?: vscode.CancellationToken,
91 for (const delay of [2, 4, 6, 8, 10, null]) {
93 return await (token ? client.sendRequest(method, param, token) : client.sendRequest(method, param));
95 if (delay === null || err.code !== lc.ErrorCodes.ContentModified) {
98 await sleep(10 * (1 << delay));
104 const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));