[vscode-spring-cli] Experimental guide apply --lsp-edit support

This commit is contained in:
aboyko
2024-02-02 20:42:27 -05:00
parent 2b443c7106
commit 9445e28e83
5 changed files with 188 additions and 17 deletions

View File

@@ -6,6 +6,7 @@ import { getWorkspaceRoot } from "./utils";
import path from "path";
import fs from "fs";
import yaml from "js-yaml";
import { WorkspaceEdit } from "vscode-languageclient";
export const SPRING_CLI_TASK_TYPE = 'spring-cli';
@@ -16,15 +17,15 @@ export class Cli {
}
projectList() : Promise<Project[]> {
return this.fetchJson("Fetching projects...", undefined, ["project", "list"]);
return this.fetchJson("Fetching projects...", undefined, ["project", "list", "--json"]);
}
projectCatalogList(): Promise<ProjectCatalog[]> {
return this.fetchJson("Fetching Catalogs...", undefined, ["project-catalog", "list"]);
return this.fetchJson("Fetching Catalogs...", undefined, ["project-catalog", "list", "--json"]);
}
projectCatalogListAvailable(): Promise<ProjectCatalog[]> {
return this.fetchJson("Fetching Available Catalogs...", undefined, ["project-catalog", "list-available"]);
return this.fetchJson("Fetching Available Catalogs...", undefined, ["project-catalog", "list-available", "--json"]);
}
guideApply(uri: Uri, cwd?: string) {
@@ -37,6 +38,17 @@ export class Cli {
return this.exec("Applying guide", uri.fsPath, args, cwd || path.dirname(uri.fsPath));
}
guideLspEdit(uri: Uri, cwd?: string): Promise<WorkspaceEdit> {
const args = [
"guide",
"apply",
"--lsp-edit",
"--file",
uri.fsPath
];
return this.fetchJson("Running guide", uri.fsPath, args, cwd || path.dirname(uri.fsPath));
}
aiAdd(question: string, cwd: string): Thenable<Uri> {
const args = [
@@ -314,7 +326,7 @@ export class Cli {
reject("Cancelled");
}
const processOpts = { cwd: cwd || getWorkspaceRoot()?.fsPath || homedir() };
const process = this.executable.endsWith(".jar") ? await cp.exec(`java -jar ${this.executable} ${args.join(" ")} --json`, processOpts) : await cp.exec(`${this.executable} ${args.join(" ")} --json`, processOpts);
const process = this.executable.endsWith(".jar") ? await cp.exec(`java -jar ${this.executable} ${args.join(" ")}`, processOpts) : await cp.exec(`${this.executable} ${args.join(" ")} --json`, processOpts);
cancellation.onCancellationRequested(() => process.kill());
const dataChunks: string[] = [];
process.stdout.on("data", s => dataChunks.push(s));

View File

@@ -6,7 +6,7 @@ import { handleProjectAdd, handleProjectRemove } from "./project";
import { handleCommandAdd, handleCommandExecute, handleCommandNew, handleCommandRemove } from "./command";
import { ExtensionContext, commands, tasks } from "vscode";
import { handleAiAdd } from "./ai";
import { handleGuideApply } from "./guide";
import { handleGuideApply, handleGuideRun } from "./guide";
export const CLI = new Cli();
@@ -30,6 +30,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
commands.registerCommand('vscode-spring-cli.ai.add', handleAiAdd),
commands.registerCommand('vscode-spring-cli.guide.apply', handleGuideApply),
commands.registerCommand('vscode-spring-cli.guide.run', handleGuideRun),
tasks.registerTaskProvider(SPRING_CLI_TASK_TYPE, new CliTaskProvider(CLI))
);

View File

@@ -1,10 +1,34 @@
import { Uri } from "vscode";
import { Uri, window, workspace } from "vscode";
import { getTargetGuideMardown } from "./utils";
import { CLI } from "./extension";
import { createConverter } from "vscode-languageclient/lib/common/protocolConverter";
import fs from "fs";
const CONVERTER = createConverter(undefined, true, true);
export async function handleGuideApply(uri: Uri) {
if (!uri) {
uri = await getTargetGuideMardown();
}
return CLI.guideApply(uri);
}
export async function handleGuideRun(uri: Uri) {
if (!uri) {
uri = await getTargetGuideMardown();
}
const lspEdit = await CLI.guideLspEdit(uri);
const workspaceEdit = await CONVERTER.asWorkspaceEdit(lspEdit);
// This is some sort of workaround for undo
// If editors for existing doc not opened then undo isn't properly working
await Promise.all(workspaceEdit.entries().map(async ([uri, edits]) => {
if (fs.existsSync(uri.fsPath)) {
const doc = await workspace.openTextDocument(uri.fsPath);
await window.showTextDocument(doc);
}
}));
return await workspace.applyEdit(workspaceEdit, {
isRefactoring: true
});
}