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

type ExpandMacroResult = [string, string]

function code_format([name, text]: [string, string]): vscode.MarkdownString {
    const markdown = new vscode.MarkdownString(`#### Recursive expansion of ${name}! macro`);
    markdown.appendCodeblock(text, 'rust');    
    return markdown;
}

export class ExpandMacroHoverProvider implements vscode.HoverProvider {
    public provideHover(
        document: vscode.TextDocument,
        position: vscode.Position,
        token: vscode.CancellationToken,
    ): Thenable<vscode.Hover | null> | null {
        async function handle() {
            const request: MacroExpandParams = {
                textDocument: { uri: document.uri.toString() },
                position,
            };
            const result = await Server.client.sendRequest<ExpandMacroResult>(
                'rust-analyzer/expandMacro',
                request
            );
            if (result != null) {
                const formated = code_format(result);
                return new vscode.Hover(formated);
            }

            return null;
        };

        return handle();
    }
}


interface MacroExpandParams {
    textDocument: TextDocumentIdentifier;
    position: Position;
}