Files
spring-tools/vscode-extensions/vscode-spring-boot/lib/copilot/guideApply.ts
Alex Boyko 19e3b38804 Slight refactorings and remove maven-model dependency (#1365) (#1366)
* trying to allow manual trigger for publishing vscode pre-releases

* remove usage of proposed chat variable resolver api for now

* remove maven-model dependency and clean up code

---------

Co-authored-by: Udayani Vaka <79973862+vudayani@users.noreply.github.com>
Co-authored-by: Martin Lippert <martin.lippert@broadcom.com>
2024-10-03 08:36:38 -04:00

45 lines
1.6 KiB
TypeScript

import { Uri, workspace, window, commands, ProgressLocation } from "vscode";
import { getTargetGuideMardown, readResponseFromFile } from "./util";
import { createConverter } from "vscode-languageclient/lib/common/protocolConverter";
import fs from "fs";
import path from "path";
const CONVERTER = createConverter(undefined, true, true);
const CANCELLED = "Cancelled";
export async function applyLspEdit(uri: Uri) {
try {
if (!uri) {
uri = await getTargetGuideMardown();
}
window.withProgress({
location: ProgressLocation.Window,
title: "Copilot agent",
cancellable: true
}, async (progress, cancellation) => {
progress.report({ message: "applying edits..." });
const fileContent = (await readResponseFromFile(uri)).toString();
const lspEdit = await commands.executeCommand("sts/copilot/agent/lspEdits", uri.toString(), fileContent);
const workspaceEdit = await CONVERTER.asWorkspaceEdit(lspEdit);
await Promise.all(workspaceEdit.entries().map(async ([uri, edits]) => {
console.log(edits);
if (fs.existsSync(uri.fsPath)) {
const doc = await workspace.openTextDocument(uri.fsPath);
await window.showTextDocument(doc);
}
}));
return await workspace.applyEdit(workspaceEdit, {
isRefactoring: true
});
});
} catch (error) {
if (error !== CANCELLED) {
window.showErrorMessage(error);
}
}
}