From 5aebf1081dced95a71c674aba65fb5b3e40e6ff1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 16:43:34 +0100 Subject: Refactor applySourceChange --- editors/code/src/commands/analyzer_status.ts | 8 ++-- editors/code/src/commands/apply_source_change.ts | 54 ------------------------ editors/code/src/commands/index.ts | 4 +- editors/code/src/commands/join_lines.ts | 12 +++--- editors/code/src/commands/on_enter.ts | 49 ++++++++++----------- 5 files changed, 31 insertions(+), 96 deletions(-) delete mode 100644 editors/code/src/commands/apply_source_change.ts (limited to 'editors/code/src/commands') diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts index c9d32fe07..849c2ec6c 100644 --- a/editors/code/src/commands/analyzer_status.ts +++ b/editors/code/src/commands/analyzer_status.ts @@ -40,11 +40,10 @@ export function analyzerStatus(ctx: Ctx): Cmd { class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { + ctx: Ctx uri = vscode.Uri.parse('rust-analyzer-status://status'); eventEmitter = new vscode.EventEmitter(); - ctx: Ctx - constructor(ctx: Ctx) { this.ctx = ctx } @@ -53,9 +52,8 @@ class TextDocumentContentProvider _uri: vscode.Uri, ): vscode.ProviderResult { const editor = vscode.window.activeTextEditor; - if (editor == null) { - return ''; - } + if (editor == null) return ''; + return this.ctx.client.sendRequest( 'rust-analyzer/analyzerStatus', null, diff --git a/editors/code/src/commands/apply_source_change.ts b/editors/code/src/commands/apply_source_change.ts deleted file mode 100644 index 8167398b1..000000000 --- a/editors/code/src/commands/apply_source_change.ts +++ /dev/null @@ -1,54 +0,0 @@ -import * as vscode from 'vscode'; -import * as lc from 'vscode-languageclient'; - -import { Server } from '../server'; - -export interface SourceChange { - label: string; - workspaceEdit: lc.WorkspaceEdit; - cursorPosition?: lc.TextDocumentPositionParams; -} - -export async function handle(change: SourceChange) { - const wsEdit = Server.client.protocol2CodeConverter.asWorkspaceEdit( - change.workspaceEdit, - ); - let created; - let moved; - if (change.workspaceEdit.documentChanges) { - for (const docChange of change.workspaceEdit.documentChanges) { - if (lc.CreateFile.is(docChange)) { - created = docChange.uri; - } else if (lc.RenameFile.is(docChange)) { - moved = docChange.newUri; - } - } - } - const toOpen = created || moved; - const toReveal = change.cursorPosition; - await vscode.workspace.applyEdit(wsEdit); - if (toOpen) { - const toOpenUri = vscode.Uri.parse(toOpen); - const doc = await vscode.workspace.openTextDocument(toOpenUri); - await vscode.window.showTextDocument(doc); - } else if (toReveal) { - const uri = Server.client.protocol2CodeConverter.asUri( - toReveal.textDocument.uri, - ); - const position = Server.client.protocol2CodeConverter.asPosition( - toReveal.position, - ); - const editor = vscode.window.activeTextEditor; - if (!editor || editor.document.uri.toString() !== uri.toString()) { - return; - } - if (!editor.selection.isEmpty) { - return; - } - editor.selection = new vscode.Selection(position, position); - editor.revealRange( - new vscode.Range(position, position), - vscode.TextEditorRevealType.Default, - ); - } -} diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 8090c7e5b..0a0a36e23 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -3,10 +3,9 @@ import { Ctx, Cmd } from '../ctx' import { analyzerStatus } from './analyzer_status'; import { matchingBrace } from './matching_brace'; import { joinLines } from './join_lines'; -import * as applySourceChange from './apply_source_change'; +import { onEnter } from './on_enter'; import * as expandMacro from './expand_macro'; import * as inlayHints from './inlay_hints'; -import * as onEnter from './on_enter'; import * as parentModule from './parent_module'; import * as runnables from './runnables'; import * as syntaxTree from './syntaxTree'; @@ -17,7 +16,6 @@ function collectGarbage(ctx: Ctx): Cmd { export { analyzerStatus, - applySourceChange, expandMacro, joinLines, matchingBrace, diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts index 7952fb0c0..1a4b8a2d8 100644 --- a/editors/code/src/commands/join_lines.ts +++ b/editors/code/src/commands/join_lines.ts @@ -1,16 +1,14 @@ import { Range, TextDocumentIdentifier } from 'vscode-languageclient'; import { Ctx, Cmd } from '../ctx'; import { - handle as applySourceChange, - SourceChange, -} from './apply_source_change'; + applySourceChange, SourceChange +} from '../source_change'; export function joinLines(ctx: Ctx): Cmd { return async () => { const editor = ctx.activeRustEditor; - if (!editor) { - return; - } + if (!editor) return; + const request: JoinLinesParams = { range: ctx.client.code2ProtocolConverter.asRange(editor.selection), textDocument: { uri: editor.document.uri.toString() }, @@ -19,7 +17,7 @@ export function joinLines(ctx: Ctx): Cmd { 'rust-analyzer/joinLines', request, ); - await applySourceChange(change); + await applySourceChange(ctx, change); } } diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index 772c64b3c..4503e13f0 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts @@ -1,33 +1,28 @@ -import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; -import { Server } from '../server'; import { - handle as applySourceChange, + applySourceChange, SourceChange, -} from './apply_source_change'; +} from '../source_change'; +import { Cmd, Ctx } from '../ctx'; -export async function handle(event: { text: string }): Promise { - const editor = vscode.window.activeTextEditor; - if ( - editor == null || - editor.document.languageId !== 'rust' || - event.text !== '\n' - ) { - return false; - } - const request: lc.TextDocumentPositionParams = { - textDocument: { uri: editor.document.uri.toString() }, - position: Server.client.code2ProtocolConverter.asPosition( - editor.selection.active, - ), - }; - const change = await Server.client.sendRequest( - 'rust-analyzer/onEnter', - request, - ); - if (!change) { - return false; +export function onEnter(ctx: Ctx): Cmd { + return async (event: { text: string }) => { + const editor = ctx.activeRustEditor; + if (!editor || event.text !== '\n') return false; + + const request: lc.TextDocumentPositionParams = { + textDocument: { uri: editor.document.uri.toString() }, + position: ctx.client.code2ProtocolConverter.asPosition( + editor.selection.active, + ), + }; + const change = await ctx.client.sendRequest( + 'rust-analyzer/onEnter', + request, + ); + if (!change) return false; + + await applySourceChange(ctx, change); + return true; } - await applySourceChange(change); - return true; } -- cgit v1.2.3