]> git.lizzy.rs Git - rust.git/blob - editors/code/src/main.ts
ecf53cf775fed63580b5d31accdf91f3fca274d7
[rust.git] / editors / code / src / main.ts
1 import * as vscode from 'vscode';
2
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
12 let ctx: Ctx | undefined;
13
14 export async function activate(context: vscode.ExtensionContext) {
15     // Register a "dumb" onEnter command for the case where server fails to
16     // start.
17     //
18     // FIXME: refactor command registration code such that commands are
19     // **always** registered, even if the server does not start. Use API like
20     // this perhaps?
21     //
22     // ```TypeScript
23     // registerCommand(
24     //    factory: (Ctx) => ((Ctx) => any),
25     //    fallback: () => any = () => vscode.window.showErrorMessage(
26     //        "rust-analyzer is not available"
27     //    ),
28     // )
29     const defaultOnEnter = vscode.commands.registerCommand(
30         'rust-analyzer.onEnter',
31         () => vscode.commands.executeCommand('default:type', { text: '\n' }),
32     );
33     context.subscriptions.push(defaultOnEnter);
34
35     const config = new Config(context);
36
37     const serverPath = await ensureServerBinary(config.serverSource);
38     if (serverPath == null) {
39         throw new Error(
40             "Rust Analyzer Language Server is not available. " +
41             "Please, ensure its [proper installation](https://rust-analyzer.github.io/manual.html#installation)."
42         );
43     }
44
45     // Note: we try to start the server before we activate type hints so that it
46     // registers its `onDidChangeDocument` handler before us.
47     //
48     // This a horribly, horribly wrong way to deal with this problem.
49     ctx = await Ctx.create(config, context, serverPath);
50
51     // Commands which invokes manually via command palette, shortcut, etc.
52     ctx.registerCommand('reload', (ctx) => {
53         return async () => {
54             vscode.window.showInformationMessage('Reloading rust-analyzer...');
55             // @DanTup maneuver
56             // https://github.com/microsoft/vscode/issues/45774#issuecomment-373423895
57             await deactivate();
58             for (const sub of ctx.subscriptions) {
59                 try {
60                     sub.dispose();
61                 } catch (e) {
62                     log.error(e);
63                 }
64             }
65             await activate(context);
66         };
67     });
68
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);
77
78     defaultOnEnter.dispose();
79     ctx.registerCommand('onEnter', commands.onEnter);
80
81     ctx.registerCommand('ssr', commands.ssr);
82     ctx.registerCommand('serverVersion', commands.serverVersion);
83
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);
89
90     activateStatusDisplay(ctx);
91
92     if (!ctx.config.highlightingSemanticTokens) {
93         activateHighlighting(ctx);
94     }
95     activateInlayHints(ctx);
96 }
97
98 export async function deactivate() {
99     await ctx?.client?.stop();
100     ctx = undefined;
101 }