Simplify startup

This commit is contained in:
Aleksey Kladov 2020-02-17 13:40:20 +01:00
parent dcdbbddd16
commit d24e612106
5 changed files with 25 additions and 33 deletions

View file

@ -17,7 +17,6 @@ export class Ctx {
// on the event loop to get a better picture of what we can do here) // on the event loop to get a better picture of what we can do here)
client: lc.LanguageClient | null = null; client: lc.LanguageClient | null = null;
private extCtx: vscode.ExtensionContext; private extCtx: vscode.ExtensionContext;
private onStartHooks: Array<(client: lc.LanguageClient) => void> = [];
constructor(extCtx: vscode.ExtensionContext) { constructor(extCtx: vscode.ExtensionContext) {
this.config = new Config(extCtx); this.config = new Config(extCtx);
@ -39,9 +38,6 @@ export class Ctx {
await client.onReady(); await client.onReady();
this.client = client; this.client = client;
for (const hook of this.onStartHooks) {
hook(client);
}
} }
get activeRustEditor(): vscode.TextEditor | undefined { get activeRustEditor(): vscode.TextEditor | undefined {
@ -69,15 +65,6 @@ export class Ctx {
pushCleanup(d: Disposable) { pushCleanup(d: Disposable) {
this.extCtx.subscriptions.push(d); 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 { export interface Disposable {

View file

@ -7,7 +7,8 @@ import { Ctx, sendRequestWithRetry } from './ctx';
export function activateHighlighting(ctx: Ctx) { export function activateHighlighting(ctx: Ctx) {
const highlighter = new Highlighter(ctx); const highlighter = new Highlighter(ctx);
ctx.onStart(client => { const client = ctx.client;
if (client != null) {
client.onNotification( client.onNotification(
'rust-analyzer/publishDecorations', 'rust-analyzer/publishDecorations',
(params: PublishDecorationsParams) => { (params: PublishDecorationsParams) => {
@ -28,7 +29,7 @@ export function activateHighlighting(ctx: Ctx) {
highlighter.setHighlights(targetEditor, params.decorations); highlighter.setHighlights(targetEditor, params.decorations);
}, },
); );
}); };
vscode.workspace.onDidChangeConfiguration( vscode.workspace.onDidChangeConfiguration(
_ => highlighter.removeHighlights(), _ => highlighter.removeHighlights(),

View file

@ -33,9 +33,9 @@ export function activateInlayHints(ctx: Ctx) {
} }
}) })
// We pass async function though it will not be awaited when called, // XXX: we don't await this, thus Promise rejections won't be handled, but
// thus Promise rejections won't be handled, but this should never throw in fact... // this should never throw in fact...
ctx.onStart(async _ => hintsUpdater.setEnabled(ctx.config.displayInlayHints)); hintsUpdater.setEnabled(ctx.config.displayInlayHints)
} }
interface InlayHintsParams { interface InlayHintsParams {

View file

@ -11,6 +11,17 @@ let ctx: Ctx | undefined;
export async function activate(context: vscode.ExtensionContext) { export async function activate(context: vscode.ExtensionContext) {
ctx = new Ctx(context); 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) => { ctx.registerCommand('reload', (ctx) => {
return async () => { return async () => {
vscode.window.showInformationMessage('Reloading rust-analyzer...'); 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('analyzerStatus', commands.analyzerStatus);
ctx.registerCommand('collectGarbage', commands.collectGarbage); ctx.registerCommand('collectGarbage', commands.collectGarbage);
ctx.registerCommand('matchingBrace', commands.matchingBrace); ctx.registerCommand('matchingBrace', commands.matchingBrace);
@ -49,15 +59,6 @@ export async function activate(context: vscode.ExtensionContext) {
activateStatusDisplay(ctx); activateStatusDisplay(ctx);
activateHighlighting(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); activateInlayHints(ctx);
} }

View file

@ -9,11 +9,14 @@ const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '
export function activateStatusDisplay(ctx: Ctx) { export function activateStatusDisplay(ctx: Ctx) {
const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command); const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command);
ctx.pushCleanup(statusDisplay); ctx.pushCleanup(statusDisplay);
ctx.onStart(client => ctx.pushCleanup(client.onProgress( const client = ctx.client;
WorkDoneProgress.type, if (client != null) {
'rustAnalyzer/cargoWatcher', ctx.pushCleanup(client.onProgress(
params => statusDisplay.handleProgressNotification(params) WorkDoneProgress.type,
))); 'rustAnalyzer/cargoWatcher',
params => statusDisplay.handleProgressNotification(params)
))
}
} }
class StatusDisplay implements Disposable { class StatusDisplay implements Disposable {