]> git.lizzy.rs Git - rust.git/blob - editors/code/src/ctx.ts
Merge #3153
[rust.git] / editors / code / src / ctx.ts
1 import * as vscode from 'vscode';
2 import * as lc from 'vscode-languageclient';
3
4 import { Config } from './config';
5 import { createClient } from './client';
6
7 export class Ctx {
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
11     // deal with it.
12     //
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> = [];
20
21     constructor(extCtx: vscode.ExtensionContext) {
22         this.config = new Config(extCtx);
23         this.extCtx = extCtx;
24     }
25
26     async restartServer() {
27         const old = this.client;
28         if (old) {
29             await old.stop();
30         }
31         this.client = null;
32         const client = await createClient(this.config);
33         if (!client) {
34             throw new Error(
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)."
37             );
38         }
39
40         this.pushCleanup(client.start());
41         await client.onReady();
42
43         this.client = client;
44         for (const hook of this.onDidRestartHooks) {
45             hook(client);
46         }
47     }
48
49     get activeRustEditor(): vscode.TextEditor | undefined {
50         const editor = vscode.window.activeTextEditor;
51         return editor && editor.document.languageId === 'rust'
52             ? editor
53             : undefined;
54     }
55
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);
60         this.pushCleanup(d);
61     }
62
63     get globalState(): vscode.Memento {
64         return this.extCtx.globalState;
65     }
66
67     get subscriptions(): Disposable[] {
68         return this.extCtx.subscriptions;
69     }
70
71     pushCleanup(d: Disposable) {
72         this.extCtx.subscriptions.push(d);
73     }
74
75     onDidRestart(hook: (client: lc.LanguageClient) => void) {
76         this.onDidRestartHooks.push(hook);
77     }
78 }
79
80 export interface Disposable {
81     dispose(): void;
82 }
83 export type Cmd = (...args: any[]) => unknown;
84
85 export async function sendRequestWithRetry<R>(
86     client: lc.LanguageClient,
87     method: string,
88     param: unknown,
89     token?: vscode.CancellationToken,
90 ): Promise<R> {
91     for (const delay of [2, 4, 6, 8, 10, null]) {
92         try {
93             return await (token ? client.sendRequest(method, param, token) : client.sendRequest(method, param));
94         } catch (err) {
95             if (delay === null || err.code !== lc.ErrorCodes.ContentModified) {
96                 throw err;
97             }
98             await sleep(10 * (1 << delay));
99         }
100     }
101     throw 'unreachable';
102 }
103
104 const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));