diff options
Diffstat (limited to 'editors/code/src/commands/expand_macro.ts')
-rw-r--r-- | editors/code/src/commands/expand_macro.ts | 102 |
1 files changed, 47 insertions, 55 deletions
diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts index 17c78280a..da208257a 100644 --- a/editors/code/src/commands/expand_macro.ts +++ b/editors/code/src/commands/expand_macro.ts | |||
@@ -1,60 +1,23 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; | 2 | import * as lc from 'vscode-languageclient'; |
3 | import { Server } from '../server'; | ||
4 | 3 | ||
5 | export const expandMacroUri = vscode.Uri.parse( | 4 | import { Ctx, Cmd } from '../ctx'; |
6 | 'rust-analyzer://expandMacro/[EXPANSION].rs', | ||
7 | ); | ||
8 | |||
9 | export class ExpandMacroContentProvider | ||
10 | implements vscode.TextDocumentContentProvider { | ||
11 | public eventEmitter = new vscode.EventEmitter<vscode.Uri>(); | ||
12 | |||
13 | public provideTextDocumentContent( | ||
14 | _uri: vscode.Uri, | ||
15 | ): vscode.ProviderResult<string> { | ||
16 | async function handle() { | ||
17 | const editor = vscode.window.activeTextEditor; | ||
18 | if (editor == null) { | ||
19 | return ''; | ||
20 | } | ||
21 | |||
22 | const position = editor.selection.active; | ||
23 | const request: MacroExpandParams = { | ||
24 | textDocument: { uri: editor.document.uri.toString() }, | ||
25 | position, | ||
26 | }; | ||
27 | const expanded = await Server.client.sendRequest<ExpandedMacro>( | ||
28 | 'rust-analyzer/expandMacro', | ||
29 | request, | ||
30 | ); | ||
31 | |||
32 | if (expanded == null) { | ||
33 | return 'Not available'; | ||
34 | } | ||
35 | |||
36 | return code_format(expanded); | ||
37 | } | ||
38 | |||
39 | return handle(); | ||
40 | } | ||
41 | |||
42 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
43 | return this.eventEmitter.event; | ||
44 | } | ||
45 | } | ||
46 | 5 | ||
47 | // Opens the virtual file that will show the syntax tree | 6 | // Opens the virtual file that will show the syntax tree |
48 | // | 7 | // |
49 | // The contents of the file come from the `TextDocumentContentProvider` | 8 | // The contents of the file come from the `TextDocumentContentProvider` |
50 | export function createHandle(provider: ExpandMacroContentProvider) { | 9 | export function expandMacro(ctx: Ctx): Cmd { |
51 | return async () => { | 10 | const tdcp = new TextDocumentContentProvider(ctx); |
52 | const uri = expandMacroUri; | 11 | ctx.pushCleanup( |
53 | 12 | vscode.workspace.registerTextDocumentContentProvider( | |
54 | const document = await vscode.workspace.openTextDocument(uri); | 13 | 'rust-analyzer', |
55 | 14 | tdcp, | |
56 | provider.eventEmitter.fire(uri); | 15 | ), |
16 | ); | ||
57 | 17 | ||
18 | return async () => { | ||
19 | const document = await vscode.workspace.openTextDocument(tdcp.uri); | ||
20 | tdcp.eventEmitter.fire(tdcp.uri); | ||
58 | return vscode.window.showTextDocument( | 21 | return vscode.window.showTextDocument( |
59 | document, | 22 | document, |
60 | vscode.ViewColumn.Two, | 23 | vscode.ViewColumn.Two, |
@@ -63,11 +26,6 @@ export function createHandle(provider: ExpandMacroContentProvider) { | |||
63 | }; | 26 | }; |
64 | } | 27 | } |
65 | 28 | ||
66 | interface MacroExpandParams { | ||
67 | textDocument: TextDocumentIdentifier; | ||
68 | position: Position; | ||
69 | } | ||
70 | |||
71 | interface ExpandedMacro { | 29 | interface ExpandedMacro { |
72 | name: string; | 30 | name: string; |
73 | expansion: string; | 31 | expansion: string; |
@@ -81,3 +39,37 @@ function code_format(expanded: ExpandedMacro): string { | |||
81 | 39 | ||
82 | return result; | 40 | return result; |
83 | } | 41 | } |
42 | |||
43 | class TextDocumentContentProvider | ||
44 | implements vscode.TextDocumentContentProvider { | ||
45 | private ctx: Ctx; | ||
46 | uri = vscode.Uri.parse('rust-analyzer://expandMacro/[EXPANSION].rs'); | ||
47 | eventEmitter = new vscode.EventEmitter<vscode.Uri>(); | ||
48 | |||
49 | constructor(ctx: Ctx) { | ||
50 | this.ctx = ctx; | ||
51 | } | ||
52 | |||
53 | async provideTextDocumentContent(_uri: vscode.Uri): Promise<string> { | ||
54 | const editor = vscode.window.activeTextEditor; | ||
55 | if (editor == null) return ''; | ||
56 | |||
57 | const position = editor.selection.active; | ||
58 | const request: lc.TextDocumentPositionParams = { | ||
59 | textDocument: { uri: editor.document.uri.toString() }, | ||
60 | position, | ||
61 | }; | ||
62 | const expanded = await this.ctx.client.sendRequest<ExpandedMacro>( | ||
63 | 'rust-analyzer/expandMacro', | ||
64 | request, | ||
65 | ); | ||
66 | |||
67 | if (expanded == null) return 'Not available'; | ||
68 | |||
69 | return code_format(expanded); | ||
70 | } | ||
71 | |||
72 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
73 | return this.eventEmitter.event; | ||
74 | } | ||
75 | } | ||