aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-02-17 11:17:01 +0000
committerAleksey Kladov <[email protected]>2020-02-17 12:40:47 +0000
commitdcdbbddd1630a4ed01906c2aff0e2b65ed99a591 (patch)
treee02793bf82f2956bf7c61dfbd7adfcfdf4df191b /editors/code/src
parentfcf15cc05afaeda6880664777ff2a3db342ea088 (diff)
Simplify TS reload logic
Fixes #3164
Diffstat (limited to 'editors/code/src')
-rw-r--r--editors/code/src/commands/index.ts7
-rw-r--r--editors/code/src/ctx.ts23
-rw-r--r--editors/code/src/highlighting.ts2
-rw-r--r--editors/code/src/inlay_hints.ts19
-rw-r--r--editors/code/src/main.ts21
-rw-r--r--editors/code/src/status_display.ts2
6 files changed, 51 insertions, 23 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index b5ebec117..d05f40d67 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -51,10 +51,3 @@ export function selectAndApplySourceChange(ctx: Ctx): Cmd {
51 } 51 }
52 }; 52 };
53} 53}
54
55export function reload(ctx: Ctx): Cmd {
56 return async () => {
57 vscode.window.showInformationMessage('Reloading rust-analyzer...');
58 await ctx.restartServer();
59 };
60}
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts
index ff6245f78..1eff88df2 100644
--- a/editors/code/src/ctx.ts
+++ b/editors/code/src/ctx.ts
@@ -1,5 +1,6 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as lc from 'vscode-languageclient';
3import { strict as assert } from "assert";
3 4
4import { Config } from './config'; 5import { Config } from './config';
5import { createClient } from './client'; 6import { createClient } from './client';
@@ -16,19 +17,16 @@ export class Ctx {
16 // on the event loop to get a better picture of what we can do here) 17 // on the event loop to get a better picture of what we can do here)
17 client: lc.LanguageClient | null = null; 18 client: lc.LanguageClient | null = null;
18 private extCtx: vscode.ExtensionContext; 19 private extCtx: vscode.ExtensionContext;
19 private onDidRestartHooks: Array<(client: lc.LanguageClient) => void> = []; 20 private onStartHooks: Array<(client: lc.LanguageClient) => void> = [];
20 21
21 constructor(extCtx: vscode.ExtensionContext) { 22 constructor(extCtx: vscode.ExtensionContext) {
22 this.config = new Config(extCtx); 23 this.config = new Config(extCtx);
23 this.extCtx = extCtx; 24 this.extCtx = extCtx;
24 } 25 }
25 26
26 async restartServer() { 27 async startServer() {
27 const old = this.client; 28 assert(this.client == null);
28 if (old) { 29
29 await old.stop();
30 }
31 this.client = null;
32 const client = await createClient(this.config); 30 const client = await createClient(this.config);
33 if (!client) { 31 if (!client) {
34 throw new Error( 32 throw new Error(
@@ -41,7 +39,7 @@ export class Ctx {
41 await client.onReady(); 39 await client.onReady();
42 40
43 this.client = client; 41 this.client = client;
44 for (const hook of this.onDidRestartHooks) { 42 for (const hook of this.onStartHooks) {
45 hook(client); 43 hook(client);
46 } 44 }
47 } 45 }
@@ -72,8 +70,13 @@ export class Ctx {
72 this.extCtx.subscriptions.push(d); 70 this.extCtx.subscriptions.push(d);
73 } 71 }
74 72
75 onDidRestart(hook: (client: lc.LanguageClient) => void) { 73 onStart(hook: (client: lc.LanguageClient) => void) {
76 this.onDidRestartHooks.push(hook); 74 const client = this.client;
75 if (client == null) {
76 this.onStartHooks.push(hook);
77 } else {
78 hook(client)
79 }
77 } 80 }
78} 81}
79 82
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts
index 4fbbe3ddc..f693fb8ba 100644
--- a/editors/code/src/highlighting.ts
+++ b/editors/code/src/highlighting.ts
@@ -7,7 +7,7 @@ import { Ctx, sendRequestWithRetry } from './ctx';
7 7
8export function activateHighlighting(ctx: Ctx) { 8export function activateHighlighting(ctx: Ctx) {
9 const highlighter = new Highlighter(ctx); 9 const highlighter = new Highlighter(ctx);
10 ctx.onDidRestart(client => { 10 ctx.onStart(client => {
11 client.onNotification( 11 client.onNotification(
12 'rust-analyzer/publishDecorations', 12 'rust-analyzer/publishDecorations',
13 (params: PublishDecorationsParams) => { 13 (params: PublishDecorationsParams) => {
diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts
index 3896878cd..9e400fabe 100644
--- a/editors/code/src/inlay_hints.ts
+++ b/editors/code/src/inlay_hints.ts
@@ -27,9 +27,15 @@ export function activateInlayHints(ctx: Ctx) {
27 ctx.subscriptions 27 ctx.subscriptions
28 ); 28 );
29 29
30 ctx.pushCleanup({
31 dispose() {
32 hintsUpdater.clear()
33 }
34 })
35
30 // We pass async function though it will not be awaited when called, 36 // We pass async function though it will not be awaited when called,
31 // thus Promise rejections won't be handled, but this should never throw in fact... 37 // 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));
33} 39}
34 40
35interface InlayHintsParams { 41interface InlayHintsParams {
@@ -61,16 +67,23 @@ class HintsUpdater {
61 67
62 constructor(ctx: Ctx) { 68 constructor(ctx: Ctx) {
63 this.ctx = ctx; 69 this.ctx = ctx;
64 this.enabled = ctx.config.displayInlayHints; 70 this.enabled = false;
65 } 71 }
66 72
67 async setEnabled(enabled: boolean): Promise<void> { 73 async setEnabled(enabled: boolean): Promise<void> {
74 console.log({ enabled, prev: this.enabled });
75
68 if (this.enabled == enabled) return; 76 if (this.enabled == enabled) return;
69 this.enabled = enabled; 77 this.enabled = enabled;
70 78
71 if (this.enabled) { 79 if (this.enabled) {
72 return await this.refresh(); 80 return await this.refresh();
81 } else {
82 return this.clear();
73 } 83 }
84 }
85
86 clear() {
74 this.allEditors.forEach(it => { 87 this.allEditors.forEach(it => {
75 this.setTypeDecorations(it, []); 88 this.setTypeDecorations(it, []);
76 this.setParameterDecorations(it, []); 89 this.setParameterDecorations(it, []);
@@ -79,6 +92,8 @@ class HintsUpdater {
79 92
80 async refresh() { 93 async refresh() {
81 if (!this.enabled) return; 94 if (!this.enabled) return;
95 console.log("freshin!");
96
82 await Promise.all(this.allEditors.map(it => this.refreshEditor(it))); 97 await Promise.all(this.allEditors.map(it => this.refreshEditor(it)));
83 } 98 }
84 99
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 5a99e96f0..ec488c340 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -11,6 +11,23 @@ let ctx: Ctx | undefined;
11export async function activate(context: vscode.ExtensionContext) { 11export async function activate(context: vscode.ExtensionContext) {
12 ctx = new Ctx(context); 12 ctx = new Ctx(context);
13 13
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
14 // Commands which invokes manually via command palette, shortcut, etc. 31 // Commands which invokes manually via command palette, shortcut, etc.
15 ctx.registerCommand('analyzerStatus', commands.analyzerStatus); 32 ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
16 ctx.registerCommand('collectGarbage', commands.collectGarbage); 33 ctx.registerCommand('collectGarbage', commands.collectGarbage);
@@ -20,7 +37,6 @@ export async function activate(context: vscode.ExtensionContext) {
20 ctx.registerCommand('syntaxTree', commands.syntaxTree); 37 ctx.registerCommand('syntaxTree', commands.syntaxTree);
21 ctx.registerCommand('expandMacro', commands.expandMacro); 38 ctx.registerCommand('expandMacro', commands.expandMacro);
22 ctx.registerCommand('run', commands.run); 39 ctx.registerCommand('run', commands.run);
23 ctx.registerCommand('reload', commands.reload);
24 ctx.registerCommand('onEnter', commands.onEnter); 40 ctx.registerCommand('onEnter', commands.onEnter);
25 ctx.registerCommand('ssr', commands.ssr) 41 ctx.registerCommand('ssr', commands.ssr)
26 42
@@ -38,7 +54,7 @@ export async function activate(context: vscode.ExtensionContext) {
38 // 54 //
39 // This a horribly, horribly wrong way to deal with this problem. 55 // This a horribly, horribly wrong way to deal with this problem.
40 try { 56 try {
41 await ctx.restartServer(); 57 await ctx.startServer();
42 } catch (e) { 58 } catch (e) {
43 vscode.window.showErrorMessage(e.message); 59 vscode.window.showErrorMessage(e.message);
44 } 60 }
@@ -47,4 +63,5 @@ export async function activate(context: vscode.ExtensionContext) {
47 63
48export async function deactivate() { 64export async function deactivate() {
49 await ctx?.client?.stop(); 65 await ctx?.client?.stop();
66 ctx = undefined;
50} 67}
diff --git a/editors/code/src/status_display.ts b/editors/code/src/status_display.ts
index 993e79d70..326b5217b 100644
--- a/editors/code/src/status_display.ts
+++ b/editors/code/src/status_display.ts
@@ -9,7 +9,7 @@ const spinnerFrames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '
9export function activateStatusDisplay(ctx: Ctx) { 9export function activateStatusDisplay(ctx: Ctx) {
10 const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command); 10 const statusDisplay = new StatusDisplay(ctx.config.cargoWatchOptions.command);
11 ctx.pushCleanup(statusDisplay); 11 ctx.pushCleanup(statusDisplay);
12 ctx.onDidRestart(client => ctx.pushCleanup(client.onProgress( 12 ctx.onStart(client => ctx.pushCleanup(client.onProgress(
13 WorkDoneProgress.type, 13 WorkDoneProgress.type,
14 'rustAnalyzer/cargoWatcher', 14 'rustAnalyzer/cargoWatcher',
15 params => statusDisplay.handleProgressNotification(params) 15 params => statusDisplay.handleProgressNotification(params)