aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/syntaxTree.ts
blob: c0baf08c51200f6cb31b11c9d1dc216148b15d34 (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
import * as vscode from 'vscode';
import { TextDocumentIdentifier } from 'vscode-languageclient';

import { Server } from '../server';

export const syntaxTreeUri = vscode.Uri.parse('rust-analyzer://syntaxtree');

export class TextDocumentContentProvider
    implements vscode.TextDocumentContentProvider {
    public eventEmitter = new vscode.EventEmitter<vscode.Uri>();
    public syntaxTree: string = 'Not available';

    public provideTextDocumentContent(
        uri: vscode.Uri
    ): vscode.ProviderResult<string> {
        const editor = vscode.window.activeTextEditor;
        if (editor == null) {
            return '';
        }
        const request: SyntaxTreeParams = {
            textDocument: { uri: editor.document.uri.toString() }
        };
        return Server.client.sendRequest<SyntaxTreeResult>(
            'rust-analyzer/syntaxTree',
            request
        );
    }

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

interface SyntaxTreeParams {
    textDocument: TextDocumentIdentifier;
}

type SyntaxTreeResult = string;

// Opens the virtual file that will show the syntax tree
//
// The contents of the file come from the `TextDocumentContentProvider`
export async function handle() {
    const document = await vscode.workspace.openTextDocument(syntaxTreeUri);
    return vscode.window.showTextDocument(
        document,
        vscode.ViewColumn.Two,
        true
    );
}