2018-10-07 22:44:25 +02:00
|
|
|
import * as vscode from 'vscode';
|
2018-10-07 22:59:02 +02:00
|
|
|
import * as lc from 'vscode-languageclient';
|
2020-02-25 00:49:19 +02:00
|
|
|
import * as ra from './rust-analyzer-api';
|
2018-10-07 22:44:25 +02:00
|
|
|
|
2019-12-30 16:43:34 +01:00
|
|
|
import { Ctx } from './ctx';
|
2018-10-07 22:44:25 +02:00
|
|
|
|
2020-02-25 00:49:19 +02:00
|
|
|
export async function applySourceChange(ctx: Ctx, change: ra.SourceChange) {
|
2019-12-31 18:14:00 +01:00
|
|
|
const client = ctx.client;
|
2019-12-31 18:55:34 +01:00
|
|
|
if (!client) return;
|
2019-12-31 18:14:00 +01:00
|
|
|
|
|
|
|
const wsEdit = client.protocol2CodeConverter.asWorkspaceEdit(
|
2019-12-09 20:57:55 +02:00
|
|
|
change.workspaceEdit,
|
2019-01-12 18:49:07 -05:00
|
|
|
);
|
2018-10-07 22:44:25 +02:00
|
|
|
let created;
|
|
|
|
let moved;
|
2019-01-03 14:14:36 +01:00
|
|
|
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 22:44:25 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-07 22:59:02 +02:00
|
|
|
const toOpen = created || moved;
|
|
|
|
const toReveal = change.cursorPosition;
|
|
|
|
await vscode.workspace.applyEdit(wsEdit);
|
2018-10-07 22:44:25 +02:00
|
|
|
if (toOpen) {
|
2019-01-05 12:12:39 +01:00
|
|
|
const toOpenUri = vscode.Uri.parse(toOpen);
|
|
|
|
const doc = await vscode.workspace.openTextDocument(toOpenUri);
|
2018-10-07 22:59:02 +02:00
|
|
|
await vscode.window.showTextDocument(doc);
|
2018-10-07 22:44:25 +02:00
|
|
|
} else if (toReveal) {
|
2019-12-31 18:14:00 +01:00
|
|
|
const uri = client.protocol2CodeConverter.asUri(
|
2019-12-09 20:57:55 +02:00
|
|
|
toReveal.textDocument.uri,
|
2018-10-08 22:38:33 +01:00
|
|
|
);
|
2019-12-31 18:14:00 +01:00
|
|
|
const position = client.protocol2CodeConverter.asPosition(
|
2019-12-09 20:57:55 +02:00
|
|
|
toReveal.position,
|
2018-10-08 22:38:33 +01:00
|
|
|
);
|
2018-10-07 22:59:02 +02:00
|
|
|
const editor = vscode.window.activeTextEditor;
|
2018-10-08 22:38:33 +01:00
|
|
|
if (!editor || editor.document.uri.toString() !== uri.toString()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!editor.selection.isEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
2019-01-03 14:14:36 +01:00
|
|
|
editor.selection = new vscode.Selection(position, position);
|
2019-01-15 11:15:51 -05:00
|
|
|
editor.revealRange(
|
|
|
|
new vscode.Range(position, position),
|
2019-12-09 20:57:55 +02:00
|
|
|
vscode.TextEditorRevealType.Default,
|
2019-01-15 11:15:51 -05:00
|
|
|
);
|
2018-10-07 22:44:25 +02:00
|
|
|
}
|
|
|
|
}
|