From d16cc223e100ae9a29384f104a378932088e0c3c Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 20 Nov 2019 01:06:10 +0800 Subject: Use DocumentProvider instead of Hover --- editors/code/src/commands/expand_macro.ts | 81 ++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 23 deletions(-) (limited to 'editors/code/src/commands/expand_macro.ts') 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'; import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; import { Server } from '../server'; -interface ExpandedMacro { - name: string, - expansion: string, -} +export const expandMacroUri = vscode.Uri.parse( + 'rust-analyzer://expandMacro/[EXPANSION].rs' +); -function code_format(expanded: ExpandedMacro): vscode.MarkdownString { - const markdown = new vscode.MarkdownString( - `#### Recursive expansion of ${expanded.name}! macro` - ); - markdown.appendCodeblock(expanded.expansion, 'rust'); - return markdown; -} +export class ExpandMacroContentProvider + implements vscode.TextDocumentContentProvider { + public eventEmitter = new vscode.EventEmitter(); -export class ExpandMacroHoverProvider implements vscode.HoverProvider { - public provideHover( - document: vscode.TextDocument, - position: vscode.Position, - token: vscode.CancellationToken - ): Thenable | null { + public provideTextDocumentContent( + uri: vscode.Uri + ): vscode.ProviderResult { async function handle() { + const editor = vscode.window.activeTextEditor; + if (editor == null) { + return ''; + } + + const position = editor.selection.active; const request: MacroExpandParams = { - textDocument: { uri: document.uri.toString() }, + textDocument: { uri: editor.document.uri.toString() }, position }; - const result = await Server.client.sendRequest( + const expanded = await Server.client.sendRequest( 'rust-analyzer/expandMacro', request ); - if (result != null) { - const formated = code_format(result); - return new vscode.Hover(formated); + + if (expanded == null) { + return 'Not available'; } - return null; + return code_format(expanded); } return handle(); } + + get onDidChange(): vscode.Event { + return this.eventEmitter.event; + } +} + +// Opens the virtual file that will show the syntax tree +// +// The contents of the file come from the `TextDocumentContentProvider` +export function createHandle(provider: ExpandMacroContentProvider) { + return async () => { + const uri = expandMacroUri; + + const document = await vscode.workspace.openTextDocument(uri); + + provider.eventEmitter.fire(uri); + + return vscode.window.showTextDocument( + document, + vscode.ViewColumn.Two, + true + ); + }; } interface MacroExpandParams { textDocument: TextDocumentIdentifier; position: Position; } + +interface ExpandedMacro { + name: string; + expansion: string; +} + +function code_format(expanded: ExpandedMacro): string { + let result = `// Recursive expansion of ${expanded.name}! macro\n`; + result += '='.repeat(result.length); + result += '\n\n'; + result += expanded.expansion; + + return result; +} -- cgit v1.2.3