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

import { Ctx, Cmd } from '../ctx';

// Opens the virtual file that will show the syntax tree
//
// The contents of the file come from the `TextDocumentContentProvider`
export function syntaxTree(ctx: Ctx): Cmd {
    const tdcp = new TextDocumentContentProvider(ctx);

    ctx.pushCleanup(
        vscode.workspace.registerTextDocumentContentProvider(
            'rust-analyzer',
            tdcp,
        ),
    );

    vscode.workspace.onDidChangeTextDocument(
        (event: vscode.TextDocumentChangeEvent) => {
            const doc = event.document;
            if (doc.languageId !== 'rust') return;
            afterLs(() => tdcp.eventEmitter.fire(tdcp.uri));
        },
        ctx.subscriptions,
    );

    vscode.window.onDidChangeActiveTextEditor(
        (editor: vscode.TextEditor | undefined) => {
            if (!editor || editor.document.languageId !== 'rust') return;
            tdcp.eventEmitter.fire(tdcp.uri);
        },
        ctx.subscriptions,
    );

    return async () => {
        const editor = vscode.window.activeTextEditor;
        const rangeEnabled = !!(editor && !editor.selection.isEmpty);

        const uri = rangeEnabled
            ? vscode.Uri.parse(`${tdcp.uri.toString()}?range=true`)
            : tdcp.uri;

        const document = await vscode.workspace.openTextDocument(uri);

        tdcp.eventEmitter.fire(uri);

        return vscode.window.showTextDocument(
            document,
            vscode.ViewColumn.Two,
            true,
        );
    };
}

// We need to order this after LS updates, but there's no API for that.
// Hence, good old setTimeout.
function afterLs(f: () => void) {
    setTimeout(f, 10);
}

interface SyntaxTreeParams {
    textDocument: lc.TextDocumentIdentifier;
    range?: lc.Range;
}

class TextDocumentContentProvider
    implements vscode.TextDocumentContentProvider {
    private ctx: Ctx;
    uri = vscode.Uri.parse('rust-analyzer://syntaxtree');
    eventEmitter = new vscode.EventEmitter<vscode.Uri>();

    constructor(ctx: Ctx) {
        this.ctx = ctx;
    }

    provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> {
        const editor = vscode.window.activeTextEditor;
        const client = this.ctx.client;
        if (!editor || !client) return '';

        let range: lc.Range | undefined;

        // When the range based query is enabled we take the range of the selection
        if (uri.query === 'range=true') {
            range = editor.selection.isEmpty
                ? undefined
                : client.code2ProtocolConverter.asRange(editor.selection);
        }

        const request: SyntaxTreeParams = {
            textDocument: { uri: editor.document.uri.toString() },
            range,
        };
        return client.sendRequest<string>(
            'rust-analyzer/syntaxTree',
            request,
        );
    }

    get onDidChange(): vscode.Event<vscode.Uri> {
        return this.eventEmitter.event;
    }
}