rust/editors/code/src/commands/apply_source_change.ts

59 lines
2.0 KiB
TypeScript
Raw Normal View History

2018-10-07 15:44:25 -05:00
import * as vscode from 'vscode';
2018-10-07 15:59:02 -05:00
import * as lc from 'vscode-languageclient';
2018-10-07 15:44:25 -05:00
import { Server } from '../server';
interface FileSystemEdit {
type: string;
uri?: string;
src?: string;
dst?: string;
}
export interface SourceChange {
2018-10-07 15:59:02 -05:00
label: string;
sourceFileEdits: lc.TextDocumentEdit[];
fileSystemEdits: FileSystemEdit[];
cursorPosition?: lc.TextDocumentPositionParams;
2018-10-07 15:44:25 -05:00
}
export async function handle(change: SourceChange) {
2018-10-07 15:59:02 -05:00
const wsEdit = new vscode.WorkspaceEdit();
for (const sourceEdit of change.sourceFileEdits) {
const uri = Server.client.protocol2CodeConverter.asUri(sourceEdit.textDocument.uri);
const edits = Server.client.protocol2CodeConverter.asTextEdits(sourceEdit.edits);
wsEdit.set(uri, edits);
2018-10-07 15:44:25 -05:00
}
let created;
let moved;
2018-10-07 15:59:02 -05:00
for (const fsEdit of change.fileSystemEdits) {
2018-10-08 13:18:55 -05:00
switch (fsEdit.type) {
case 'createFile':
const uri = vscode.Uri.parse(fsEdit.uri!);
wsEdit.createFile(uri);
created = uri;
break;
case 'moveFile':
const src = vscode.Uri.parse(fsEdit.src!);
const dst = vscode.Uri.parse(fsEdit.dst!);
wsEdit.renameFile(src, dst);
moved = dst;
break;
2018-10-07 15:44:25 -05:00
}
}
2018-10-07 15:59:02 -05:00
const toOpen = created || moved;
const toReveal = change.cursorPosition;
await vscode.workspace.applyEdit(wsEdit);
2018-10-07 15:44:25 -05:00
if (toOpen) {
2018-10-07 15:59:02 -05:00
const doc = await vscode.workspace.openTextDocument(toOpen);
await vscode.window.showTextDocument(doc);
2018-10-07 15:44:25 -05:00
} else if (toReveal) {
2018-10-07 15:59:02 -05:00
const uri = Server.client.protocol2CodeConverter.asUri(toReveal.textDocument.uri);
const position = Server.client.protocol2CodeConverter.asPosition(toReveal.position);
const editor = vscode.window.activeTextEditor;
2018-10-08 13:18:55 -05:00
if (!editor || editor.document.uri.toString() !== uri.toString()) { return; }
2018-10-07 15:59:02 -05:00
if (!editor.selection.isEmpty) { return; }
editor!.selection = new vscode.Selection(position, position);
2018-10-07 15:44:25 -05:00
}
}