aboutsummaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
Diffstat (limited to 'editors')
-rw-r--r--editors/code/package.json5
-rw-r--r--editors/code/src/commands/expand_macro.ts81
-rw-r--r--editors/code/src/extension.ts22
3 files changed, 75 insertions, 33 deletions
diff --git a/editors/code/package.json b/editors/code/package.json
index ee997e58f..94887674b 100644
--- a/editors/code/package.json
+++ b/editors/code/package.json
@@ -91,6 +91,11 @@
91 "category": "Rust Analyzer" 91 "category": "Rust Analyzer"
92 }, 92 },
93 { 93 {
94 "command": "rust-analyzer.expandMacro",
95 "title": "Expand macro recursively",
96 "category": "Rust Analyzer"
97 },
98 {
94 "command": "rust-analyzer.matchingBrace", 99 "command": "rust-analyzer.matchingBrace",
95 "title": "Find matching brace", 100 "title": "Find matching brace",
96 "category": "Rust Analyzer" 101 "category": "Rust Analyzer"
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}
diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts
index 8654b6030..683497dfd 100644
--- a/editors/code/src/extension.ts
+++ b/editors/code/src/extension.ts
@@ -3,7 +3,7 @@ import * as lc from 'vscode-languageclient';
3 3
4import * as commands from './commands'; 4import * as commands from './commands';
5import { CargoWatchProvider } from './commands/cargo_watch'; 5import { CargoWatchProvider } from './commands/cargo_watch';
6import { ExpandMacroHoverProvider } from './commands/expand_macro'; 6import { ExpandMacroContentProvider } from './commands/expand_macro';
7import { HintsUpdater } from './commands/inlay_hints'; 7import { HintsUpdater } from './commands/inlay_hints';
8import { 8import {
9 interactivelyStartCargoWatch, 9 interactivelyStartCargoWatch,
@@ -98,6 +98,7 @@ export function activate(context: vscode.ExtensionContext) {
98 ] 98 ]
99 ]; 99 ];
100 const syntaxTreeContentProvider = new SyntaxTreeContentProvider(); 100 const syntaxTreeContentProvider = new SyntaxTreeContentProvider();
101 const expandMacroContentProvider = new ExpandMacroContentProvider();
101 102
102 // The events below are plain old javascript events, triggered and handled by vscode 103 // The events below are plain old javascript events, triggered and handled by vscode
103 vscode.window.onDidChangeActiveTextEditor( 104 vscode.window.onDidChangeActiveTextEditor(
@@ -110,11 +111,21 @@ export function activate(context: vscode.ExtensionContext) {
110 syntaxTreeContentProvider 111 syntaxTreeContentProvider
111 ) 112 )
112 ); 113 );
114 disposeOnDeactivation(
115 vscode.workspace.registerTextDocumentContentProvider(
116 'rust-analyzer',
117 expandMacroContentProvider
118 )
119 );
113 120
114 registerCommand( 121 registerCommand(
115 'rust-analyzer.syntaxTree', 122 'rust-analyzer.syntaxTree',
116 commands.syntaxTree.createHandle(syntaxTreeContentProvider) 123 commands.syntaxTree.createHandle(syntaxTreeContentProvider)
117 ); 124 );
125 registerCommand(
126 'rust-analyzer.expandMacro',
127 commands.expandMacro.createHandle(expandMacroContentProvider)
128 );
118 129
119 vscode.workspace.onDidChangeTextDocument( 130 vscode.workspace.onDidChangeTextDocument(
120 events.changeTextDocument.createHandler(syntaxTreeContentProvider), 131 events.changeTextDocument.createHandler(syntaxTreeContentProvider),
@@ -122,15 +133,6 @@ export function activate(context: vscode.ExtensionContext) {
122 context.subscriptions 133 context.subscriptions
123 ); 134 );
124 135
125 const expandMacroContentProvider = new ExpandMacroHoverProvider();
126
127 disposeOnDeactivation(
128 vscode.languages.registerHoverProvider(
129 'rust',
130 expandMacroContentProvider
131 )
132 );
133
134 const startServer = () => Server.start(allNotifications); 136 const startServer = () => Server.start(allNotifications);
135 const reloadCommand = () => reloadServer(startServer); 137 const reloadCommand = () => reloadServer(startServer);
136 138