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