aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/main.ts
blob: 7ad5e6934aa869b4974ff88017c77302868a631f (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';

import * as commands from './commands';
import { HintsUpdater } from './commands/inlay_hints';
import { StatusDisplay } from './commands/watch_status';
import * as events from './events';
import * as notifications from './notifications';
import { Server } from './server';
import { Ctx } from './ctx';

let ctx!: Ctx;

export async function activate(context: vscode.ExtensionContext) {
    ctx = new Ctx(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('runSingle', commands.runSingle); // Internal action for lenses

    function disposeOnDeactivation(disposable: vscode.Disposable) {
        context.subscriptions.push(disposable);
    }

    function registerCommand(name: string, f: any) {
        disposeOnDeactivation(vscode.commands.registerCommand(name, f));
    }

    registerCommand(
        'rust-analyzer.showReferences',
        (uri: string, position: lc.Position, locations: lc.Location[]) => {
            vscode.commands.executeCommand(
                'editor.action.showReferences',
                vscode.Uri.parse(uri),
                Server.client.protocol2CodeConverter.asPosition(position),
                locations.map(Server.client.protocol2CodeConverter.asLocation),
            );
        },
    );

    if (Server.config.enableEnhancedTyping) {
        ctx.overrideCommand('type', commands.onEnter);
    }

    const watchStatus = new StatusDisplay(
        Server.config.cargoWatchOptions.command,
    );
    disposeOnDeactivation(watchStatus);

    // Notifications are events triggered by the language server
    const allNotifications: [string, lc.GenericNotificationHandler][] = [
        [
            'rust-analyzer/publishDecorations',
            notifications.publishDecorations.handle,
        ],
        [
            '$/progress',
            params => watchStatus.handleProgressNotification(params),
        ],
    ];

    // The events below are plain old javascript events, triggered and handled by vscode
    vscode.window.onDidChangeActiveTextEditor(
        events.changeActiveTextEditor.makeHandler(),
    );

    const startServer = () => Server.start(allNotifications);
    const reloadCommand = () => reloadServer(startServer);

    vscode.commands.registerCommand('rust-analyzer.reload', reloadCommand);

    // Start the language server, finally!
    try {
        await startServer();
    } catch (e) {
        vscode.window.showErrorMessage(e.message);
    }

    if (Server.config.displayInlayHints) {
        const hintsUpdater = new HintsUpdater();
        hintsUpdater.refreshHintsForVisibleEditors().then(() => {
            // vscode may ignore top level hintsUpdater.refreshHintsForVisibleEditors()
            // so update the hints once when the focus changes to guarantee their presence
            let editorChangeDisposable: vscode.Disposable | null = null;
            editorChangeDisposable = vscode.window.onDidChangeActiveTextEditor(
                _ => {
                    if (editorChangeDisposable !== null) {
                        editorChangeDisposable.dispose();
                    }
                    return hintsUpdater.refreshHintsForVisibleEditors();
                },
            );

            disposeOnDeactivation(
                vscode.window.onDidChangeVisibleTextEditors(_ =>
                    hintsUpdater.refreshHintsForVisibleEditors(),
                ),
            );
            disposeOnDeactivation(
                vscode.workspace.onDidChangeTextDocument(e =>
                    hintsUpdater.refreshHintsForVisibleEditors(e),
                ),
            );
            disposeOnDeactivation(
                vscode.workspace.onDidChangeConfiguration(_ =>
                    hintsUpdater.toggleHintsDisplay(
                        Server.config.displayInlayHints,
                    ),
                ),
            );
        });
    }
}

export function deactivate(): Thenable<void> {
    if (!Server.client) {
        return Promise.resolve();
    }
    return Server.client.stop();
}

async function reloadServer(startServer: () => Promise<void>) {
    if (Server.client != null) {
        vscode.window.showInformationMessage('Reloading rust-analyzer...');
        await Server.client.stop();
        await startServer();
    }
}