diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-05-25 11:17:40 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-05-25 11:17:40 +0100 |
commit | e4f91bfa578e57c1ef4be3343ebb4e8950e5dae6 (patch) | |
tree | cb0ab7180b97dfdf8347c15d6e445628f7802367 /editors/code/src/commands/expand_macro.ts | |
parent | 1527feb744c7911b6ca482554f0399d3ef0ebfdc (diff) | |
parent | 6058b8b0f6a24ad5b905d99d780a31b9e3d578d7 (diff) |
Merge #4605
4605: Reorganize TypeScript r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'editors/code/src/commands/expand_macro.ts')
-rw-r--r-- | editors/code/src/commands/expand_macro.ts | 66 |
1 files changed, 0 insertions, 66 deletions
diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts deleted file mode 100644 index 23f2ef1d5..000000000 --- a/editors/code/src/commands/expand_macro.ts +++ /dev/null | |||
@@ -1,66 +0,0 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as ra from '../rust-analyzer-api'; | ||
3 | |||
4 | import { Ctx, Cmd } from '../ctx'; | ||
5 | |||
6 | // Opens the virtual file that will show the syntax tree | ||
7 | // | ||
8 | // The contents of the file come from the `TextDocumentContentProvider` | ||
9 | export function expandMacro(ctx: Ctx): Cmd { | ||
10 | const tdcp = new TextDocumentContentProvider(ctx); | ||
11 | ctx.pushCleanup( | ||
12 | vscode.workspace.registerTextDocumentContentProvider( | ||
13 | 'rust-analyzer', | ||
14 | tdcp, | ||
15 | ), | ||
16 | ); | ||
17 | |||
18 | return async () => { | ||
19 | const document = await vscode.workspace.openTextDocument(tdcp.uri); | ||
20 | tdcp.eventEmitter.fire(tdcp.uri); | ||
21 | return vscode.window.showTextDocument( | ||
22 | document, | ||
23 | vscode.ViewColumn.Two, | ||
24 | true, | ||
25 | ); | ||
26 | }; | ||
27 | } | ||
28 | |||
29 | function codeFormat(expanded: ra.ExpandedMacro): string { | ||
30 | let result = `// Recursive expansion of ${expanded.name}! macro\n`; | ||
31 | result += '// ' + '='.repeat(result.length - 3); | ||
32 | result += '\n\n'; | ||
33 | result += expanded.expansion; | ||
34 | |||
35 | return result; | ||
36 | } | ||
37 | |||
38 | class TextDocumentContentProvider | ||
39 | implements vscode.TextDocumentContentProvider { | ||
40 | uri = vscode.Uri.parse('rust-analyzer://expandMacro/[EXPANSION].rs'); | ||
41 | eventEmitter = new vscode.EventEmitter<vscode.Uri>(); | ||
42 | |||
43 | constructor(private readonly ctx: Ctx) { | ||
44 | } | ||
45 | |||
46 | async provideTextDocumentContent(_uri: vscode.Uri): Promise<string> { | ||
47 | const editor = vscode.window.activeTextEditor; | ||
48 | const client = this.ctx.client; | ||
49 | if (!editor || !client) return ''; | ||
50 | |||
51 | const position = editor.selection.active; | ||
52 | |||
53 | const expanded = await client.sendRequest(ra.expandMacro, { | ||
54 | textDocument: { uri: editor.document.uri.toString() }, | ||
55 | position, | ||
56 | }); | ||
57 | |||
58 | if (expanded == null) return 'Not available'; | ||
59 | |||
60 | return codeFormat(expanded); | ||
61 | } | ||
62 | |||
63 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
64 | return this.eventEmitter.event; | ||
65 | } | ||
66 | } | ||