From 8abf5363433e977c5393bb569e2a5d559cb0a602 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 29 Aug 2018 18:03:14 +0300 Subject: Grand refactoring --- code/src/extension.ts | 91 +++++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 40 deletions(-) (limited to 'code/src') diff --git a/code/src/extension.ts b/code/src/extension.ts index 3b24b73b6..f2589ef2f 100644 --- a/code/src/extension.ts +++ b/code/src/extension.ts @@ -60,11 +60,8 @@ export function activate(context: vscode.ExtensionContext) { textDocument: { uri: editor.document.uri.toString() }, range: client.code2ProtocolConverter.asRange(editor.selection), } - let response = await client.sendRequest("m/joinLines", request) - let edits = client.protocol2CodeConverter.asTextEdits(response) - let wsEdit = new vscode.WorkspaceEdit() - wsEdit.set(editor.document.uri, edits) - return vscode.workspace.applyEdit(wsEdit) + let change = await client.sendRequest("m/joinLines", request) + await applySourceChange(change) }) registerCommand('libsyntax-rust.parentModule', async () => { let editor = vscode.window.activeTextEditor @@ -113,28 +110,7 @@ export function activate(context: vscode.ExtensionContext) { return await vscode.tasks.executeTask(task) } }) - registerCommand('libsyntax-rust.fsEdit', async (ops: FsOp[]) => { - let edit = new vscode.WorkspaceEdit() - let created; - let moved; - for (let op of ops) { - if (op.type == "createFile") { - let uri = vscode.Uri.parse(op.uri!) - edit.createFile(uri) - created = uri - } else if (op.type == "moveFile") { - let src = vscode.Uri.parse(op.src!) - let dst = vscode.Uri.parse(op.dst!) - edit.renameFile(src, dst) - moved = dst - } else { - console.error(`unknown op: ${JSON.stringify(op)}`) - } - } - await vscode.workspace.applyEdit(edit) - let doc = await vscode.workspace.openTextDocument((created || moved)!) - await vscode.window.showTextDocument(doc) - }) + registerCommand('libsyntax-rust.applySourceChange', applySourceChange) dispose(vscode.workspace.registerTextDocumentContentProvider( 'libsyntax-rust', @@ -207,18 +183,6 @@ function startServer() { ) } ) - client.onRequest( - new lc.RequestType("m/moveCursor"), - (params: lc.Position, token: lc.CancellationToken) => { - let editor = vscode.window.activeTextEditor; - if (!editor) return - if (!editor.selection.isEmpty) return - let position = client.protocol2CodeConverter.asPosition(params) - afterLs(() => { - editor!.selection = new vscode.Selection(position, position) - }) - } - ) }) client.start(); } @@ -383,9 +347,56 @@ function createTask(spec: Runnable): vscode.Task { return t; } -interface FsOp { +interface FileSystemEdit { type: string; uri?: string; src?: string; dst?: string; } + +interface SourceChange { + label: string, + sourceFileEdits: lc.TextDocumentEdit[], + fileSystemEdits: FileSystemEdit[], + cursorPosition?: lc.TextDocumentPositionParams, +} + +async function applySourceChange(change: SourceChange) { + console.log(`applySOurceChange ${JSON.stringify(change)}`) + let wsEdit = new vscode.WorkspaceEdit() + for (let sourceEdit of change.sourceFileEdits) { + let uri = client.protocol2CodeConverter.asUri(sourceEdit.textDocument.uri) + let edits = client.protocol2CodeConverter.asTextEdits(sourceEdit.edits) + wsEdit.set(uri, edits) + } + let created; + let moved; + for (let fsEdit of change.fileSystemEdits) { + if (fsEdit.type == "createFile") { + let uri = vscode.Uri.parse(fsEdit.uri!) + wsEdit.createFile(uri) + created = uri + } else if (fsEdit.type == "moveFile") { + let src = vscode.Uri.parse(fsEdit.src!) + let dst = vscode.Uri.parse(fsEdit.dst!) + wsEdit.renameFile(src, dst) + moved = dst + } else { + console.error(`unknown op: ${JSON.stringify(fsEdit)}`) + } + } + let toOpen = created || moved + let toReveal = change.cursorPosition + await vscode.workspace.applyEdit(wsEdit) + if (toOpen) { + let doc = await vscode.workspace.openTextDocument(toOpen) + await vscode.window.showTextDocument(doc) + } else if (toReveal) { + let uri = client.protocol2CodeConverter.asUri(toReveal.textDocument.uri) + let position = client.protocol2CodeConverter.asPosition(toReveal.position) + let editor = vscode.window.activeTextEditor; + if (!editor || editor.document.uri != uri) return + if (!editor.selection.isEmpty) return + editor!.selection = new vscode.Selection(position, position) + } +} -- cgit v1.2.3