refactor writeMavenDependencies implementation

This commit is contained in:
vudayani
2024-09-28 22:05:15 +05:30
committed by Martin Lippert
parent f9949d6480
commit 25f8eef426
28 changed files with 218 additions and 1120 deletions

View File

@@ -1,4 +1,4 @@
import { Uri, workspace, window, commands } from "vscode";
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";
@@ -14,21 +14,29 @@ export async function applyLspEdit(uri: Uri) {
uri = await getTargetGuideMardown();
}
const fileContent = (await readResponseFromFile(uri)).toString();
const lspEdit = await commands.executeCommand("sts/copilot/agent/lspEdits", uri.toString(), path.dirname(uri.fsPath), fileContent);
const workspaceEdit = await CONVERTER.asWorkspaceEdit(lspEdit);
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(), path.dirname(uri.fsPath), 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);
}
}));
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
});
return await workspace.applyEdit(workspaceEdit, {
isRefactoring: true
});
});
} catch (error) {
if (error !== CANCELLED) {
window.showErrorMessage(error);

View File

@@ -86,7 +86,7 @@ export default class SpringBootChatAgent {
stream.markdown(chatResponse);
stream.button({
command: 'vscode-spring-boot.agent.apply',
title: l10n.t('Preview Changes')
title: l10n.t('Apply Changes')
});
return { metadata: { command: '' } };
}

View File

@@ -1,121 +0,0 @@
// import { ProgressLocation, Uri, window, workspace } from "vscode";
// import cp from "child_process";
// import * as vscode from 'vscode';
// import { homedir } from "os";
// import { getWorkspaceRoot, getWorkspaceRootPath } from "./util";
// import path from "path";
// import { WorkspaceEdit } from "vscode-languageclient";
// export const CANCELLED = "Cancelled";
// export class SpringCli {
// get executable(): string {
// return workspace.getConfiguration("spring-cli").get("executable") || "spring";
// }
// guideLspEdit(uri: Uri, cwd?: string): Promise<WorkspaceEdit> {
// const args = [
// "guide",
// "apply",
// "--lsp-edit",
// "--file",
// uri.fsPath
// ];
// return this.fetchJson("Applying guide", uri.fsPath, args, cwd || path.dirname(uri.fsPath), true);
// }
// enhanceResponse(uri: Uri, cwd: string): Thenable<string> {
// const args = [
// "ai",
// "enhance-response",
// "--file",
// uri.fsPath
// ];
// return this.exec("Spring cli ai", "Enhance response", args, cwd);
// }
// private async executeCommand(args: string[], cwd?: string): Promise<string> {
// const processOpts = { cwd: cwd || (await getWorkspaceRoot())?.fsPath || homedir() };
// const process = this.executable.endsWith(".jar") ? await cp.exec(`java -jar ${this.executable} ${args.join(" ")}`, processOpts) : await cp.exec(`${this.executable} ${args.join(" ")}`, processOpts);
// const dataChunks: string[] = [];
// process.stdout.on("data", s => dataChunks.push(s));
// return new Promise<string>((resolve, reject) => {
// process.on("exit", (code) => {
// if (code) {
// reject(`Failed to execute command: ${dataChunks.join()}`);
// } else {
// resolve(dataChunks.join());
// }
// });
// });
// }
// private async exec<T>(title: string, message: string, args: string[], cwd?: string): Promise<T> {
// return vscode.window.withProgress({
// location: vscode.ProgressLocation.Window,
// cancellable: true,
// title,
// }, async (progress, cancellation) => {
// if (message) {
// progress.report({ message });
// }
// return new Promise<T>(async (resolve, reject) => {
// if (cancellation.isCancellationRequested) {
// reject("Cancelled");
// }
// try {
// const output: string = await this.executeCommand(args, cwd);
// resolve(output as T);
// } catch (error) {
// console.error(`Error: ${error}`);
// reject(error);
// }
// });
// });
// }
// private async fetchJson<T>(title: string, message: string, args: string[], cwd?: string, omitJsonParam?: boolean): Promise<T> {
// return window.withProgress({
// location: ProgressLocation.Window,
// cancellable: true,
// title
// }, (progress, cancellation) => {
// if (message) {
// progress.report({ message });
// }
// return new Promise<T>(async (resolve, reject) => {
// if (cancellation.isCancellationRequested) {
// reject(CANCELLED);
// }
// const processOpts = { cwd: cwd || getWorkspaceRootPath()?.fsPath || homedir() };
// const process = this.executable.endsWith(".jar") ? await cp.exec(`java -jar ${this.executable} ${args.join(" ")}`, processOpts) : await cp.exec(`${this.executable} ${args.join(" ")} ${omitJsonParam ? "" : "--json"}`, processOpts);
// cancellation.onCancellationRequested(() => process.kill());
// const dataChunks: string[] = [];
// process.stdout.on("data", s => dataChunks.push(s));
// process.on("exit", (code) => {
// if (code) {
// if (cancellation.isCancellationRequested) {
// reject(CANCELLED);
// } else {
// reject(`Failed to fetch data: ${dataChunks.join()}`);
// }
// } else {
// try {
// resolve(JSON.parse(dataChunks.join()) as T);
// } catch (error) {
// reject(error);
// }
// }
// });
// });
// });
// }
// }