aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/main.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/main.ts')
-rw-r--r--editors/code/src/main.ts61
1 files changed, 17 insertions, 44 deletions
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 0c4abdac8..51dedd5ef 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -1,10 +1,8 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient';
3 2
4import * as commands from './commands'; 3import * as commands from './commands';
5import { activateInlayHints } from './inlay_hints'; 4import { activateInlayHints } from './inlay_hints';
6import { StatusDisplay } from './status_display'; 5import { activateStatusDisplay } from './status_display';
7import { Server } from './server';
8import { Ctx } from './ctx'; 6import { Ctx } from './ctx';
9import { activateHighlighting } from './highlighting'; 7import { activateHighlighting } from './highlighting';
10 8
@@ -13,6 +11,17 @@ let ctx!: Ctx;
13export async function activate(context: vscode.ExtensionContext) { 11export async function activate(context: vscode.ExtensionContext) {
14 ctx = new Ctx(context); 12 ctx = new Ctx(context);
15 13
14 // Note: we try to start the server before we register various commands, so
15 // that it registers its `onDidChangeDocument` handler before us.
16 //
17 // This a horribly, horribly wrong way to deal with this problem.
18 try {
19 await ctx.restartServer();
20 } catch (e) {
21 vscode.window.showErrorMessage(e.message);
22 }
23
24
16 // Commands which invokes manually via command pallet, shortcut, etc. 25 // Commands which invokes manually via command pallet, shortcut, etc.
17 ctx.registerCommand('analyzerStatus', commands.analyzerStatus); 26 ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
18 ctx.registerCommand('collectGarbage', commands.collectGarbage); 27 ctx.registerCommand('collectGarbage', commands.collectGarbage);
@@ -22,6 +31,7 @@ export async function activate(context: vscode.ExtensionContext) {
22 ctx.registerCommand('syntaxTree', commands.syntaxTree); 31 ctx.registerCommand('syntaxTree', commands.syntaxTree);
23 ctx.registerCommand('expandMacro', commands.expandMacro); 32 ctx.registerCommand('expandMacro', commands.expandMacro);
24 ctx.registerCommand('run', commands.run); 33 ctx.registerCommand('run', commands.run);
34 ctx.registerCommand('reload', commands.reload);
25 35
26 // Internal commands which are invoked by the server. 36 // Internal commands which are invoked by the server.
27 ctx.registerCommand('runSingle', commands.runSingle); 37 ctx.registerCommand('runSingle', commands.runSingle);
@@ -31,48 +41,11 @@ export async function activate(context: vscode.ExtensionContext) {
31 if (ctx.config.enableEnhancedTyping) { 41 if (ctx.config.enableEnhancedTyping) {
32 ctx.overrideCommand('type', commands.onEnter); 42 ctx.overrideCommand('type', commands.onEnter);
33 } 43 }
34 44 activateStatusDisplay(ctx);
35 const watchStatus = new StatusDisplay(ctx.config.cargoWatchOptions.command);
36 ctx.pushCleanup(watchStatus);
37
38 // Notifications are events triggered by the language server
39 const allNotifications: [string, lc.GenericNotificationHandler][] = [
40 [
41 '$/progress',
42 params => watchStatus.handleProgressNotification(params),
43 ],
44 ];
45
46 const startServer = () => Server.start(allNotifications);
47 const reloadCommand = () => reloadServer(startServer);
48
49 vscode.commands.registerCommand('rust-analyzer.reload', reloadCommand);
50
51 // Start the language server, finally!
52 try {
53 await startServer();
54 } catch (e) {
55 vscode.window.showErrorMessage(e.message);
56 }
57
58 activateHighlighting(ctx); 45 activateHighlighting(ctx);
59 46 activateInlayHints(ctx);
60 if (ctx.config.displayInlayHints) {
61 activateInlayHints(ctx);
62 }
63} 47}
64 48
65export function deactivate(): Thenable<void> { 49export async function deactivate() {
66 if (!Server.client) { 50 await ctx?.client?.stop();
67 return Promise.resolve();
68 }
69 return Server.client.stop();
70}
71
72async function reloadServer(startServer: () => Promise<void>) {
73 if (Server.client != null) {
74 vscode.window.showInformationMessage('Reloading rust-analyzer...');
75 await Server.client.stop();
76 await startServer();
77 }
78} 51}