From 19e3b38804af3247b35a0c19b908e981d28b0d2a Mon Sep 17 00:00:00 2001 From: Alex Boyko Date: Thu, 3 Oct 2024 08:36:38 -0400 Subject: [PATCH] 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 --- .../vscode/commons/rewrite/ORDocUtils.java | 2 - .../spring-boot-language-server/pom.xml | 10 ----- .../copilot/CopilotAgentCommandHandler.java | 3 +- .../copilot/ProjectArtifactEditGenerator.java | 38 +--------------- .../boot/java/copilot/util/PomReader.java | 45 ------------------- .../java/rewrite/RewriteRecipeRepository.java | 1 - .../lib/copilot/guideApply.ts | 2 +- .../lib/copilot/springBootAgent.ts | 18 ++++---- 8 files changed, 12 insertions(+), 107 deletions(-) delete mode 100644 headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/util/PomReader.java diff --git a/headless-services/commons/commons-rewrite/src/main/java/org/springframework/ide/vscode/commons/rewrite/ORDocUtils.java b/headless-services/commons/commons-rewrite/src/main/java/org/springframework/ide/vscode/commons/rewrite/ORDocUtils.java index e9deeda71..7949f1a2b 100644 --- a/headless-services/commons/commons-rewrite/src/main/java/org/springframework/ide/vscode/commons/rewrite/ORDocUtils.java +++ b/headless-services/commons/commons-rewrite/src/main/java/org/springframework/ide/vscode/commons/rewrite/ORDocUtils.java @@ -117,7 +117,6 @@ public class ORDocUtils { log.error("Diff conversion failed", ex); } } - System.out.println("edits "+ edit); return Optional.of(edit); } return Optional.empty(); @@ -170,7 +169,6 @@ public class ORDocUtils { createDeleteFileEdit(docUri, we); } else { String docUri = result.getBefore().getSourcePath().toUri().toASCIIString(); - System.out.println(result.getBefore().getSourcePath().toString()); createUpdateFileEdit(documents, docUri, result.getBefore().printAll(), result.getAfter().printAll(), changeAnnotationId, we); } diff --git a/headless-services/spring-boot-language-server/pom.xml b/headless-services/spring-boot-language-server/pom.xml index 0668710bf..d5e4620d1 100644 --- a/headless-services/spring-boot-language-server/pom.xml +++ b/headless-services/spring-boot-language-server/pom.xml @@ -210,16 +210,6 @@ commonmark 0.22.0 - - org.apache.maven - maven-model - 3.8.1 - - com.fasterxml.jackson.dataformat jackson-dataformat-xml diff --git a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/CopilotAgentCommandHandler.java b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/CopilotAgentCommandHandler.java index 9f4f3bcfa..ea761f94f 100644 --- a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/CopilotAgentCommandHandler.java +++ b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/CopilotAgentCommandHandler.java @@ -63,8 +63,7 @@ public class CopilotAgentCommandHandler { private CompletableFuture createLspEdits(ExecuteCommandParams params) throws IOException { log.info("Command Handler for lsp edits: "); String docURI = ((JsonElement) params.getArguments().get(0)).getAsString(); - String path = ((JsonElement) params.getArguments().get(0)).getAsString(); - String content = ((JsonElement) params.getArguments().get(2)).getAsString(); + String content = ((JsonElement) params.getArguments().get(1)).getAsString(); IJavaProject project = this.projectFinder.find(new TextDocumentIdentifier(docURI)).get(); List projectArtifacts = computeProjectArtifacts(content); diff --git a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/ProjectArtifactEditGenerator.java b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/ProjectArtifactEditGenerator.java index 4f5b81f06..50466e2ac 100644 --- a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/ProjectArtifactEditGenerator.java +++ b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/ProjectArtifactEditGenerator.java @@ -19,15 +19,12 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.io.IOUtils; -import org.apache.maven.model.Dependency; -import org.apache.maven.model.Model; import org.eclipse.lsp4j.ChangeAnnotation; import org.eclipse.lsp4j.WorkspaceEdit; import org.openrewrite.Result; import org.openrewrite.xml.tree.Xml; import org.springframework.ide.vscode.boot.java.copilot.InjectMavenActionHandler.MavenDependencyMetadata; import org.springframework.ide.vscode.boot.java.copilot.util.ClassNameExtractor; -import org.springframework.ide.vscode.boot.java.copilot.util.PomReader; import org.springframework.ide.vscode.boot.java.copilot.util.PropertyFileUtils; import org.springframework.ide.vscode.boot.java.copilot.util.SpringCliException; import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService; @@ -40,12 +37,6 @@ public class ProjectArtifactEditGenerator { private final Path projectPath; - private final String readmeFileName; - - private final Pattern compiledGroupIdPattern; - - private final Pattern compiledArtifactIdPattern; - private final SimpleTextDocumentService simpleTextDocumentService; public ProjectArtifactEditGenerator(SimpleTextDocumentService simpleTextDocumentService, @@ -53,9 +44,6 @@ public class ProjectArtifactEditGenerator { this.simpleTextDocumentService = simpleTextDocumentService; this.projectArtifacts = projectArtifacts; this.projectPath = projectPath; - this.readmeFileName = readmeFileName; - compiledGroupIdPattern = Pattern.compile("(.*?)"); - compiledArtifactIdPattern = Pattern.compile("(.*?)"); } public ProcessArtifactResult process() throws IOException { @@ -136,24 +124,19 @@ public class ProjectArtifactEditGenerator { private void writeMavenDependencies(ProjectArtifact projectArtifact, Path projectPath, String changeAnnotationId, WorkspaceEdit we) { - PomReader pomReader = new PomReader(); Path currentProjectPomPath = this.projectPath.resolve("pom.xml"); if (Files.notExists(currentProjectPomPath)) { throw new SpringCliException("Could not find pom.xml in " + this.projectPath + ". Make sure you are running the command in the project's root directory."); } - Model currentModel = pomReader.readPom(currentProjectPomPath.toFile()); - List currentDependencies = currentModel.getDependencies(); InjectMavenActionHandler injectMavenActionHandler = new InjectMavenActionHandler(null, new HashMap<>(), projectPath); - + // Move the parsing to injectMavenActionHandler List xmlDocuments = injectMavenActionHandler.parseToXml(projectArtifact.getText()); for (Xml.Document xmlDocument : xmlDocuments) { MavenDependencyMetadata dep = injectMavenActionHandler.findMavenDependencyTags(xmlDocument); - if (!candidateDependencyAlreadyPresent(dep, currentDependencies)) { - injectMavenActionHandler.injectDependency(dep); - } + injectMavenActionHandler.injectDependency(dep); } List res = injectMavenActionHandler.run().getChangeset().getAllResults(); @@ -164,23 +147,6 @@ public class ProjectArtifactEditGenerator { } } - private boolean candidateDependencyAlreadyPresent(MavenDependencyMetadata dep, - List currentDependencies) { - String candidateGroupId = dep.groupId(); - String candidateArtifactId = dep.artifactId(); - boolean candidateDependencyAlreadyPresent = false; - for (Dependency currentDependency : currentDependencies) { - String currentGroupId = currentDependency.getGroupId(); - String currentArtifactId = currentDependency.getArtifactId(); - if (candidateGroupId.equals(currentGroupId) && candidateArtifactId.equals(currentArtifactId)) { - candidateDependencyAlreadyPresent = true; - break; - } - } - return candidateDependencyAlreadyPresent; - - } - private void writeApplicationProperties(ProjectArtifact projectArtifact, Path projectPath, String changeAnnotationId, WorkspaceEdit we) throws IOException { Path applicationPropertiesPath = projectPath.resolve("src").resolve("main").resolve("resources") diff --git a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/util/PomReader.java b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/util/PomReader.java deleted file mode 100644 index efaf49c6e..000000000 --- a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/util/PomReader.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.springframework.ide.vscode.boot.java.copilot.util; - -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.io.Reader; -import java.nio.file.Files; - -import org.apache.maven.model.Model; -import org.apache.maven.model.io.xpp3.MavenXpp3Reader; -import org.codehaus.plexus.util.xml.pull.XmlPullParserException; - -/** - * @author Ryan Baxter - */ -public class PomReader { - - /** - * Returns a parsed POM. - */ - public Model readPom(File file) { - File pom = file; - if (file.isDirectory()) { - pom = new File(file, "pom.xml"); - } - if (!pom.exists()) { - return null; - } - String fileText = ""; - try (Reader reader = new FileReader(pom)) { - if (file.isFile()) { - fileText = new String(Files.readAllBytes(file.toPath())); - } - MavenXpp3Reader xpp3Reader = new MavenXpp3Reader(); - return xpp3Reader.read(reader); - } - catch (XmlPullParserException | IOException ex) { - if (file.isFile() && fileText.length() == 0) { - throw new IllegalStateException("File [" + pom.getAbsolutePath() + "] is empty", ex); - } - throw new IllegalStateException("Failed to read file: " + pom.getAbsolutePath(), ex); - } - } - -} diff --git a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/RewriteRecipeRepository.java b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/RewriteRecipeRepository.java index 0da6b4ba2..c003a9fd9 100644 --- a/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/RewriteRecipeRepository.java +++ b/headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/RewriteRecipeRepository.java @@ -56,7 +56,6 @@ import org.openrewrite.config.RecipeDescriptor; import org.openrewrite.config.YamlResourceLoader; import org.openrewrite.internal.InMemoryLargeSourceSet; import org.openrewrite.java.JavaParser; -import org.openrewrite.maven.AddDependency; import org.openrewrite.maven.MavenParser; import org.openrewrite.tree.ParseError; import org.slf4j.Logger; diff --git a/vscode-extensions/vscode-spring-boot/lib/copilot/guideApply.ts b/vscode-extensions/vscode-spring-boot/lib/copilot/guideApply.ts index 6c70b4bac..628dcedb3 100644 --- a/vscode-extensions/vscode-spring-boot/lib/copilot/guideApply.ts +++ b/vscode-extensions/vscode-spring-boot/lib/copilot/guideApply.ts @@ -21,7 +21,7 @@ export async function applyLspEdit(uri: Uri) { }, 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 lspEdit = await commands.executeCommand("sts/copilot/agent/lspEdits", uri.toString(), fileContent); const workspaceEdit = await CONVERTER.asWorkspaceEdit(lspEdit); diff --git a/vscode-extensions/vscode-spring-boot/lib/copilot/springBootAgent.ts b/vscode-extensions/vscode-spring-boot/lib/copilot/springBootAgent.ts index 673980464..279fa142e 100644 --- a/vscode-extensions/vscode-spring-boot/lib/copilot/springBootAgent.ts +++ b/vscode-extensions/vscode-spring-boot/lib/copilot/springBootAgent.ts @@ -65,23 +65,21 @@ export default class SpringBootChatAgent { // Chat request to copilot LLM const response = await this.copilotRequest.chatRequest(messages, {}, cancellationToken); - - // write the response to markdown file - const targetMarkdownUri = await writeResponseToFile(response, bootProjInfo.name, selectedProject.fsPath); - let documentContent; - if (!targetMarkdownUri) { - documentContent = 'Note: The code provided is just an example and may not be suitable for production use. \n ' + response; + if (response == null || response === '') { + documentContent = 'Failed to process the request. Please try again.'; } else { - // modify the response from copilot LLM i.e. make response Boot 3 compliant if necessary if (bootProjInfo.springBootVersion.startsWith('3')) { - const enhancedResponse = await commands.executeCommand("sts/copilot/agent/enhanceResponse", response) as string; - await writeResponseToFile(enhancedResponse, bootProjInfo.name, selectedProject.fsPath); + documentContent = await commands.executeCommand("sts/copilot/agent/enhanceResponse", response) as string; + } else { + documentContent = 'Note: The code provided is just an example and may not be suitable for production use. \n ' + response; } - documentContent = await workspace.fs.readFile(targetMarkdownUri); } + // write the final response to markdown file + await writeResponseToFile(documentContent, bootProjInfo.name, selectedProject.fsPath); + const chatResponse = Buffer.from(documentContent).toString(); stream.markdown(chatResponse); stream.button({