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