From 68f47a5b10cfbb7b0168a0f24ddc7d8ced6cedda Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 19:12:49 +0100 Subject: Cleanup --- editors/code/src/commands/analyzer_status.ts | 2 +- editors/code/src/commands/syntax_tree.ts | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts index b2b624b75..e680179ae 100644 --- a/editors/code/src/commands/analyzer_status.ts +++ b/editors/code/src/commands/analyzer_status.ts @@ -1,8 +1,8 @@ import * as vscode from 'vscode'; import { Ctx, Cmd } from '../ctx'; -// Shows status of rust-analyzer (for debugging) +// Shows status of rust-analyzer (for debugging) export function analyzerStatus(ctx: Ctx): Cmd { let poller: NodeJS.Timer | null = null; const tdcp = new TextDocumentContentProvider(ctx); diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts index e61fb36df..9831c2a2e 100644 --- a/editors/code/src/commands/syntax_tree.ts +++ b/editors/code/src/commands/syntax_tree.ts @@ -7,12 +7,12 @@ import { Ctx, Cmd } from '../ctx'; // // The contents of the file come from the `TextDocumentContentProvider` export function syntaxTree(ctx: Ctx): Cmd { - const stcp = new SyntaxTreeContentProvider(ctx); + const tdcp = new TextDocumentContentProvider(ctx); ctx.pushCleanup( vscode.workspace.registerTextDocumentContentProvider( 'rust-analyzer', - stcp, + tdcp, ), ); @@ -20,7 +20,7 @@ export function syntaxTree(ctx: Ctx): Cmd { (event: vscode.TextDocumentChangeEvent) => { const doc = event.document; if (doc.languageId !== 'rust') return; - afterLs(() => stcp.eventEmitter.fire(stcp.uri)); + afterLs(() => tdcp.eventEmitter.fire(tdcp.uri)); }, ctx.subscriptions, ); @@ -28,7 +28,7 @@ export function syntaxTree(ctx: Ctx): Cmd { vscode.window.onDidChangeActiveTextEditor( (editor: vscode.TextEditor | undefined) => { if (!editor || editor.document.languageId !== 'rust') return; - stcp.eventEmitter.fire(stcp.uri); + tdcp.eventEmitter.fire(tdcp.uri); }, ctx.subscriptions, ); @@ -38,12 +38,12 @@ export function syntaxTree(ctx: Ctx): Cmd { const rangeEnabled = !!(editor && !editor.selection.isEmpty); const uri = rangeEnabled - ? vscode.Uri.parse(`${stcp.uri.toString()}?range=true`) - : stcp.uri; + ? vscode.Uri.parse(`${tdcp.uri.toString()}?range=true`) + : tdcp.uri; const document = await vscode.workspace.openTextDocument(uri); - stcp.eventEmitter.fire(uri); + tdcp.eventEmitter.fire(uri); return vscode.window.showTextDocument( document, @@ -64,7 +64,7 @@ interface SyntaxTreeParams { range?: lc.Range; } -export class SyntaxTreeContentProvider +class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { ctx: Ctx; uri = vscode.Uri.parse('rust-analyzer://syntaxtree'); @@ -86,8 +86,8 @@ export class SyntaxTreeContentProvider range = editor.selection.isEmpty ? undefined : this.ctx.client.code2ProtocolConverter.asRange( - editor.selection, - ); + editor.selection, + ); } const request: SyntaxTreeParams = { -- cgit v1.2.3 From 94be27fc44763afbf67d36010d4aa05639191e4b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 19:30:30 +0100 Subject: Move expand macro to the new context --- editors/code/src/commands/analyzer_status.ts | 2 +- editors/code/src/commands/expand_macro.ts | 102 ++++++++++++--------------- editors/code/src/commands/index.ts | 2 +- editors/code/src/commands/syntax_tree.ts | 7 +- editors/code/src/main.ts | 15 +--- 5 files changed, 53 insertions(+), 75 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts index e680179ae..2c8362286 100644 --- a/editors/code/src/commands/analyzer_status.ts +++ b/editors/code/src/commands/analyzer_status.ts @@ -37,7 +37,7 @@ export function analyzerStatus(ctx: Ctx): Cmd { class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { - ctx: Ctx; + private ctx: Ctx; uri = vscode.Uri.parse('rust-analyzer-status://status'); eventEmitter = new vscode.EventEmitter(); diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts index 17c78280a..da208257a 100644 --- a/editors/code/src/commands/expand_macro.ts +++ b/editors/code/src/commands/expand_macro.ts @@ -1,60 +1,23 @@ import * as vscode from 'vscode'; -import { Position, TextDocumentIdentifier } from 'vscode-languageclient'; -import { Server } from '../server'; +import * as lc from 'vscode-languageclient'; -export const expandMacroUri = vscode.Uri.parse( - 'rust-analyzer://expandMacro/[EXPANSION].rs', -); - -export class ExpandMacroContentProvider - implements vscode.TextDocumentContentProvider { - public eventEmitter = new vscode.EventEmitter(); - - 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: editor.document.uri.toString() }, - position, - }; - const expanded = await Server.client.sendRequest( - 'rust-analyzer/expandMacro', - request, - ); - - if (expanded == null) { - return 'Not available'; - } - - return code_format(expanded); - } - - return handle(); - } - - get onDidChange(): vscode.Event { - return this.eventEmitter.event; - } -} +import { Ctx, Cmd } from '../ctx'; // 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); +export function expandMacro(ctx: Ctx): Cmd { + const tdcp = new TextDocumentContentProvider(ctx); + ctx.pushCleanup( + vscode.workspace.registerTextDocumentContentProvider( + 'rust-analyzer', + tdcp, + ), + ); + return async () => { + const document = await vscode.workspace.openTextDocument(tdcp.uri); + tdcp.eventEmitter.fire(tdcp.uri); return vscode.window.showTextDocument( document, vscode.ViewColumn.Two, @@ -63,11 +26,6 @@ export function createHandle(provider: ExpandMacroContentProvider) { }; } -interface MacroExpandParams { - textDocument: TextDocumentIdentifier; - position: Position; -} - interface ExpandedMacro { name: string; expansion: string; @@ -81,3 +39,37 @@ function code_format(expanded: ExpandedMacro): string { return result; } + +class TextDocumentContentProvider + implements vscode.TextDocumentContentProvider { + private ctx: Ctx; + uri = vscode.Uri.parse('rust-analyzer://expandMacro/[EXPANSION].rs'); + eventEmitter = new vscode.EventEmitter(); + + constructor(ctx: Ctx) { + this.ctx = ctx; + } + + async provideTextDocumentContent(_uri: vscode.Uri): Promise { + const editor = vscode.window.activeTextEditor; + if (editor == null) return ''; + + const position = editor.selection.active; + const request: lc.TextDocumentPositionParams = { + textDocument: { uri: editor.document.uri.toString() }, + position, + }; + const expanded = await this.ctx.client.sendRequest( + 'rust-analyzer/expandMacro', + request, + ); + + if (expanded == null) return 'Not available'; + + return code_format(expanded); + } + + get onDidChange(): vscode.Event { + return this.eventEmitter.event; + } +} diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 8f91b3b7d..325ae3da8 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -6,7 +6,7 @@ import { joinLines } from './join_lines'; import { onEnter } from './on_enter'; import { parentModule } from './parent_module'; import { syntaxTree } from './syntax_tree'; -import * as expandMacro from './expand_macro'; +import { expandMacro } from './expand_macro'; import * as inlayHints from './inlay_hints'; import * as runnables from './runnables'; diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts index 9831c2a2e..20ff7e5ca 100644 --- a/editors/code/src/commands/syntax_tree.ts +++ b/editors/code/src/commands/syntax_tree.ts @@ -66,10 +66,9 @@ interface SyntaxTreeParams { class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { - ctx: Ctx; + private ctx: Ctx; uri = vscode.Uri.parse('rust-analyzer://syntaxtree'); eventEmitter = new vscode.EventEmitter(); - syntaxTree: string = 'Not available'; constructor(ctx: Ctx) { this.ctx = ctx; @@ -86,8 +85,8 @@ class TextDocumentContentProvider range = editor.selection.isEmpty ? undefined : this.ctx.client.code2ProtocolConverter.asRange( - editor.selection, - ); + editor.selection, + ); } const request: SyntaxTreeParams = { diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index d92cd164f..b8e3396a6 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -2,7 +2,6 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; import * as commands from './commands'; -import { ExpandMacroContentProvider } from './commands/expand_macro'; import { HintsUpdater } from './commands/inlay_hints'; import { StatusDisplay } from './commands/watch_status'; import * as events from './events'; @@ -20,6 +19,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('joinLines', commands.joinLines); ctx.registerCommand('parentModule', commands.parentModule); ctx.registerCommand('syntaxTree', commands.syntaxTree); + ctx.registerCommand('expandMacro', commands.expandMacro); function disposeOnDeactivation(disposable: vscode.Disposable) { context.subscriptions.push(disposable); @@ -65,25 +65,12 @@ export async function activate(context: vscode.ExtensionContext) { params => watchStatus.handleProgressNotification(params), ], ]; - const expandMacroContentProvider = new ExpandMacroContentProvider(); // The events below are plain old javascript events, triggered and handled by vscode vscode.window.onDidChangeActiveTextEditor( events.changeActiveTextEditor.makeHandler(), ); - disposeOnDeactivation( - vscode.workspace.registerTextDocumentContentProvider( - 'rust-analyzer', - expandMacroContentProvider, - ), - ); - - registerCommand( - 'rust-analyzer.expandMacro', - commands.expandMacro.createHandle(expandMacroContentProvider), - ); - const startServer = () => Server.start(allNotifications); const reloadCommand = () => reloadServer(startServer); -- cgit v1.2.3