Skip to content

Commit

Permalink
Add kotlin autoDetect
Browse files Browse the repository at this point in the history
  • Loading branch information
kunli2 committed Aug 21, 2023
1 parent 1f7d3a3 commit 7fb61c4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<!-- Pinned versions, as RELEASE would make it into the published pom.xml -->
<rewrite.version>8.2.0-SNAPSHOT</rewrite.version>
<rewrite.python.version>1.1.0-SNAPSHOT</rewrite.python.version>
<rewrite.kotlin.version>1.4.0-SNAPSHOT</rewrite.kotlin.version>

<!-- using 'ssh' url scheme by default, which assumes a human is performing git operations leveraging an ssh key -->
<developerConnectionUrl>scm:git:ssh://[email protected]/openrewrite/rewrite-maven-plugin.git
Expand Down Expand Up @@ -208,6 +209,11 @@
<artifactId>rewrite-python</artifactId>
<version>${rewrite.python.version}</version>
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-kotlin</artifactId>
<version>${rewrite.kotlin.version}</version>
</dependency>

<dependency>
<groupId>com.puppycrawl.tools</groupId>
Expand Down
41 changes: 25 additions & 16 deletions src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.ipc.http.HttpSender;
import org.openrewrite.ipc.http.HttpUrlConnectionSender;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.kotlin.tree.K;
import org.openrewrite.marker.*;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.xml.tree.Xml;
Expand Down Expand Up @@ -274,28 +276,35 @@ protected List<Result> runRecipe(Recipe recipe, LargeSourceSet sourceSet, Execut

private List<SourceFile> sourcesWithAutoDetectedStyles(Stream<SourceFile> sourceFiles) {
org.openrewrite.java.style.Autodetect.Detector javaDetector = org.openrewrite.java.style.Autodetect.detector();
org.openrewrite.kotlin.style.Autodetect.Detector kotlinDetector = org.openrewrite.kotlin.style.Autodetect.detector();
org.openrewrite.xml.style.Autodetect.Detector xmlDetector = org.openrewrite.xml.style.Autodetect.detector();

List<SourceFile> sourceFileList = sourceFiles
.peek(javaDetector::sample)
.peek(s -> {
if (s instanceof K.CompilationUnit) {
kotlinDetector.sample(s);
} else if (s instanceof J.CompilationUnit) {
javaDetector.sample(s);
}
})
.peek(xmlDetector::sample)
.collect(toList());

Map<Class<? extends Tree>, NamedStyles> stylesByType = new HashMap<>();
stylesByType.put(JavaSourceFile.class, javaDetector.build());
stylesByType.put(Xml.Document.class, xmlDetector.build());

return ListUtils.map(sourceFileList, applyAutodetectedStyle(stylesByType));
}

private UnaryOperator<SourceFile> applyAutodetectedStyle(Map<Class<? extends Tree>, NamedStyles> stylesByType) {
return before -> {
for (Map.Entry<Class<? extends Tree>, NamedStyles> styleTypeEntry : stylesByType.entrySet()) {
if (styleTypeEntry.getKey().isAssignableFrom(before.getClass())) {
before = before.withMarkers(before.getMarkers().add(styleTypeEntry.getValue()));
}
Marker javaAutoDetect = javaDetector.build();
Marker kotlinAutoDetect = kotlinDetector.build();
Marker xmlAutoDetect = xmlDetector.build();

return ListUtils.map(sourceFileList, s -> {
Markers markers = s.getMarkers();
if (s instanceof K.CompilationUnit) {
markers = markers.add(kotlinAutoDetect);
} else if (s instanceof J.CompilationUnit) {
markers = markers.add(javaAutoDetect);
} else if (s instanceof Xml.Document) {
markers = markers.add(xmlAutoDetect);
}
return before;
};
return s.withMarkers(markers);
});
}

@Nullable
Expand Down

0 comments on commit 7fb61c4

Please sign in to comment.