aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/ctx.ts
blob: 39eddfcbdf5ae611fbea7e1d4cf082b5b24dd9f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';
import { Server } from './server';
import { Config } from './config';

export class Ctx {
    private extCtx: vscode.ExtensionContext;

    constructor(extCtx: vscode.ExtensionContext) {
        this.extCtx = extCtx;
    }

    get client(): lc.LanguageClient {
        return Server.client;
    }

    get config(): Config {
        return Server.config;
    }

    get activeRustEditor(): vscode.TextEditor | undefined {
        const editor = vscode.window.activeTextEditor;
        return editor && editor.document.languageId === 'rust'
            ? editor
            : undefined;
    }

    registerCommand(name: string, factory: (ctx: Ctx) => Cmd) {
        const fullName = `rust-analyzer.${name}`;
        const cmd = factory(this);
        const d = vscode.commands.registerCommand(fullName, cmd);
        this.pushCleanup(d);
    }

    overrideCommand(name: string, factory: (ctx: Ctx) => Cmd) {
        const defaultCmd = `default:${name}`;
        const override = factory(this);
        const original = (...args: any[]) =>
            vscode.commands.executeCommand(defaultCmd, ...args);
        try {
            const d = vscode.commands.registerCommand(
                name,
                async (...args: any[]) => {
                    if (!(await override(...args))) {
                        return await original(...args);
                    }
                },
            );
            this.pushCleanup(d);
        } catch (_) {
            vscode.window.showWarningMessage(
                'Enhanced typing feature is disabled because of incompatibility with VIM extension, consider turning off rust-analyzer.enableEnhancedTyping: https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/README.md#settings',
            );
        }
    }

    get subscriptions(): { dispose(): any }[] {
        return this.extCtx.subscriptions;
    }

    pushCleanup(d: { dispose(): any }) {
        this.extCtx.subscriptions.push(d);
    }

    async sendRequestWithRetry<R>(
        method: string,
        param: any,
        token: vscode.CancellationToken,
    ): Promise<R> {
        await this.client.onReady();
        for (const delay of [2, 4, 6, 8, 10, null]) {
            try {
                return await this.client.sendRequest(method, param, token);
            } catch (e) {
                if (
                    e.code === lc.ErrorCodes.ContentModified &&
                    delay !== null
                ) {
                    await sleep(10 * (1 << delay));
                    continue;
                }
                throw e;
            }
        }
        throw 'unreachable';
    }
}

export type Cmd = (...args: any[]) => any;

const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));