aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands/expand_macro.ts
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-11-19 17:06:10 +0000
committerEdwin Cheng <[email protected]>2019-11-19 17:06:10 +0000
commitd16cc223e100ae9a29384f104a378932088e0c3c (patch)
treefda3e9fc76db85ae902a11a200a762030863a74f /editors/code/src/commands/expand_macro.ts
parent80fe467ce864de077d2f5a4e29e1a4d5fe535dc3 (diff)
Use DocumentProvider instead of Hover
Diffstat (limited to 'editors/code/src/commands/expand_macro.ts')
-rw-r--r--editors/code/src/commands/expand_macro.ts81
1 files changed, 58 insertions, 23 deletions
diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts
index 3fc3e0391..1fa2cf739 100644
--- a/editors/code/src/commands/expand_macro.ts
+++ b/editors/code/src/commands/expand_macro.ts
@@ -2,47 +2,82 @@ import * as vscode from 'vscode';
2import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; 2import { Position, TextDocumentIdentifier } from 'vscode-languageclient';
3import { Server } from '../server'; 3import { Server } from '../server';
4 4
5interface ExpandedMacro { 5export const expandMacroUri = vscode.Uri.parse(
6 name: string, 6 'rust-analyzer://expandMacro/[EXPANSION].rs'
7 expansion: string, 7);
8}
9 8
10function code_format(expanded: ExpandedMacro): vscode.MarkdownString { 9export class ExpandMacroContentProvider
11 const markdown = new vscode.MarkdownString( 10 implements vscode.TextDocumentContentProvider {
12 `#### Recursive expansion of ${expanded.name}! macro` 11 public eventEmitter = new vscode.EventEmitter<vscode.Uri>();
13 );
14 markdown.appendCodeblock(expanded.expansion, 'rust');
15 return markdown;
16}
17 12
18export class ExpandMacroHoverProvider implements vscode.HoverProvider { 13 public provideTextDocumentContent(
19 public provideHover( 14 uri: vscode.Uri
20 document: vscode.TextDocument, 15 ): vscode.ProviderResult<string> {
21 position: vscode.Position,
22 token: vscode.CancellationToken
23 ): Thenable<vscode.Hover | null> | null {
24 async function handle() { 16 async function handle() {
17 const editor = vscode.window.activeTextEditor;
18 if (editor == null) {
19 return '';
20 }
21
22 const position = editor.selection.active;
25 const request: MacroExpandParams = { 23 const request: MacroExpandParams = {
26 textDocument: { uri: document.uri.toString() }, 24 textDocument: { uri: editor.document.uri.toString() },
27 position 25 position
28 }; 26 };
29 const result = await Server.client.sendRequest<ExpandedMacro>( 27 const expanded = await Server.client.sendRequest<ExpandedMacro>(
30 'rust-analyzer/expandMacro', 28 'rust-analyzer/expandMacro',
31 request 29 request
32 ); 30 );
33 if (result != null) { 31
34 const formated = code_format(result); 32 if (expanded == null) {
35 return new vscode.Hover(formated); 33 return 'Not available';
36 } 34 }
37 35
38 return null; 36 return code_format(expanded);
39 } 37 }
40 38
41 return handle(); 39 return handle();
42 } 40 }
41
42 get onDidChange(): vscode.Event<vscode.Uri> {
43 return this.eventEmitter.event;
44 }
45}
46
47// Opens the virtual file that will show the syntax tree
48//
49// The contents of the file come from the `TextDocumentContentProvider`
50export function createHandle(provider: ExpandMacroContentProvider) {
51 return async () => {
52 const uri = expandMacroUri;
53
54 const document = await vscode.workspace.openTextDocument(uri);
55
56 provider.eventEmitter.fire(uri);
57
58 return vscode.window.showTextDocument(
59 document,
60 vscode.ViewColumn.Two,
61 true
62 );
63 };
43} 64}
44 65
45interface MacroExpandParams { 66interface MacroExpandParams {
46 textDocument: TextDocumentIdentifier; 67 textDocument: TextDocumentIdentifier;
47 position: Position; 68 position: Position;
48} 69}
70
71interface ExpandedMacro {
72 name: string;
73 expansion: string;
74}
75
76function code_format(expanded: ExpandedMacro): string {
77 let result = `// Recursive expansion of ${expanded.name}! macro\n`;
78 result += '='.repeat(result.length);
79 result += '\n\n';
80 result += expanded.expansion;
81
82 return result;
83}