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