Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add kotlin autoDetect #619

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.6.0-SNAPSHOT</rewrite.version>
<rewrite.python.version>1.2.0-SNAPSHOT</rewrite.python.version>
<rewrite.kotlin.version>1.4.0-SNAPSHOT</rewrite.kotlin.version>
timtebeek marked this conversation as resolved.
Show resolved Hide resolved

<!-- 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