1 import * as vscode from 'vscode';
3 import * as commands from './commands';
4 import { activateInlayHints } from './inlay_hints';
5 import { activateStatusDisplay } from './status_display';
6 import { Ctx } from './ctx';
7 import { activateHighlighting } from './highlighting';
8 import { ensureServerBinary } from './installation/server';
9 import { Config } from './config';
10 import { log } from './util';
11 import { ensureProperExtensionVersion } from './installation/extension';
13 let ctx: Ctx | undefined;
15 export async function activate(context: vscode.ExtensionContext) {
16 // Register a "dumb" onEnter command for the case where server fails to
19 // FIXME: refactor command registration code such that commands are
20 // **always** registered, even if the server does not start. Use API like
25 // factory: (Ctx) => ((Ctx) => any),
26 // fallback: () => any = () => vscode.window.showErrorMessage(
27 // "rust-analyzer is not available"
30 const defaultOnEnter = vscode.commands.registerCommand(
31 'rust-analyzer.onEnter',
32 () => vscode.commands.executeCommand('default:type', { text: '\n' }),
34 context.subscriptions.push(defaultOnEnter);
36 const config = new Config(context);
38 vscode.workspace.onDidChangeConfiguration(() => ensureProperExtensionVersion(config).catch(log.error));
40 // Don't await the user response here, otherwise we will block the lsp server bootstrap
41 void ensureProperExtensionVersion(config).catch(log.error);
43 const serverPath = await ensureServerBinary(config);
45 if (serverPath == null) {
47 "Rust Analyzer Language Server is not available. " +
48 "Please, ensure its [proper installation](https://rust-analyzer.github.io/manual.html#installation)."
52 // Note: we try to start the server before we activate type hints so that it
53 // registers its `onDidChangeDocument` handler before us.
55 // This a horribly, horribly wrong way to deal with this problem.
56 ctx = await Ctx.create(config, context, serverPath);
58 // Commands which invokes manually via command palette, shortcut, etc.
59 ctx.registerCommand('reload', (ctx) => {
61 vscode.window.showInformationMessage('Reloading rust-analyzer...');
63 // https://github.com/microsoft/vscode/issues/45774#issuecomment-373423895
65 for (const sub of ctx.subscriptions) {
72 await activate(context);
76 ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
77 ctx.registerCommand('collectGarbage', commands.collectGarbage);
78 ctx.registerCommand('matchingBrace', commands.matchingBrace);
79 ctx.registerCommand('joinLines', commands.joinLines);
80 ctx.registerCommand('parentModule', commands.parentModule);
81 ctx.registerCommand('syntaxTree', commands.syntaxTree);
82 ctx.registerCommand('expandMacro', commands.expandMacro);
83 ctx.registerCommand('run', commands.run);
85 defaultOnEnter.dispose();
86 ctx.registerCommand('onEnter', commands.onEnter);
88 ctx.registerCommand('ssr', commands.ssr);
89 ctx.registerCommand('serverVersion', commands.serverVersion);
91 // Internal commands which are invoked by the server.
92 ctx.registerCommand('runSingle', commands.runSingle);
93 ctx.registerCommand('debugSingle', commands.debugSingle);
94 ctx.registerCommand('showReferences', commands.showReferences);
95 ctx.registerCommand('applySourceChange', commands.applySourceChange);
96 ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange);
98 activateStatusDisplay(ctx);
100 if (!ctx.config.highlightingSemanticTokens) {
101 activateHighlighting(ctx);
103 activateInlayHints(ctx);
106 export async function deactivate() {
107 await ctx?.client?.stop();