diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 1eff88df2a..c06d8ac317 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -17,7 +17,6 @@ export class Ctx { // 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 onStartHooks: Array<(client: lc.LanguageClient) => void> = []; constructor(extCtx: vscode.ExtensionContext) { this.config = new Config(extCtx); @@ -39,9 +38,6 @@ export class Ctx { await client.onReady(); this.client = client; - for (const hook of this.onStartHooks) { - hook(client); - } } get activeRustEditor(): vscode.TextEditor | undefined { @@ -69,15 +65,6 @@ export class Ctx { pushCleanup(d: Disposable) { this.extCtx.subscriptions.push(d); } - - onStart(hook: (client: lc.LanguageClient) => void) { - const client = this.client; - if (client == null) { - this.onStartHooks.push(hook); - } else { - hook(client) - } - } } export interface Disposable { diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index f693fb8ba7..a2db04de87 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts @@ -7,7 +7,8 @@ import { Ctx, sendRequestWithRetry } from './ctx'; export function activateHighlighting(ctx: Ctx) { const highlighter = new Highlighter(ctx); - ctx.onStart(client => { + const client = ctx.client; + if (client != null) { client.onNotification( 'rust-analyzer/publishDecorations', (params: PublishDecorationsParams) => { @@ -28,7 +29,7 @@ export function activateHighlighting(ctx: Ctx) { highlighter.setHighlights(targetEditor, params.decorations); }, ); - }); + }; vscode.workspace.onDidChangeConfiguration( _ => highlighter.removeHighlights(), diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index 9e400fabec..55bbd7f004 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts @@ -33,9 +33,9 @@ export function activateInlayHints(ctx: Ctx) { } }) - // We pass async function though it will not be awaited when called, - // thus Promise rejections won't be handled, but this should never throw in fact... - ctx.onStart(async _ => hintsUpdater.setEnabled(ctx.config.displayInlayHints)); + // XXX: we don't await this, thus Promise rejections won't be handled, but + // this should never throw in fact... + hintsUpdater.setEnabled(ctx.config.displayInlayHints) } interface InlayHintsParams { diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index ec488c3405..0bf2c4829b 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -11,6 +11,17 @@ let ctx: Ctx | undefined; export async function activate(context: vscode.ExtensionContext) { ctx = new Ctx(context); + // Note: we try to start the server before we activate type hints so that it + // registers its `onDidChangeDocument` handler before us. + // + // This a horribly, horribly wrong way to deal with this problem. + try { + await ctx.startServer(); + } catch (e) { + vscode.window.showErrorMessage(e.message); + } + + // Commands which invokes manually via command palette, shortcut, etc. ctx.registerCommand('reload', (ctx) => { return async () => { vscode.window.showInformationMessage('Reloading rust-analyzer...'); @@ -28,7 +39,6 @@ export async function activate(context: vscode.ExtensionContext) { } }) - // Commands which invokes manually via command palette, shortcut, etc. ctx.registerCommand('analyzerStatus', commands.analyzerStatus); ctx.registerCommand('collectGarbage', commands.collectGarbage); ctx.registerCommand('matchingBrace', commands.matchingBrace); @@ -49,15 +59,6 @@ export async function activate(context: vscode.ExtensionContext) { activateStatusDisplay(ctx); activateHighlighting(ctx); - // Note: we try to start the server before we activate type hints so that it - // registers its `onDidChangeDocument` handler before us. - // - // This a horribly, horribly wrong way to deal with this problem. - try { - await ctx.startServer(); - } catch (e) { - vscode.window.showErrorMessage(e.message); - } activateInlayHints(ctx); } diff --git a/editors/code/src/status_display.ts b/editors/code/src/status_display.ts index 326b5217b5..ed0d821665 100644 --- a/editors/code/src/status_display.ts +++ b/editors/code/src/status_display.ts @@ -9,11 +9,14 @@ const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', ' export function activateStatusDisplay(ctx: Ctx) { const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command); ctx.pushCleanup(statusDisplay); - ctx.onStart(client => ctx.pushCleanup(client.onProgress( - WorkDoneProgress.type, - 'rustAnalyzer/cargoWatcher', - params => statusDisplay.handleProgressNotification(params) - ))); + const client = ctx.client; + if (client != null) { + ctx.pushCleanup(client.onProgress( + WorkDoneProgress.type, + 'rustAnalyzer/cargoWatcher', + params => statusDisplay.handleProgressNotification(params) + )) + } } class StatusDisplay implements Disposable {