blob: 34e0c8fb337a84cced6878c3f584bbe75bb59041 (
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
|
import * as vscode from 'vscode';
import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
import { Server } from '../server';
export const expandMacroUri = vscode.Uri.parse(
'rust-analyzer://expandMacro/[EXPANSION].rs'
);
export class ExpandMacroContentProvider
implements vscode.TextDocumentContentProvider {
public eventEmitter = new vscode.EventEmitter<vscode.Uri>();
public provideTextDocumentContent(
uri: vscode.Uri
): vscode.ProviderResult<string> {
async function handle() {
const editor = vscode.window.activeTextEditor;
if (editor == null) {
return '';
}
const position = editor.selection.active;
const request: MacroExpandParams = {
textDocument: { uri: editor.document.uri.toString() },
position
};
const expanded = await Server.client.sendRequest<ExpandedMacro>(
'rust-analyzer/expandMacro',
request
);
if (expanded == null) {
return 'Not available';
}
return code_format(expanded);
}
return handle();
}
get onDidChange(): vscode.Event<vscode.Uri> {
return this.eventEmitter.event;
}
}
// Opens the virtual file that will show the syntax tree
//
// The contents of the file come from the `TextDocumentContentProvider`
export function createHandle(provider: ExpandMacroContentProvider) {
return async () => {
const uri = expandMacroUri;
const document = await vscode.workspace.openTextDocument(uri);
provider.eventEmitter.fire(uri);
return vscode.window.showTextDocument(
document,
vscode.ViewColumn.Two,
true
);
};
}
interface MacroExpandParams {
textDocument: TextDocumentIdentifier;
position: Position;
}
interface ExpandedMacro {
name: string;
expansion: string;
}
function code_format(expanded: ExpandedMacro): string {
let result = `// Recursive expansion of ${expanded.name}! macro\n`;
result += '// ' + '='.repeat(result.length - 3);
result += '\n\n';
result += expanded.expansion;
return result;
}
|