diff options
Diffstat (limited to 'editors')
-rw-r--r-- | editors/code/package.json | 5 | ||||
-rw-r--r-- | editors/code/src/commands/expand_macro.ts | 83 | ||||
-rw-r--r-- | editors/code/src/commands/index.ts | 2 | ||||
-rw-r--r-- | editors/code/src/extension.ts | 12 |
4 files changed, 102 insertions, 0 deletions
diff --git a/editors/code/package.json b/editors/code/package.json index fbf675d46..e21dfa174 100644 --- a/editors/code/package.json +++ b/editors/code/package.json | |||
@@ -92,6 +92,11 @@ | |||
92 | "category": "Rust Analyzer" | 92 | "category": "Rust Analyzer" |
93 | }, | 93 | }, |
94 | { | 94 | { |
95 | "command": "rust-analyzer.expandMacro", | ||
96 | "title": "Expand macro recursively", | ||
97 | "category": "Rust Analyzer" | ||
98 | }, | ||
99 | { | ||
95 | "command": "rust-analyzer.matchingBrace", | 100 | "command": "rust-analyzer.matchingBrace", |
96 | "title": "Find matching brace", | 101 | "title": "Find matching brace", |
97 | "category": "Rust Analyzer" | 102 | "category": "Rust Analyzer" |
diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts new file mode 100644 index 000000000..34e0c8fb3 --- /dev/null +++ b/editors/code/src/commands/expand_macro.ts | |||
@@ -0,0 +1,83 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; | ||
3 | import { Server } from '../server'; | ||
4 | |||
5 | export const expandMacroUri = vscode.Uri.parse( | ||
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 | |||
47 | // Opens the virtual file that will show the syntax tree | ||
48 | // | ||
49 | // The contents of the file come from the `TextDocumentContentProvider` | ||
50 | export 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 | }; | ||
64 | } | ||
65 | |||
66 | interface MacroExpandParams { | ||
67 | textDocument: TextDocumentIdentifier; | ||
68 | position: Position; | ||
69 | } | ||
70 | |||
71 | interface ExpandedMacro { | ||
72 | name: string; | ||
73 | expansion: string; | ||
74 | } | ||
75 | |||
76 | function code_format(expanded: ExpandedMacro): string { | ||
77 | let result = `// Recursive expansion of ${expanded.name}! macro\n`; | ||
78 | result += '// ' + '='.repeat(result.length - 3); | ||
79 | result += '\n\n'; | ||
80 | result += expanded.expansion; | ||
81 | |||
82 | return result; | ||
83 | } | ||
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index c194bd2ea..2ade6d331 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts | |||
@@ -1,5 +1,6 @@ | |||
1 | import * as analyzerStatus from './analyzer_status'; | 1 | import * as analyzerStatus from './analyzer_status'; |
2 | import * as applySourceChange from './apply_source_change'; | 2 | import * as applySourceChange from './apply_source_change'; |
3 | import * as expandMacro from './expand_macro'; | ||
3 | import * as inlayHints from './inlay_hints'; | 4 | import * as inlayHints from './inlay_hints'; |
4 | import * as joinLines from './join_lines'; | 5 | import * as joinLines from './join_lines'; |
5 | import * as matchingBrace from './matching_brace'; | 6 | import * as matchingBrace from './matching_brace'; |
@@ -11,6 +12,7 @@ import * as syntaxTree from './syntaxTree'; | |||
11 | export { | 12 | export { |
12 | analyzerStatus, | 13 | analyzerStatus, |
13 | applySourceChange, | 14 | applySourceChange, |
15 | expandMacro, | ||
14 | joinLines, | 16 | joinLines, |
15 | matchingBrace, | 17 | matchingBrace, |
16 | parentModule, | 18 | parentModule, |
diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts index c06928d12..683497dfd 100644 --- a/editors/code/src/extension.ts +++ b/editors/code/src/extension.ts | |||
@@ -3,6 +3,7 @@ import * as lc from 'vscode-languageclient'; | |||
3 | 3 | ||
4 | import * as commands from './commands'; | 4 | import * as commands from './commands'; |
5 | import { CargoWatchProvider } from './commands/cargo_watch'; | 5 | import { CargoWatchProvider } from './commands/cargo_watch'; |
6 | import { ExpandMacroContentProvider } from './commands/expand_macro'; | ||
6 | import { HintsUpdater } from './commands/inlay_hints'; | 7 | import { HintsUpdater } from './commands/inlay_hints'; |
7 | import { | 8 | import { |
8 | interactivelyStartCargoWatch, | 9 | interactivelyStartCargoWatch, |
@@ -97,6 +98,7 @@ export function activate(context: vscode.ExtensionContext) { | |||
97 | ] | 98 | ] |
98 | ]; | 99 | ]; |
99 | const syntaxTreeContentProvider = new SyntaxTreeContentProvider(); | 100 | const syntaxTreeContentProvider = new SyntaxTreeContentProvider(); |
101 | const expandMacroContentProvider = new ExpandMacroContentProvider(); | ||
100 | 102 | ||
101 | // 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 |
102 | vscode.window.onDidChangeActiveTextEditor( | 104 | vscode.window.onDidChangeActiveTextEditor( |
@@ -109,11 +111,21 @@ export function activate(context: vscode.ExtensionContext) { | |||
109 | syntaxTreeContentProvider | 111 | syntaxTreeContentProvider |
110 | ) | 112 | ) |
111 | ); | 113 | ); |
114 | disposeOnDeactivation( | ||
115 | vscode.workspace.registerTextDocumentContentProvider( | ||
116 | 'rust-analyzer', | ||
117 | expandMacroContentProvider | ||
118 | ) | ||
119 | ); | ||
112 | 120 | ||
113 | registerCommand( | 121 | registerCommand( |
114 | 'rust-analyzer.syntaxTree', | 122 | 'rust-analyzer.syntaxTree', |
115 | commands.syntaxTree.createHandle(syntaxTreeContentProvider) | 123 | commands.syntaxTree.createHandle(syntaxTreeContentProvider) |
116 | ); | 124 | ); |
125 | registerCommand( | ||
126 | 'rust-analyzer.expandMacro', | ||
127 | commands.expandMacro.createHandle(expandMacroContentProvider) | ||
128 | ); | ||
117 | 129 | ||
118 | vscode.workspace.onDidChangeTextDocument( | 130 | vscode.workspace.onDidChangeTextDocument( |
119 | events.changeTextDocument.createHandler(syntaxTreeContentProvider), | 131 | events.changeTextDocument.createHandler(syntaxTreeContentProvider), |