Skip to content

Simplify TS reload logic #3190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions editors/code/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,3 @@ export function selectAndApplySourceChange(ctx: Ctx): Cmd {
}
};
}

export function reload(ctx: Ctx): Cmd {
return async () => {
vscode.window.showInformationMessage('Reloading rust-analyzer...');
await ctx.restartServer();
};
}
18 changes: 4 additions & 14 deletions editors/code/src/ctx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';
import { strict as assert } from "assert";

import { Config } from './config';
import { createClient } from './client';
Expand All @@ -16,19 +17,15 @@ 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 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;
async startServer() {
assert(this.client == null);

const client = await createClient(this.config);
if (!client) {
throw new Error(
Expand All @@ -41,9 +38,6 @@ export class Ctx {
await client.onReady();

this.client = client;
for (const hook of this.onDidRestartHooks) {
hook(client);
}
}

get activeRustEditor(): vscode.TextEditor | undefined {
Expand Down Expand Up @@ -71,10 +65,6 @@ export class Ctx {
pushCleanup(d: Disposable) {
this.extCtx.subscriptions.push(d);
}

onDidRestart(hook: (client: lc.LanguageClient) => void) {
this.onDidRestartHooks.push(hook);
}
}

export interface Disposable {
Expand Down
5 changes: 3 additions & 2 deletions editors/code/src/highlighting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { Ctx, sendRequestWithRetry } from './ctx';

export function activateHighlighting(ctx: Ctx) {
const highlighter = new Highlighter(ctx);
ctx.onDidRestart(client => {
const client = ctx.client;
if (client != null) {
client.onNotification(
'rust-analyzer/publishDecorations',
(params: PublishDecorationsParams) => {
Expand All @@ -28,7 +29,7 @@ export function activateHighlighting(ctx: Ctx) {
highlighter.setHighlights(targetEditor, params.decorations);
},
);
});
};

vscode.workspace.onDidChangeConfiguration(
_ => highlighter.removeHighlights(),
Expand Down
23 changes: 19 additions & 4 deletions editors/code/src/inlay_hints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ export function activateInlayHints(ctx: Ctx) {
ctx.subscriptions
);

// 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.onDidRestart(async _ => hintsUpdater.setEnabled(ctx.config.displayInlayHints));
ctx.pushCleanup({
dispose() {
hintsUpdater.clear()
}
})

// 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 {
Expand Down Expand Up @@ -61,16 +67,23 @@ class HintsUpdater {

constructor(ctx: Ctx) {
this.ctx = ctx;
this.enabled = ctx.config.displayInlayHints;
this.enabled = false;
}

async setEnabled(enabled: boolean): Promise<void> {
console.log({ enabled, prev: this.enabled });

if (this.enabled == enabled) return;
this.enabled = enabled;

if (this.enabled) {
return await this.refresh();
} else {
return this.clear();
}
}

clear() {
this.allEditors.forEach(it => {
this.setTypeDecorations(it, []);
this.setParameterDecorations(it, []);
Expand All @@ -79,6 +92,8 @@ class HintsUpdater {

async refresh() {
if (!this.enabled) return;
console.log("freshin!");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intended?


await Promise.all(this.allEditors.map(it => this.refreshEditor(it)));
}

Expand Down
38 changes: 28 additions & 10 deletions editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,34 @@ 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...');
// @DanTup maneuver
// https://github.com/microsoft/vscode/issues/45774#issuecomment-373423895
await deactivate()
for (const sub of ctx.subscriptions) {
try {
sub.dispose();
} catch (e) {
console.error(e);
}
}
await activate(context)
}
})

ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
ctx.registerCommand('collectGarbage', commands.collectGarbage);
ctx.registerCommand('matchingBrace', commands.matchingBrace);
Expand All @@ -20,7 +47,6 @@ export async function activate(context: vscode.ExtensionContext) {
ctx.registerCommand('syntaxTree', commands.syntaxTree);
ctx.registerCommand('expandMacro', commands.expandMacro);
ctx.registerCommand('run', commands.run);
ctx.registerCommand('reload', commands.reload);
ctx.registerCommand('onEnter', commands.onEnter);
ctx.registerCommand('ssr', commands.ssr)

Expand All @@ -33,18 +59,10 @@ 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.restartServer();
} catch (e) {
vscode.window.showErrorMessage(e.message);
}
activateInlayHints(ctx);
}

export async function deactivate() {
await ctx?.client?.stop();
ctx = undefined;
}
13 changes: 8 additions & 5 deletions editors/code/src/status_display.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '
export function activateStatusDisplay(ctx: Ctx) {
const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command);
ctx.pushCleanup(statusDisplay);
ctx.onDidRestart(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 {
Expand Down