From 3ccd05fedc46796f793295901a8619492256468e Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 18 Nov 2019 02:47:50 +0800 Subject: Add recursive expand in vscode --- editors/code/src/commands/expand_macro.ts | 45 +++++++++++++++++++++++++++++++ editors/code/src/commands/index.ts | 2 ++ 2 files changed, 47 insertions(+) create mode 100644 editors/code/src/commands/expand_macro.ts (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts new file mode 100644 index 000000000..bf1923190 --- /dev/null +++ b/editors/code/src/commands/expand_macro.ts @@ -0,0 +1,45 @@ +import * as vscode from 'vscode'; +import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; +import { Server } from '../server'; + +type ExpandMacroResult = [string, string] + +function code_format([name, text]: [string, string]): vscode.MarkdownString { + const markdown = new vscode.MarkdownString(`#### Recursive expansion of ${name}! macro`); + markdown.appendCodeblock(text, 'rust'); + return markdown; +} + +export class ExpandMacroHoverProvider implements vscode.HoverProvider { + public provideHover( + document: vscode.TextDocument, + position: vscode.Position, + token: vscode.CancellationToken, + ): Thenable | null { + async function handle() { + const request: MacroExpandParams = { + textDocument: { uri: document.uri.toString() }, + position, + }; + const result = await Server.client.sendRequest( + 'rust-analyzer/expandMacro', + request + ); + if (result != null) { + const formated = code_format(result); + return new vscode.Hover(formated); + } + + return null; + }; + + return handle(); + } +} + + +interface MacroExpandParams { + textDocument: TextDocumentIdentifier; + position: Position; +} + 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 @@ import * as analyzerStatus from './analyzer_status'; import * as applySourceChange from './apply_source_change'; +import * as expandMacro from './expand_macro'; import * as inlayHints from './inlay_hints'; import * as joinLines from './join_lines'; import * as matchingBrace from './matching_brace'; @@ -11,6 +12,7 @@ import * as syntaxTree from './syntaxTree'; export { analyzerStatus, applySourceChange, + expandMacro, joinLines, matchingBrace, parentModule, -- cgit v1.2.3 From 8010b42b21a59e4bb1e025155b8133ae52d3cf45 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 18 Nov 2019 03:39:11 +0800 Subject: Fix npm formatting --- editors/code/src/commands/expand_macro.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts index bf1923190..d024a70e0 100644 --- a/editors/code/src/commands/expand_macro.ts +++ b/editors/code/src/commands/expand_macro.ts @@ -2,11 +2,13 @@ import * as vscode from 'vscode'; import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; import { Server } from '../server'; -type ExpandMacroResult = [string, string] +type ExpandMacroResult = [string, string]; function code_format([name, text]: [string, string]): vscode.MarkdownString { - const markdown = new vscode.MarkdownString(`#### Recursive expansion of ${name}! macro`); - markdown.appendCodeblock(text, 'rust'); + const markdown = new vscode.MarkdownString( + `#### Recursive expansion of ${name}! macro` + ); + markdown.appendCodeblock(text, 'rust'); return markdown; } @@ -14,12 +16,12 @@ export class ExpandMacroHoverProvider implements vscode.HoverProvider { public provideHover( document: vscode.TextDocument, position: vscode.Position, - token: vscode.CancellationToken, + token: vscode.CancellationToken ): Thenable | null { async function handle() { const request: MacroExpandParams = { textDocument: { uri: document.uri.toString() }, - position, + position }; const result = await Server.client.sendRequest( 'rust-analyzer/expandMacro', @@ -31,15 +33,13 @@ export class ExpandMacroHoverProvider implements vscode.HoverProvider { } return null; - }; + } return handle(); } } - interface MacroExpandParams { textDocument: TextDocumentIdentifier; position: Position; } - -- cgit v1.2.3 From 4012da07fd22223660a21c65d54d10a9a632eda0 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 19 Nov 2019 22:56:48 +0800 Subject: Change return type of expand_macro --- editors/code/src/commands/expand_macro.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts index d024a70e0..3fc3e0391 100644 --- a/editors/code/src/commands/expand_macro.ts +++ b/editors/code/src/commands/expand_macro.ts @@ -2,13 +2,16 @@ import * as vscode from 'vscode'; import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; import { Server } from '../server'; -type ExpandMacroResult = [string, string]; +interface ExpandedMacro { + name: string, + expansion: string, +} -function code_format([name, text]: [string, string]): vscode.MarkdownString { +function code_format(expanded: ExpandedMacro): vscode.MarkdownString { const markdown = new vscode.MarkdownString( - `#### Recursive expansion of ${name}! macro` + `#### Recursive expansion of ${expanded.name}! macro` ); - markdown.appendCodeblock(text, 'rust'); + markdown.appendCodeblock(expanded.expansion, 'rust'); return markdown; } @@ -23,7 +26,7 @@ export class ExpandMacroHoverProvider implements vscode.HoverProvider { textDocument: { uri: document.uri.toString() }, position }; - const result = await Server.client.sendRequest( + const result = await Server.client.sendRequest( 'rust-analyzer/expandMacro', request ); -- cgit v1.2.3 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') 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 From 1d56b80250d43a7d263d2e9583871c85081261b6 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Wed, 20 Nov 2019 01:22:28 +0800 Subject: Minor fix for outpu text formating --- editors/code/src/commands/expand_macro.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts index 1fa2cf739..34e0c8fb3 100644 --- a/editors/code/src/commands/expand_macro.ts +++ b/editors/code/src/commands/expand_macro.ts @@ -75,7 +75,7 @@ interface ExpandedMacro { function code_format(expanded: ExpandedMacro): string { let result = `// Recursive expansion of ${expanded.name}! macro\n`; - result += '='.repeat(result.length); + result += '// ' + '='.repeat(result.length - 3); result += '\n\n'; result += expanded.expansion; -- cgit v1.2.3