]> git.lizzy.rs Git - rust.git/blob - editors/code/src/ctx.ts
Remove two stage constuction
[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;
18     private extCtx: vscode.ExtensionContext;
19
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();
25         return res;
26     }
27
28     private constructor(config: Config, extCtx: vscode.ExtensionContext, client: lc.LanguageClient) {
29         this.config = config;
30         this.extCtx = extCtx;
31         this.client = client
32     }
33
34     get activeRustEditor(): vscode.TextEditor | undefined {
35         const editor = vscode.window.activeTextEditor;
36         return editor && editor.document.languageId === 'rust'
37             ? editor
38             : undefined;
39     }
40
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);
45         this.pushCleanup(d);
46     }
47
48     get globalState(): vscode.Memento {
49         return this.extCtx.globalState;
50     }
51
52     get subscriptions(): Disposable[] {
53         return this.extCtx.subscriptions;
54     }
55
56     pushCleanup(d: Disposable) {
57         this.extCtx.subscriptions.push(d);
58     }
59 }
60
61 export interface Disposable {
62     dispose(): void;
63 }
64 export type Cmd = (...args: any[]) => unknown;
65
66 export async function sendRequestWithRetry<R>(
67     client: lc.LanguageClient,
68     method: string,
69     param: unknown,
70     token?: vscode.CancellationToken,
71 ): Promise<R> {
72     for (const delay of [2, 4, 6, 8, 10, null]) {
73         try {
74             return await (token ? client.sendRequest(method, param, token) : client.sendRequest(method, param));
75         } catch (err) {
76             if (delay === null || err.code !== lc.ErrorCodes.ContentModified) {
77                 throw err;
78             }
79             await sleep(10 * (1 << delay));
80         }
81     }
82     throw 'unreachable';
83 }
84
85 const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));