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

48 lines
1.6 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';
export interface SourceChange {
2018-10-07 15:59:02 -05:00
label: string;
workspaceEdit: lc.WorkspaceEdit;
2018-10-07 15:59:02 -05:00
cursorPosition?: lc.TextDocumentPositionParams;
2018-10-07 15:44:25 -05:00
}
export async function handle(change: SourceChange) {
const wsEdit = Server.client.protocol2CodeConverter.asWorkspaceEdit(change.workspaceEdit);
2018-10-07 15:44:25 -05:00
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;
}
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-08 16:38:33 -05:00
const uri = Server.client.protocol2CodeConverter.asUri(
toReveal.textDocument.uri
);
const position = Server.client.protocol2CodeConverter.asPosition(
toReveal.position
);
2018-10-07 15:59:02 -05:00
const editor = vscode.window.activeTextEditor;
2018-10-08 16:38:33 -05:00
if (!editor || editor.document.uri.toString() !== uri.toString()) {
return;
}
if (!editor.selection.isEmpty) {
return;
}
editor.selection = new vscode.Selection(position, position);
2018-10-07 15:44:25 -05:00
}
}