1 import * as vscode from 'vscode';
2 import * as lc from 'vscode-languageclient';
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';
14 export async function activate(context: vscode.ExtensionContext) {
15 ctx = new Ctx(context);
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);
27 // Internal commands which are invoked by the server.
28 ctx.registerCommand('runSingle', commands.runSingle);
29 ctx.registerCommand('showReferences', commands.showReferences);
31 if (Server.config.enableEnhancedTyping) {
32 ctx.overrideCommand('type', commands.onEnter);
35 const watchStatus = new StatusDisplay(
36 Server.config.cargoWatchOptions.command,
38 ctx.pushCleanup(watchStatus);
40 // Notifications are events triggered by the language server
41 const allNotifications: [string, lc.GenericNotificationHandler][] = [
43 'rust-analyzer/publishDecorations',
44 notifications.publishDecorations.handle,
48 params => watchStatus.handleProgressNotification(params),
52 // The events below are plain old javascript events, triggered and handled by vscode
53 vscode.window.onDidChangeActiveTextEditor(
54 events.changeActiveTextEditor.makeHandler(),
57 const startServer = () => Server.start(allNotifications);
58 const reloadCommand = () => reloadServer(startServer);
60 vscode.commands.registerCommand('rust-analyzer.reload', reloadCommand);
62 // Start the language server, finally!
66 vscode.window.showErrorMessage(e.message);
69 if (Server.config.displayInlayHints) {
70 activateInlayHints(ctx);
74 export function deactivate(): Thenable<void> {
76 return Promise.resolve();
78 return Server.client.stop();
81 async function reloadServer(startServer: () => Promise<void>) {
82 if (Server.client != null) {
83 vscode.window.showInformationMessage('Reloading rust-analyzer...');
84 await Server.client.stop();