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 { Server } from './server';
8 import { Ctx } from './ctx';
9 import { activateHighlighting } from './highlighting';
13 export async function activate(context: vscode.ExtensionContext) {
14 ctx = new Ctx(context);
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);
26 // Internal commands which are invoked by the server.
27 ctx.registerCommand('runSingle', commands.runSingle);
28 ctx.registerCommand('showReferences', commands.showReferences);
30 if (ctx.config.enableEnhancedTyping) {
31 ctx.overrideCommand('type', commands.onEnter);
34 const watchStatus = new StatusDisplay(ctx.config.cargoWatchOptions.command);
35 ctx.pushCleanup(watchStatus);
37 // Notifications are events triggered by the language server
38 const allNotifications: [string, lc.GenericNotificationHandler][] = [
41 params => watchStatus.handleProgressNotification(params),
45 const startServer = () => Server.start(allNotifications);
46 const reloadCommand = () => reloadServer(startServer);
48 vscode.commands.registerCommand('rust-analyzer.reload', reloadCommand);
50 // Start the language server, finally!
54 vscode.window.showErrorMessage(e.message);
57 activateHighlighting(ctx);
59 if (ctx.config.displayInlayHints) {
60 activateInlayHints(ctx);
64 export function deactivate(): Thenable<void> {
66 return Promise.resolve();
68 return Server.client.stop();
71 async function reloadServer(startServer: () => Promise<void>) {
72 if (Server.client != null) {
73 vscode.window.showInformationMessage('Reloading rust-analyzer...');
74 await Server.client.stop();