Skip to content

Commit c3788dc

Browse files
committed
Simplify TS reload logic
Fixes #3164
1 parent 334f534 commit c3788dc

File tree

6 files changed

+51
-23
lines changed

6 files changed

+51
-23
lines changed

editors/code/src/commands/index.ts

-7
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,3 @@ export function selectAndApplySourceChange(ctx: Ctx): Cmd {
5151
}
5252
};
5353
}
54-
55-
export function reload(ctx: Ctx): Cmd {
56-
return async () => {
57-
vscode.window.showInformationMessage('Reloading rust-analyzer...');
58-
await ctx.restartServer();
59-
};
60-
}

editors/code/src/ctx.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as vscode from 'vscode';
22
import * as lc from 'vscode-languageclient';
3+
import { strict as assert } from "assert";
34

45
import { Config } from './config';
56
import { createClient } from './client';
@@ -16,19 +17,16 @@ export class Ctx {
1617
// on the event loop to get a better picture of what we can do here)
1718
client: lc.LanguageClient | null = null;
1819
private extCtx: vscode.ExtensionContext;
19-
private onDidRestartHooks: Array<(client: lc.LanguageClient) => void> = [];
20+
private onStartHooks: Array<(client: lc.LanguageClient) => void> = [];
2021

2122
constructor(extCtx: vscode.ExtensionContext) {
2223
this.config = new Config(extCtx);
2324
this.extCtx = extCtx;
2425
}
2526

26-
async restartServer() {
27-
const old = this.client;
28-
if (old) {
29-
await old.stop();
30-
}
31-
this.client = null;
27+
async startServer() {
28+
assert(this.client == null);
29+
3230
const client = await createClient(this.config);
3331
if (!client) {
3432
throw new Error(
@@ -41,7 +39,7 @@ export class Ctx {
4139
await client.onReady();
4240

4341
this.client = client;
44-
for (const hook of this.onDidRestartHooks) {
42+
for (const hook of this.onStartHooks) {
4543
hook(client);
4644
}
4745
}
@@ -72,8 +70,13 @@ export class Ctx {
7270
this.extCtx.subscriptions.push(d);
7371
}
7472

75-
onDidRestart(hook: (client: lc.LanguageClient) => void) {
76-
this.onDidRestartHooks.push(hook);
73+
onStart(hook: (client: lc.LanguageClient) => void) {
74+
const client = this.client;
75+
if (client == null) {
76+
this.onStartHooks.push(hook);
77+
} else {
78+
hook(client)
79+
}
7780
}
7881
}
7982

editors/code/src/highlighting.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Ctx, sendRequestWithRetry } from './ctx';
77

88
export function activateHighlighting(ctx: Ctx) {
99
const highlighter = new Highlighter(ctx);
10-
ctx.onDidRestart(client => {
10+
ctx.onStart(client => {
1111
client.onNotification(
1212
'rust-analyzer/publishDecorations',
1313
(params: PublishDecorationsParams) => {

editors/code/src/inlay_hints.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,15 @@ export function activateInlayHints(ctx: Ctx) {
2727
ctx.subscriptions
2828
);
2929

30+
ctx.pushCleanup({
31+
dispose() {
32+
hintsUpdater.clear()
33+
}
34+
})
35+
3036
// We pass async function though it will not be awaited when called,
3137
// thus Promise rejections won't be handled, but this should never throw in fact...
32-
ctx.onDidRestart(async _ => hintsUpdater.setEnabled(ctx.config.displayInlayHints));
38+
ctx.onStart(async _ => hintsUpdater.setEnabled(ctx.config.displayInlayHints));
3339
}
3440

3541
interface InlayHintsParams {
@@ -61,16 +67,23 @@ class HintsUpdater {
6167

6268
constructor(ctx: Ctx) {
6369
this.ctx = ctx;
64-
this.enabled = ctx.config.displayInlayHints;
70+
this.enabled = false;
6571
}
6672

6773
async setEnabled(enabled: boolean): Promise<void> {
74+
console.log({ enabled, prev: this.enabled });
75+
6876
if (this.enabled == enabled) return;
6977
this.enabled = enabled;
7078

7179
if (this.enabled) {
7280
return await this.refresh();
81+
} else {
82+
return this.clear();
7383
}
84+
}
85+
86+
clear() {
7487
this.allEditors.forEach(it => {
7588
this.setTypeDecorations(it, []);
7689
this.setParameterDecorations(it, []);
@@ -79,6 +92,8 @@ class HintsUpdater {
7992

8093
async refresh() {
8194
if (!this.enabled) return;
95+
console.log("freshin!");
96+
8297
await Promise.all(this.allEditors.map(it => this.refreshEditor(it)));
8398
}
8499

editors/code/src/main.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ let ctx: Ctx | undefined;
1111
export async function activate(context: vscode.ExtensionContext) {
1212
ctx = new Ctx(context);
1313

14+
ctx.registerCommand('reload', (ctx) => {
15+
return async () => {
16+
vscode.window.showInformationMessage('Reloading rust-analyzer...');
17+
// @DanTup maneuver
18+
// https://github.com/microsoft/vscode/issues/45774#issuecomment-373423895
19+
await deactivate()
20+
for (const sub of ctx.subscriptions) {
21+
try {
22+
sub.dispose();
23+
} catch (e) {
24+
console.error(e);
25+
}
26+
}
27+
await activate(context)
28+
}
29+
})
30+
1431
// Commands which invokes manually via command palette, shortcut, etc.
1532
ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
1633
ctx.registerCommand('collectGarbage', commands.collectGarbage);
@@ -20,7 +37,6 @@ export async function activate(context: vscode.ExtensionContext) {
2037
ctx.registerCommand('syntaxTree', commands.syntaxTree);
2138
ctx.registerCommand('expandMacro', commands.expandMacro);
2239
ctx.registerCommand('run', commands.run);
23-
ctx.registerCommand('reload', commands.reload);
2440
ctx.registerCommand('onEnter', commands.onEnter);
2541
ctx.registerCommand('ssr', commands.ssr)
2642

@@ -38,7 +54,7 @@ export async function activate(context: vscode.ExtensionContext) {
3854
//
3955
// This a horribly, horribly wrong way to deal with this problem.
4056
try {
41-
await ctx.restartServer();
57+
await ctx.startServer();
4258
} catch (e) {
4359
vscode.window.showErrorMessage(e.message);
4460
}
@@ -47,4 +63,5 @@ export async function activate(context: vscode.ExtensionContext) {
4763

4864
export async function deactivate() {
4965
await ctx?.client?.stop();
66+
ctx = undefined;
5067
}

editors/code/src/status_display.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '
99
export function activateStatusDisplay(ctx: Ctx) {
1010
const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command);
1111
ctx.pushCleanup(statusDisplay);
12-
ctx.onDidRestart(client => ctx.pushCleanup(client.onProgress(
12+
ctx.onStart(client => ctx.pushCleanup(client.onProgress(
1313
WorkDoneProgress.type,
1414
'rustAnalyzer/cargoWatcher',
1515
params => statusDisplay.handleProgressNotification(params)

0 commit comments

Comments
 (0)