Skip to content

Commit 19e3b38

Browse files
BoykoAlexvudayanimartinlippert
authored
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 <[email protected]> Co-authored-by: Martin Lippert <[email protected]>
1 parent 53dbdea commit 19e3b38

File tree

8 files changed

+12
-107
lines changed

8 files changed

+12
-107
lines changed

headless-services/commons/commons-rewrite/src/main/java/org/springframework/ide/vscode/commons/rewrite/ORDocUtils.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public static Optional<TextDocumentEdit> computeTextDocEdit(TextDocument doc, St
117117
log.error("Diff conversion failed", ex);
118118
}
119119
}
120-
System.out.println("edits "+ edit);
121120
return Optional.of(edit);
122121
}
123122
return Optional.empty();
@@ -170,7 +169,6 @@ public static Optional<WorkspaceEdit> createWorkspaceEdit(SimpleTextDocumentServ
170169
createDeleteFileEdit(docUri, we);
171170
} else {
172171
String docUri = result.getBefore().getSourcePath().toUri().toASCIIString();
173-
System.out.println(result.getBefore().getSourcePath().toString());
174172
createUpdateFileEdit(documents, docUri, result.getBefore().printAll(), result.getAfter().printAll(), changeAnnotationId, we);
175173
}
176174

headless-services/spring-boot-language-server/pom.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,6 @@
210210
<artifactId>commonmark</artifactId>
211211
<version>0.22.0</version>
212212
</dependency>
213-
<dependency>
214-
<groupId>org.apache.maven</groupId>
215-
<artifactId>maven-model</artifactId>
216-
<version>3.8.1</version>
217-
</dependency>
218-
<!-- <dependency>
219-
<groupId>javax.xml.bind</groupId>
220-
<artifactId>jaxb-api</artifactId>
221-
<version>2.3.1</version>
222-
</dependency>-->
223213
<dependency>
224214
<groupId>com.fasterxml.jackson.dataformat</groupId>
225215
<artifactId>jackson-dataformat-xml</artifactId>

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/CopilotAgentCommandHandler.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ private CompletableFuture<Object> enhanceResponseHandler(ExecuteCommandParams pa
6363
private CompletableFuture<WorkspaceEdit> createLspEdits(ExecuteCommandParams params) throws IOException {
6464
log.info("Command Handler for lsp edits: ");
6565
String docURI = ((JsonElement) params.getArguments().get(0)).getAsString();
66-
String path = ((JsonElement) params.getArguments().get(0)).getAsString();
67-
String content = ((JsonElement) params.getArguments().get(2)).getAsString();
66+
String content = ((JsonElement) params.getArguments().get(1)).getAsString();
6867

6968
IJavaProject project = this.projectFinder.find(new TextDocumentIdentifier(docURI)).get();
7069
List<ProjectArtifact> projectArtifacts = computeProjectArtifacts(content);

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/ProjectArtifactEditGenerator.java

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@
1919
import java.util.regex.Pattern;
2020

2121
import org.apache.commons.io.IOUtils;
22-
import org.apache.maven.model.Dependency;
23-
import org.apache.maven.model.Model;
2422
import org.eclipse.lsp4j.ChangeAnnotation;
2523
import org.eclipse.lsp4j.WorkspaceEdit;
2624
import org.openrewrite.Result;
2725
import org.openrewrite.xml.tree.Xml;
2826
import org.springframework.ide.vscode.boot.java.copilot.InjectMavenActionHandler.MavenDependencyMetadata;
2927
import org.springframework.ide.vscode.boot.java.copilot.util.ClassNameExtractor;
30-
import org.springframework.ide.vscode.boot.java.copilot.util.PomReader;
3128
import org.springframework.ide.vscode.boot.java.copilot.util.PropertyFileUtils;
3229
import org.springframework.ide.vscode.boot.java.copilot.util.SpringCliException;
3330
import org.springframework.ide.vscode.commons.languageserver.util.SimpleTextDocumentService;
@@ -40,22 +37,13 @@ public class ProjectArtifactEditGenerator {
4037

4138
private final Path projectPath;
4239

43-
private final String readmeFileName;
44-
45-
private final Pattern compiledGroupIdPattern;
46-
47-
private final Pattern compiledArtifactIdPattern;
48-
4940
private final SimpleTextDocumentService simpleTextDocumentService;
5041

5142
public ProjectArtifactEditGenerator(SimpleTextDocumentService simpleTextDocumentService,
5243
List<ProjectArtifact> projectArtifacts, Path projectPath, String readmeFileName) {
5344
this.simpleTextDocumentService = simpleTextDocumentService;
5445
this.projectArtifacts = projectArtifacts;
5546
this.projectPath = projectPath;
56-
this.readmeFileName = readmeFileName;
57-
compiledGroupIdPattern = Pattern.compile("<groupId>(.*?)</groupId>");
58-
compiledArtifactIdPattern = Pattern.compile("<artifactId>(.*?)</artifactId>");
5947
}
6048

6149
public ProcessArtifactResult<WorkspaceEdit> process() throws IOException {
@@ -136,24 +124,19 @@ private void writeTestCode(ProjectArtifact projectArtifact, Path projectPath, St
136124

137125
private void writeMavenDependencies(ProjectArtifact projectArtifact, Path projectPath, String changeAnnotationId,
138126
WorkspaceEdit we) {
139-
PomReader pomReader = new PomReader();
140127
Path currentProjectPomPath = this.projectPath.resolve("pom.xml");
141128
if (Files.notExists(currentProjectPomPath)) {
142129
throw new SpringCliException("Could not find pom.xml in " + this.projectPath
143130
+ ". Make sure you are running the command in the project's root directory.");
144131
}
145-
Model currentModel = pomReader.readPom(currentProjectPomPath.toFile());
146-
List<Dependency> currentDependencies = currentModel.getDependencies();
147132

148133
InjectMavenActionHandler injectMavenActionHandler = new InjectMavenActionHandler(null, new HashMap<>(),
149134
projectPath);
150-
135+
// Move the parsing to injectMavenActionHandler
151136
List<Xml.Document> xmlDocuments = injectMavenActionHandler.parseToXml(projectArtifact.getText());
152137
for (Xml.Document xmlDocument : xmlDocuments) {
153138
MavenDependencyMetadata dep = injectMavenActionHandler.findMavenDependencyTags(xmlDocument);
154-
if (!candidateDependencyAlreadyPresent(dep, currentDependencies)) {
155-
injectMavenActionHandler.injectDependency(dep);
156-
}
139+
injectMavenActionHandler.injectDependency(dep);
157140
}
158141

159142
List<Result> res = injectMavenActionHandler.run().getChangeset().getAllResults();
@@ -164,23 +147,6 @@ private void writeMavenDependencies(ProjectArtifact projectArtifact, Path projec
164147
}
165148
}
166149

167-
private boolean candidateDependencyAlreadyPresent(MavenDependencyMetadata dep,
168-
List<Dependency> currentDependencies) {
169-
String candidateGroupId = dep.groupId();
170-
String candidateArtifactId = dep.artifactId();
171-
boolean candidateDependencyAlreadyPresent = false;
172-
for (Dependency currentDependency : currentDependencies) {
173-
String currentGroupId = currentDependency.getGroupId();
174-
String currentArtifactId = currentDependency.getArtifactId();
175-
if (candidateGroupId.equals(currentGroupId) && candidateArtifactId.equals(currentArtifactId)) {
176-
candidateDependencyAlreadyPresent = true;
177-
break;
178-
}
179-
}
180-
return candidateDependencyAlreadyPresent;
181-
182-
}
183-
184150
private void writeApplicationProperties(ProjectArtifact projectArtifact, Path projectPath,
185151
String changeAnnotationId, WorkspaceEdit we) throws IOException {
186152
Path applicationPropertiesPath = projectPath.resolve("src").resolve("main").resolve("resources")

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/copilot/util/PomReader.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/java/rewrite/RewriteRecipeRepository.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import org.openrewrite.config.YamlResourceLoader;
5757
import org.openrewrite.internal.InMemoryLargeSourceSet;
5858
import org.openrewrite.java.JavaParser;
59-
import org.openrewrite.maven.AddDependency;
6059
import org.openrewrite.maven.MavenParser;
6160
import org.openrewrite.tree.ParseError;
6261
import org.slf4j.Logger;

vscode-extensions/vscode-spring-boot/lib/copilot/guideApply.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function applyLspEdit(uri: Uri) {
2121
}, async (progress, cancellation) => {
2222
progress.report({ message: "applying edits..." });
2323
const fileContent = (await readResponseFromFile(uri)).toString();
24-
const lspEdit = await commands.executeCommand("sts/copilot/agent/lspEdits", uri.toString(), path.dirname(uri.fsPath), fileContent);
24+
const lspEdit = await commands.executeCommand("sts/copilot/agent/lspEdits", uri.toString(), fileContent);
2525
const workspaceEdit = await CONVERTER.asWorkspaceEdit(lspEdit);
2626

2727

vscode-extensions/vscode-spring-boot/lib/copilot/springBootAgent.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,21 @@ export default class SpringBootChatAgent {
6565

6666
// Chat request to copilot LLM
6767
const response = await this.copilotRequest.chatRequest(messages, {}, cancellationToken);
68-
69-
// write the response to markdown file
70-
const targetMarkdownUri = await writeResponseToFile(response, bootProjInfo.name, selectedProject.fsPath);
71-
7268
let documentContent;
7369

74-
if (!targetMarkdownUri) {
75-
documentContent = 'Note: The code provided is just an example and may not be suitable for production use. \n ' + response;
70+
if (response == null || response === '') {
71+
documentContent = 'Failed to process the request. Please try again.';
7672
} else {
77-
// modify the response from copilot LLM i.e. make response Boot 3 compliant if necessary
7873
if (bootProjInfo.springBootVersion.startsWith('3')) {
79-
const enhancedResponse = await commands.executeCommand("sts/copilot/agent/enhanceResponse", response) as string;
80-
await writeResponseToFile(enhancedResponse, bootProjInfo.name, selectedProject.fsPath);
74+
documentContent = await commands.executeCommand("sts/copilot/agent/enhanceResponse", response) as string;
75+
} else {
76+
documentContent = 'Note: The code provided is just an example and may not be suitable for production use. \n ' + response;
8177
}
82-
documentContent = await workspace.fs.readFile(targetMarkdownUri);
8378
}
8479

80+
// write the final response to markdown file
81+
await writeResponseToFile(documentContent, bootProjInfo.name, selectedProject.fsPath);
82+
8583
const chatResponse = Buffer.from(documentContent).toString();
8684
stream.markdown(chatResponse);
8785
stream.button({

0 commit comments

Comments
 (0)