aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/extension.ts
blob: 4acd54d90701629fe431577f529e31f6610b9a51 (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
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';

import * as commands from './commands';
import { TextDocumentContentProvider } from './commands/syntaxTree';
import * as events from './events';
import * as notifications from './notifications';
import { Server } from './server';

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

    function registerCommand(name: string, f: any) {
        disposeOnDeactivation(vscode.commands.registerCommand(name, f));
    }
    function overrideCommand(
        name: string,
        f: (...args: any[]) => Promise<boolean>
    ) {
        const defaultCmd = `default:${name}`;
        const original = (...args: any[]) =>
            vscode.commands.executeCommand(defaultCmd, ...args);

        try {
            registerCommand(name, async (...args: any[]) => {
                const editor = vscode.window.activeTextEditor;
                if (
                    !editor ||
                    !editor.document ||
                    editor.document.languageId !== 'rust'
                ) {
                    return await original(...args);
                }
                if (!(await f(...args))) {
                    return await original(...args);
                }
            });
        } catch(_) {
            vscode.window.showWarningMessage('Enhanced typing feature is disabled because of incompatibility with VIM extension');
        }
    }

    // Commands are requests from vscode to the language server
    registerCommand('ra-lsp.syntaxTree', commands.syntaxTree.handle);
    registerCommand('ra-lsp.extendSelection', commands.extendSelection.handle);
    registerCommand('ra-lsp.matchingBrace', commands.matchingBrace.handle);
    registerCommand('ra-lsp.joinLines', commands.joinLines.handle);
    registerCommand('ra-lsp.parentModule', commands.parentModule.handle);
    registerCommand('ra-lsp.run', commands.runnables.handle);
    registerCommand(
        'ra-lsp.applySourceChange',
        commands.applySourceChange.handle
    );
    overrideCommand('type', commands.onEnter.handle);

    // Notifications are events triggered by the language server
    const allNotifications: Iterable<
        [string, lc.GenericNotificationHandler]
    > = [['m/publishDecorations', notifications.publishDecorations.handle]];

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

    const textDocumentContentProvider = new TextDocumentContentProvider();
    disposeOnDeactivation(
        vscode.workspace.registerTextDocumentContentProvider(
            'ra-lsp',
            textDocumentContentProvider
        )
    );

    vscode.workspace.onDidChangeTextDocument(
        events.changeTextDocument.createHandler(textDocumentContentProvider),
        null,
        context.subscriptions
    );

    // Start the language server, finally!
    Server.start(allNotifications);
}

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