Skip to content

Commit 13c8d64

Browse files
authored
Rewrite snippet formatting based on styles detected (#1775)
Signed-off-by: BoykoAlex <alex.boyko@broadcom.com>
1 parent c7d7c57 commit 13c8d64

File tree

6 files changed

+329
-307
lines changed

6 files changed

+329
-307
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2025 Broadcom, Inc.
2+
* Copyright (c) 2025, 2026 Broadcom, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -80,6 +80,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
8080
public MethodDeclaration visitMethodDeclaration(MethodDeclaration method, ExecutionContext ctx) {
8181
MethodDeclaration m = super.visitMethodDeclaration(method, ctx);
8282
if (matcher.matches(m.getMethodType())) {
83+
boolean changed = false;
8384
Optional<Annotation> optAnnotation = m.getLeadingAnnotations().stream().filter(a -> TypeUtils.isOfClassType(a.getType(), annotationType)).findFirst();
8485
if (optAnnotation.isEmpty()) {
8586
List<J.Annotation> annotations = new ArrayList<>(m.getLeadingAnnotations());
@@ -90,24 +91,28 @@ public MethodDeclaration visitMethodDeclaration(MethodDeclaration method, Execut
9091
Markers.EMPTY,
9192
new J.Identifier(Tree.randomId(), Space.EMPTY, Markers.EMPTY, List.of(), at.getClassName(), at, null),
9293
null);
93-
annotations.add(autoFormat(annotation, ctx));
94+
annotations.add(annotation);
9495
m = m.withLeadingAnnotations(annotations);
95-
m = autoFormat(m, m.getName(), ctx, getCursor().getParent());
9696
maybeAddImport(annotationType);
97-
optAnnotation = Optional.of(annotation);
97+
changed = true;
9898
}
9999
if (attributes != null) {
100100
for (Attribute attr : attributes) {
101-
m = (MethodDeclaration) new AddOrUpdateAnnotationAttribute(annotationType, attr.name(),
101+
MethodDeclaration newM = (MethodDeclaration) new AddOrUpdateAnnotationAttribute(annotationType, attr.name(),
102102
attr.value(), (String) null, null, null).getVisitor().visit(m, ctx, getCursor().getParent());
103+
if (newM != m) {
104+
m = newM;
105+
changed = true;
106+
}
103107
}
104108
}
105-
109+
if (changed) {
110+
m = autoFormat(m, m.getName(), ctx, getCursor().getParent());
111+
}
106112
}
107113
return m;
108114
}
109115
});
110116
}
111117

112-
113118
}

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -305,16 +305,15 @@ public static List<CompilationUnit> parse(JavaParser parser, Iterable<Path> sour
305305
synchronized(parser) {
306306
cus = parser.parse(sourceFiles, null, ctx).map(CompilationUnit.class::cast).collect(Collectors.toList());
307307
}
308-
List<J.CompilationUnit> finalCus = new ArrayList<>(cus.size());
309-
for (CompilationUnit cu : cus) {
308+
org.openrewrite.java.style.Autodetect.Detector javaDetector = org.openrewrite.java.style.Autodetect.detector();
309+
cus.forEach(javaDetector::sampleJava);
310+
org.openrewrite.java.style.Autodetect javaStyling = javaDetector.build();
311+
312+
return ListUtils.map(cus, (i, cu) -> {
313+
cu = cu.withMarkers(cu.getMarkers().addIfAbsent(javaStyling));
310314
J.CompilationUnit newCu = (J.CompilationUnit) new UpdateSourcePositions().getVisitor().visit(cu, ctx);
311-
if (newCu == null) {
312-
finalCus.add(cu);
313-
} else {
314-
finalCus.add(newCu);
315-
}
316-
}
317-
return finalCus;
315+
return (newCu == null ? cu : newCu).withMarkers(null);
316+
});
318317
}
319318

320319
public static List<SourceFile> parseInputs(JavaParser parser, List<Parser.Input> inputs, Consumer<SourceFile> parseCallback) {
@@ -339,10 +338,14 @@ public void parsed(Input input, SourceFile source) {
339338
synchronized (parser) {
340339
final ExecutionContext c = ctx;
341340
List<CompilationUnit> cus = parser.parseInputs(inputs, null, ctx).map(CompilationUnit.class::cast).collect(Collectors.toList());
342-
sourceFiles.addAll(ListUtils.map(cus, (i, cu) -> (CompilationUnit) new UpdateSourcePositions().getVisitor().visit(cu, c)));
341+
// Collect java styling info
342+
org.openrewrite.java.style.Autodetect.Detector javaDetector = org.openrewrite.java.style.Autodetect.detector();
343+
cus.forEach(javaDetector::sampleJava);
344+
org.openrewrite.java.style.Autodetect javaStyling = javaDetector.build();
345+
sourceFiles.addAll(ListUtils.map(cus, (i, cu) -> (CompilationUnit) new UpdateSourcePositions().getVisitor().visit(cu.withMarkers(cu.getMarkers().add(javaStyling)), c)));
343346
}
344347

345-
sourceFiles.addAll(new XmlParser().parseInputs(
348+
List<SourceFile> xmlSources = new XmlParser().parseInputs(
346349
inputs.stream()
347350
.filter(p -> p.getPath().getFileName().toString().endsWith(".xml") ||
348351
p.getPath().getFileName().toString().endsWith(".wsdl") ||
@@ -353,15 +356,20 @@ public void parsed(Input input, SourceFile source) {
353356
.collect(Collectors.toList()),
354357
null,
355358
ctx
356-
).collect(Collectors.toList()));
359+
).collect(Collectors.toList());
360+
org.openrewrite.xml.style.Autodetect.Detector xmlDetector = org.openrewrite.xml.style.Autodetect.detector();
361+
xmlSources.forEach(xmlDetector::sample);
362+
org.openrewrite.xml.style.Autodetect xmlStyling = xmlDetector.build();
363+
sourceFiles.addAll(ListUtils.map(xmlSources, (i, s) -> s.withMarkers(s.getMarkers().addIfAbsent(xmlStyling))));
357364

358-
sourceFiles.addAll(new YamlParser().parseInputs(
365+
List<SourceFile> yamlSources = new YamlParser().parseInputs(
359366
inputs.stream()
360367
.filter(p -> p.getPath().getFileName().toString().endsWith(".yml") || p.getPath().getFileName().toString().endsWith(".yaml"))
361368
.collect(Collectors.toList()),
362369
null,
363370
ctx
364-
).collect(Collectors.toList()));
371+
).collect(Collectors.toList());
372+
sourceFiles.addAll(yamlSources);
365373

366374
sourceFiles.addAll(new PropertiesParser().parseInputs(
367375
inputs.stream()

headless-services/commons/commons-rewrite/src/test/java/org/openrewrite/java/spring/framework/BeanMethodsNotPublicTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ final DataSource dataSource2() {
8686
return new DataSource();
8787
}
8888
89-
@Bean // comments
89+
@Bean
90+
// comments
9091
static DataSource dataSource3() {
9192
return new DataSource();
9293
}

headless-services/commons/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,14 @@
129129
<dependency>
130130
<groupId>org.openrewrite</groupId>
131131
<artifactId>rewrite-bom</artifactId>
132-
<version>8.65.0</version>
132+
<version>8.72.6</version>
133133
<type>pom</type>
134134
<scope>import</scope>
135135
</dependency>
136136
<dependency>
137137
<groupId>org.gradle</groupId>
138138
<artifactId>gradle-tooling-api</artifactId>
139-
<version>8.11</version>
139+
<version>8.14</version>
140140
</dependency>
141141
<dependency>
142142
<groupId>org.eclipse.jgit</groupId>

0 commit comments

Comments
 (0)