From 7fb61c4645beadc3bc32c542817001d85d466698 Mon Sep 17 00:00:00 2001 From: Kun Li Date: Mon, 21 Aug 2023 14:56:41 -0700 Subject: [PATCH 1/3] Add kotlin autoDetect --- pom.xml | 6 +++ .../maven/AbstractRewriteMojo.java | 41 +++++++++++-------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index 904aeeb4..035a3892 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,7 @@ 8.2.0-SNAPSHOT 1.1.0-SNAPSHOT + 1.4.0-SNAPSHOT scm:git:ssh://git@github.com/openrewrite/rewrite-maven-plugin.git @@ -208,6 +209,11 @@ rewrite-python ${rewrite.python.version} + + org.openrewrite + rewrite-kotlin + ${rewrite.kotlin.version} + com.puppycrawl.tools diff --git a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java index 14b8a734..9a5c72d5 100644 --- a/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java +++ b/src/main/java/org/openrewrite/maven/AbstractRewriteMojo.java @@ -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; @@ -274,28 +276,35 @@ protected List runRecipe(Recipe recipe, LargeSourceSet sourceSet, Execut private List sourcesWithAutoDetectedStyles(Stream 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 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, 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 applyAutodetectedStyle(Map, NamedStyles> stylesByType) { - return before -> { - for (Map.Entry, 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 From 4071806199abba374dabe76f039efd3814f011ee Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 22 Sep 2023 11:00:37 +0200 Subject: [PATCH 2/3] Apply suggestions from code review --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index ffe53d3e..d0461633 100644 --- a/pom.xml +++ b/pom.xml @@ -82,7 +82,7 @@ 8.6.0-SNAPSHOT 1.2.0-SNAPSHOT - 1.4.0-SNAPSHOT + 1.5.0-SNAPSHOT scm:git:ssh://git@github.com/openrewrite/rewrite-maven-plugin.git From 3d9c5a999372c4b56e31f489db3ed77c274075d7 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 4 Dec 2023 17:08:45 +0100 Subject: [PATCH 3/3] Update pom.xml --- pom.xml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index d0461633..889ec5e2 100644 --- a/pom.xml +++ b/pom.xml @@ -80,9 +80,9 @@ - 8.6.0-SNAPSHOT - 1.2.0-SNAPSHOT - 1.5.0-SNAPSHOT + 8.11.0-SNAPSHOT + 1.3.0-SNAPSHOT + 1.8.0-SNAPSHOT scm:git:ssh://git@github.com/openrewrite/rewrite-maven-plugin.git @@ -214,7 +214,6 @@ rewrite-kotlin ${rewrite.kotlin.version} - com.puppycrawl.tools checkstyle