]> git.lizzy.rs Git - rust.git/blob - editors/code/src/main.ts
Privatize highlighting
[rust.git] / editors / code / src / main.ts
1 import * as vscode from 'vscode';
2 import * as lc from 'vscode-languageclient';
3
4 import * as commands from './commands';
5 import { activateInlayHints } from './inlay_hints';
6 import { StatusDisplay } from './status_display';
7 import { Server } from './server';
8 import { Ctx } from './ctx';
9 import { activateHighlighting } from './highlighting';
10
11 let ctx!: Ctx;
12
13 export async function activate(context: vscode.ExtensionContext) {
14     ctx = new Ctx(context);
15
16     // Commands which invokes manually via command pallet, shortcut, etc.
17     ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
18     ctx.registerCommand('collectGarbage', commands.collectGarbage);
19     ctx.registerCommand('matchingBrace', commands.matchingBrace);
20     ctx.registerCommand('joinLines', commands.joinLines);
21     ctx.registerCommand('parentModule', commands.parentModule);
22     ctx.registerCommand('syntaxTree', commands.syntaxTree);
23     ctx.registerCommand('expandMacro', commands.expandMacro);
24     ctx.registerCommand('run', commands.run);
25
26     // Internal commands which are invoked by the server.
27     ctx.registerCommand('runSingle', commands.runSingle);
28     ctx.registerCommand('showReferences', commands.showReferences);
29
30     if (ctx.config.enableEnhancedTyping) {
31         ctx.overrideCommand('type', commands.onEnter);
32     }
33
34     const watchStatus = new StatusDisplay(ctx.config.cargoWatchOptions.command);
35     ctx.pushCleanup(watchStatus);
36
37     // Notifications are events triggered by the language server
38     const allNotifications: [string, lc.GenericNotificationHandler][] = [
39         [
40             '$/progress',
41             params => watchStatus.handleProgressNotification(params),
42         ],
43     ];
44
45     const startServer = () => Server.start(allNotifications);
46     const reloadCommand = () => reloadServer(startServer);
47
48     vscode.commands.registerCommand('rust-analyzer.reload', reloadCommand);
49
50     // Start the language server, finally!
51     try {
52         await startServer();
53     } catch (e) {
54         vscode.window.showErrorMessage(e.message);
55     }
56
57     activateHighlighting(ctx);
58
59     if (ctx.config.displayInlayHints) {
60         activateInlayHints(ctx);
61     }
62 }
63
64 export function deactivate(): Thenable<void> {
65     if (!Server.client) {
66         return Promise.resolve();
67     }
68     return Server.client.stop();
69 }
70
71 async function reloadServer(startServer: () => Promise<void>) {
72     if (Server.client != null) {
73         vscode.window.showInformationMessage('Reloading rust-analyzer...');
74         await Server.client.stop();
75         await startServer();
76     }
77 }