rust/editors/code/src/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';
import * as ra from './rust-analyzer-api';
2018-10-07 15:44:25 -05:00
2019-12-30 09:43:34 -06:00
import { Ctx } from './ctx';
2018-10-07 15:44:25 -05:00
export async function applySourceChange(ctx: Ctx, change: ra.SourceChange) {
2019-12-31 11:14:00 -06:00
const client = ctx.client;
2019-12-31 11:55:34 -06:00
if (!client) return;
2019-12-31 11:14:00 -06:00
const wsEdit = client.protocol2CodeConverter.asWorkspaceEdit(
2019-12-09 12:57:55 -06:00
change.workspaceEdit,
2019-01-12 17:49:07 -06:00
);
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) {
2019-12-31 11:14:00 -06:00
const uri = client.protocol2CodeConverter.asUri(
2019-12-09 12:57:55 -06:00
toReveal.textDocument.uri,
2018-10-08 16:38:33 -05:00
);
2019-12-31 11:14:00 -06:00
const position = client.protocol2CodeConverter.asPosition(
2019-12-09 12:57:55 -06:00
toReveal.position,
2018-10-08 16:38:33 -05:00
);
2018-10-07 15:59:02 -05:00
const editor = vscode.window.activeTextEditor;
2020-04-20 11:02:36 -05:00
if (!editor || !editor.selection.isEmpty) {
2018-10-08 16:38:33 -05:00
return;
}
2020-04-20 11:02:36 -05:00
if (editor.document.uri !== uri) {
const doc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(doc);
2018-10-08 16:38:33 -05:00
}
editor.selection = new vscode.Selection(position, position);
2019-01-15 10:15:51 -06:00
editor.revealRange(
new vscode.Range(position, position),
2019-12-09 12:57:55 -06:00
vscode.TextEditorRevealType.Default,
2019-01-15 10:15:51 -06:00
);
2018-10-07 15:44:25 -05:00
}
}