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

55 lines
1.8 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) {
2019-01-12 17:49:07 -06:00
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) {
2019-01-05 05:12:39 -06:00
const toOpenUri = vscode.Uri.parse(toOpen);
const doc = await vscode.workspace.openTextDocument(toOpenUri);
2018-10-07 15:59:02 -05:00
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);
2019-01-15 10:15:51 -06:00
editor.revealRange(
new vscode.Range(position, position),
vscode.TextEditorRevealType.Default
);
2018-10-07 15:44:25 -05:00
}
}