diff options
author | Edwin Cheng <[email protected]> | 2019-11-19 17:06:10 +0000 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2019-11-19 17:06:10 +0000 |
commit | d16cc223e100ae9a29384f104a378932088e0c3c (patch) | |
tree | fda3e9fc76db85ae902a11a200a762030863a74f /editors/code/src | |
parent | 80fe467ce864de077d2f5a4e29e1a4d5fe535dc3 (diff) |
Use DocumentProvider instead of Hover
Diffstat (limited to 'editors/code/src')
-rw-r--r-- | editors/code/src/commands/expand_macro.ts | 81 | ||||
-rw-r--r-- | editors/code/src/extension.ts | 22 |
2 files changed, 70 insertions, 33 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'; | |||
2 | import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; | 2 | import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; |
3 | import { Server } from '../server'; | 3 | import { Server } from '../server'; |
4 | 4 | ||
5 | interface ExpandedMacro { | 5 | export const expandMacroUri = vscode.Uri.parse( |
6 | name: string, | 6 | 'rust-analyzer://expandMacro/[EXPANSION].rs' |
7 | expansion: string, | 7 | ); |
8 | } | ||
9 | 8 | ||
10 | function code_format(expanded: ExpandedMacro): vscode.MarkdownString { | 9 | export 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 | ||
18 | export 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` | ||
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 | }; | ||
43 | } | 64 | } |
44 | 65 | ||
45 | interface MacroExpandParams { | 66 | interface MacroExpandParams { |
46 | textDocument: TextDocumentIdentifier; | 67 | textDocument: TextDocumentIdentifier; |
47 | position: Position; | 68 | position: Position; |
48 | } | 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); | ||
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 | ||
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 { ExpandMacroHoverProvider } from './commands/expand_macro'; | 6 | import { ExpandMacroContentProvider } from './commands/expand_macro'; |
7 | import { HintsUpdater } from './commands/inlay_hints'; | 7 | import { HintsUpdater } from './commands/inlay_hints'; |
8 | import { | 8 | import { |
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 | ||