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
92
93
94
95
96
97
98
99
100
101
|
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';
import { Config } from './config';
import { createClient } from './client';
export class Ctx {
readonly config: Config;
// Because we have "reload server" action, various listeners **will** face a
// situation where the client is not ready yet, and should be prepared to
// deal with it.
//
// Ideally, this should be replaced with async getter though.
// FIXME: this actually needs syncronization of some kind (check how
// vscode deals with `deactivate()` call when extension has some work scheduled
// on the event loop to get a better picture of what we can do here)
client: lc.LanguageClient | null = null;
private extCtx: vscode.ExtensionContext;
private onDidRestartHooks: Array<(client: lc.LanguageClient) => void> = [];
constructor(extCtx: vscode.ExtensionContext) {
this.config = new Config(extCtx);
this.extCtx = extCtx;
}
async restartServer() {
const old = this.client;
if (old) {
await old.stop();
}
this.client = null;
const client = await createClient(this.config);
if (!client) {
throw new Error("Rust Analyzer Language Server is not available");
}
this.pushCleanup(client.start());
await client.onReady();
this.client = client;
for (const hook of this.onDidRestartHooks) {
hook(client);
}
}
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);
}
get subscriptions(): Disposable[] {
return this.extCtx.subscriptions;
}
pushCleanup(d: Disposable) {
this.extCtx.subscriptions.push(d);
}
onDidRestart(hook: (client: lc.LanguageClient) => void) {
this.onDidRestartHooks.push(hook);
}
}
export interface Disposable {
dispose(): void;
}
export type Cmd = (...args: any[]) => unknown;
export async function sendRequestWithRetry<R>(
client: lc.LanguageClient,
method: string,
param: unknown,
token?: vscode.CancellationToken,
): Promise<R> {
for (const delay of [2, 4, 6, 8, 10, null]) {
try {
return await (token ? client.sendRequest(method, param, token) : client.sendRequest(method, param));
} catch (e) {
if (
e.code === lc.ErrorCodes.ContentModified &&
delay !== null
) {
await sleep(10 * (1 << delay));
continue;
}
throw e;
}
}
throw 'unreachable';
}
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
|