aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/main.ts
blob: 7b3bb6302c062e79068f0dd220a414dc4b7da71f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import * as vscode from 'vscode';

import * as commands from './commands';
import { activateInlayHints } from './inlay_hints';
import { activateStatusDisplay } from './status_display';
import { Ctx } from './ctx';
import { activateHighlighting } from './highlighting';
import { ensureServerBinary } from './installation/server';
import { Config } from './config';
import { log } from './util';

let ctx: Ctx | undefined;

export async function activate(context: vscode.ExtensionContext) {
    const config = new Config(context);

    const serverPath = await ensureServerBinary(config.serverSource);
    if (serverPath == null) {
        throw new Error(
            "Rust Analyzer Language Server is not available. " +
            "Please, ensure its [proper installation](https://rust-analyzer.github.io/manual.html#installation)."
        );
    }

    // Note: we try to start the server before we activate type hints so that it
    // registers its `onDidChangeDocument` handler before us.
    //
    // This a horribly, horribly wrong way to deal with this problem.
    ctx = await Ctx.create(config, context, serverPath);

    // Commands which invokes manually via command palette, shortcut, etc.
    ctx.registerCommand('reload', (ctx) => {
        return async () => {
            vscode.window.showInformationMessage('Reloading rust-analyzer...');
            // @DanTup maneuver
            // https://github.com/microsoft/vscode/issues/45774#issuecomment-373423895
            await deactivate();
            for (const sub of ctx.subscriptions) {
                try {
                    sub.dispose();
                } catch (e) {
                    log.error(e);
                }
            }
            await activate(context);
        };
    });

    ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
    ctx.registerCommand('collectGarbage', commands.collectGarbage);
    ctx.registerCommand('matchingBrace', commands.matchingBrace);
    ctx.registerCommand('joinLines', commands.joinLines);
    ctx.registerCommand('parentModule', commands.parentModule);
    ctx.registerCommand('syntaxTree', commands.syntaxTree);
    ctx.registerCommand('expandMacro', commands.expandMacro);
    ctx.registerCommand('run', commands.run);
    ctx.registerCommand('onEnter', commands.onEnter);
    ctx.registerCommand('ssr', commands.ssr);
    ctx.registerCommand('serverVersion', commands.serverVersion);

    // Internal commands which are invoked by the server.
    ctx.registerCommand('runSingle', commands.runSingle);
    ctx.registerCommand('showReferences', commands.showReferences);
    ctx.registerCommand('applySourceChange', commands.applySourceChange);
    ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange);

    activateStatusDisplay(ctx);

    activateHighlighting(ctx);
    activateInlayHints(ctx);
}

export async function deactivate() {
    await ctx?.client?.stop();
    ctx = undefined;
}