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';
12 let ctx: Ctx | undefined;
14 export async function activate(context: vscode.ExtensionContext) {
15 // Register a "dumb" onEnter command for the case where server fails to
18 // FIXME: refactor command registration code such that commands are
19 // **always** registered, even if the server does not start. Use API like
24 // factory: (Ctx) => ((Ctx) => any),
25 // fallback: () => any = () => vscode.window.showErrorMessage(
26 // "rust-analyzer is not available"
29 const defaultOnEnter = vscode.commands.registerCommand(
30 'rust-analyzer.onEnter',
31 () => vscode.commands.executeCommand('default:type', { text: '\n' }),
33 context.subscriptions.push(defaultOnEnter);
35 const config = new Config(context);
37 const serverPath = await ensureServerBinary(config.serverSource);
38 if (serverPath == null) {
40 "Rust Analyzer Language Server is not available. " +
41 "Please, ensure its [proper installation](https://rust-analyzer.github.io/manual.html#installation)."
45 // Note: we try to start the server before we activate type hints so that it
46 // registers its `onDidChangeDocument` handler before us.
48 // This a horribly, horribly wrong way to deal with this problem.
49 ctx = await Ctx.create(config, context, serverPath);
51 // Commands which invokes manually via command palette, shortcut, etc.
52 ctx.registerCommand('reload', (ctx) => {
54 vscode.window.showInformationMessage('Reloading rust-analyzer...');
56 // https://github.com/microsoft/vscode/issues/45774#issuecomment-373423895
58 for (const sub of ctx.subscriptions) {
65 await activate(context);
69 ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
70 ctx.registerCommand('collectGarbage', commands.collectGarbage);
71 ctx.registerCommand('matchingBrace', commands.matchingBrace);
72 ctx.registerCommand('joinLines', commands.joinLines);
73 ctx.registerCommand('parentModule', commands.parentModule);
74 ctx.registerCommand('syntaxTree', commands.syntaxTree);
75 ctx.registerCommand('expandMacro', commands.expandMacro);
76 ctx.registerCommand('run', commands.run);
78 defaultOnEnter.dispose();
79 ctx.registerCommand('onEnter', commands.onEnter);
81 ctx.registerCommand('ssr', commands.ssr);
82 ctx.registerCommand('serverVersion', commands.serverVersion);
84 // Internal commands which are invoked by the server.
85 ctx.registerCommand('runSingle', commands.runSingle);
86 ctx.registerCommand('showReferences', commands.showReferences);
87 ctx.registerCommand('applySourceChange', commands.applySourceChange);
88 ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange);
90 activateStatusDisplay(ctx);
92 if (!ctx.config.highlightingSemanticTokens) {
93 activateHighlighting(ctx);
95 activateInlayHints(ctx);
98 export async function deactivate() {
99 await ctx?.client?.stop();