Skip to content

Commit 7b9a8bc

Browse files
committed
Simplify startup
1 parent c3788dc commit 7b9a8bc

File tree

5 files changed

+25
-33
lines changed

5 files changed

+25
-33
lines changed

editors/code/src/ctx.ts

-13
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export class Ctx {
1717
// on the event loop to get a better picture of what we can do here)
1818
client: lc.LanguageClient | null = null;
1919
private extCtx: vscode.ExtensionContext;
20-
private onStartHooks: Array<(client: lc.LanguageClient) => void> = [];
2120

2221
constructor(extCtx: vscode.ExtensionContext) {
2322
this.config = new Config(extCtx);
@@ -39,9 +38,6 @@ export class Ctx {
3938
await client.onReady();
4039

4140
this.client = client;
42-
for (const hook of this.onStartHooks) {
43-
hook(client);
44-
}
4541
}
4642

4743
get activeRustEditor(): vscode.TextEditor | undefined {
@@ -69,15 +65,6 @@ export class Ctx {
6965
pushCleanup(d: Disposable) {
7066
this.extCtx.subscriptions.push(d);
7167
}
72-
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-
}
80-
}
8168
}
8269

8370
export interface Disposable {

editors/code/src/highlighting.ts

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

88
export function activateHighlighting(ctx: Ctx) {
99
const highlighter = new Highlighter(ctx);
10-
ctx.onStart(client => {
10+
const client = ctx.client;
11+
if (client != null) {
1112
client.onNotification(
1213
'rust-analyzer/publishDecorations',
1314
(params: PublishDecorationsParams) => {
@@ -28,7 +29,7 @@ export function activateHighlighting(ctx: Ctx) {
2829
highlighter.setHighlights(targetEditor, params.decorations);
2930
},
3031
);
31-
});
32+
};
3233

3334
vscode.workspace.onDidChangeConfiguration(
3435
_ => highlighter.removeHighlights(),

editors/code/src/inlay_hints.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ export function activateInlayHints(ctx: Ctx) {
3333
}
3434
})
3535

36-
// We pass async function though it will not be awaited when called,
37-
// thus Promise rejections won't be handled, but this should never throw in fact...
38-
ctx.onStart(async _ => hintsUpdater.setEnabled(ctx.config.displayInlayHints));
36+
// XXX: we don't await this, thus Promise rejections won't be handled, but
37+
// this should never throw in fact...
38+
hintsUpdater.setEnabled(ctx.config.displayInlayHints)
3939
}
4040

4141
interface InlayHintsParams {

editors/code/src/main.ts

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

14+
// Note: we try to start the server before we activate type hints so that it
15+
// registers its `onDidChangeDocument` handler before us.
16+
//
17+
// This a horribly, horribly wrong way to deal with this problem.
18+
try {
19+
await ctx.startServer();
20+
} catch (e) {
21+
vscode.window.showErrorMessage(e.message);
22+
}
23+
24+
// Commands which invokes manually via command palette, shortcut, etc.
1425
ctx.registerCommand('reload', (ctx) => {
1526
return async () => {
1627
vscode.window.showInformationMessage('Reloading rust-analyzer...');
@@ -28,7 +39,6 @@ export async function activate(context: vscode.ExtensionContext) {
2839
}
2940
})
3041

31-
// Commands which invokes manually via command palette, shortcut, etc.
3242
ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
3343
ctx.registerCommand('collectGarbage', commands.collectGarbage);
3444
ctx.registerCommand('matchingBrace', commands.matchingBrace);
@@ -49,15 +59,6 @@ export async function activate(context: vscode.ExtensionContext) {
4959
activateStatusDisplay(ctx);
5060

5161
activateHighlighting(ctx);
52-
// Note: we try to start the server before we activate type hints so that it
53-
// registers its `onDidChangeDocument` handler before us.
54-
//
55-
// This a horribly, horribly wrong way to deal with this problem.
56-
try {
57-
await ctx.startServer();
58-
} catch (e) {
59-
vscode.window.showErrorMessage(e.message);
60-
}
6162
activateInlayHints(ctx);
6263
}
6364

editors/code/src/status_display.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '
99
export function activateStatusDisplay(ctx: Ctx) {
1010
const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command);
1111
ctx.pushCleanup(statusDisplay);
12-
ctx.onStart(client => ctx.pushCleanup(client.onProgress(
13-
WorkDoneProgress.type,
14-
'rustAnalyzer/cargoWatcher',
15-
params => statusDisplay.handleProgressNotification(params)
16-
)));
12+
const client = ctx.client;
13+
if (client != null) {
14+
ctx.pushCleanup(client.onProgress(
15+
WorkDoneProgress.type,
16+
'rustAnalyzer/cargoWatcher',
17+
params => statusDisplay.handleProgressNotification(params)
18+
))
19+
}
1720
}
1821

1922
class StatusDisplay implements Disposable {

0 commit comments

Comments
 (0)