From a103007b8310f1e0dc7e70e6eaee7c2fc6d556bf Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Thu, 10 Feb 2022 17:05:49 -0800 Subject: [PATCH] Add autodetection of Java styles --- pom.xml | 2 +- .../maven/MavenMojoProjectParser.java | 27 +++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index f55d551e..946f0e60 100644 --- a/pom.xml +++ b/pom.xml @@ -40,7 +40,7 @@ - 7.18.0 + 7.18.1 scm:git:ssh://git@github.com/openrewrite/rewrite-maven-plugin.git diff --git a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java index f32bc8a4..2e009324 100644 --- a/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java +++ b/src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java @@ -21,6 +21,10 @@ import org.openrewrite.java.JavaParser; import org.openrewrite.java.marker.JavaProject; import org.openrewrite.java.marker.JavaVersion; +import org.openrewrite.java.style.Autodetect; +import org.openrewrite.java.style.ImportLayoutStyle; +import org.openrewrite.java.style.SpacesStyle; +import org.openrewrite.java.style.TabsAndIndentsStyle; import org.openrewrite.java.tree.J; import org.openrewrite.marker.BuildTool; import org.openrewrite.marker.Generated; @@ -48,6 +52,7 @@ import static java.util.Collections.emptyList; import static java.util.stream.Collectors.toList; import static org.openrewrite.Tree.randomId; +import static org.openrewrite.internal.ListUtils.map; // ----------------------------------------------------------------------------------------------------------------- // Notes About Provenance Information: @@ -308,7 +313,7 @@ public List listSourceFiles(Iterable styles, // JavaParser will add SourceSet Markers to any Java SourceFile, so only adding the project provenance info to // java source. - sourceFiles.addAll(ListUtils.map(javaParser.parse(mainJavaSources, baseDir, ctx), + sourceFiles.addAll(ListUtils.map(maybeAutodetectStyles(javaParser.parse(mainJavaSources, baseDir, ctx), styles), addProvenance(baseDir, projectProvenance, generatedSourcePaths))); ResourceParser rp = new ResourceParser(logger, exclusions, sizeThresholdMb); @@ -330,7 +335,7 @@ public List listSourceFiles(Iterable styles, // JavaParser will add SourceSet Markers to any Java SourceFile, so only adding the project provenance info to // java source. sourceFiles.addAll(ListUtils.map( - javaParser.parse(listJavaSources(mavenProject.getBuild().getTestSourceDirectory()), baseDir, ctx), + maybeAutodetectStyles(javaParser.parse(listJavaSources(mavenProject.getBuild().getTestSourceDirectory()), baseDir, ctx), styles), addProvenance(baseDir, projectProvenance, null))); // Any resources parsed from "test/resources" should also have the test source set added to them. @@ -347,6 +352,24 @@ public List listSourceFiles(Iterable styles, return sourceFiles; } + private List maybeAutodetectStyles(List sourceFiles, @Nullable Iterable styles) { + if (styles != null) { + return sourceFiles; + } + Autodetect autodetect = Autodetect.detect(sourceFiles); + + Collection namedStyles = Collections.singletonList(autodetect); + + ImportLayoutStyle importLayout = NamedStyles.merge(ImportLayoutStyle.class, namedStyles); + SpacesStyle spacesStyle = NamedStyles.merge(SpacesStyle.class, namedStyles); + TabsAndIndentsStyle tabsStyle = NamedStyles.merge(TabsAndIndentsStyle.class, namedStyles); + + return map(sourceFiles, cu -> { + List markers = ListUtils.concat(map(cu.getMarkers().getMarkers(), m -> m instanceof NamedStyles ? null : m), autodetect); + return cu.withMarkers(cu.getMarkers().withMarkers(markers)); + }); + } + private static UnaryOperator addProvenance(Path baseDir, List provenance, @Nullable Collection generatedSources) { return s -> { for (Marker marker : provenance) {