Skip to content

Commit

Permalink
Add autodetection of Java styles
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Feb 11, 2022
1 parent a504ae0 commit a103007
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</scm>

<properties>
<rewrite.version>7.18.0</rewrite.version>
<rewrite.version>7.18.1</rewrite.version>

<!-- using 'ssh' url scheme by default, which assumes a human is performing git operations leveraging an ssh key -->
<developerConnectionUrl>scm:git:ssh://git@github.com/openrewrite/rewrite-maven-plugin.git</developerConnectionUrl>
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -308,7 +313,7 @@ public List<SourceFile> listSourceFiles(Iterable<NamedStyles> 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);
Expand All @@ -330,7 +335,7 @@ public List<SourceFile> listSourceFiles(Iterable<NamedStyles> 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.
Expand All @@ -347,6 +352,24 @@ public List<SourceFile> listSourceFiles(Iterable<NamedStyles> styles,
return sourceFiles;
}

private List<J.CompilationUnit> maybeAutodetectStyles(List<J.CompilationUnit> sourceFiles, @Nullable Iterable<NamedStyles> styles) {
if (styles != null) {
return sourceFiles;
}
Autodetect autodetect = Autodetect.detect(sourceFiles);

Collection<NamedStyles> 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<Marker> markers = ListUtils.concat(map(cu.getMarkers().getMarkers(), m -> m instanceof NamedStyles ? null : m), autodetect);
return cu.withMarkers(cu.getMarkers().withMarkers(markers));
});
}

private static <S extends SourceFile> UnaryOperator<S> addProvenance(Path baseDir, List<Marker> provenance, @Nullable Collection<Path> generatedSources) {
return s -> {
for (Marker marker : provenance) {
Expand Down

0 comments on commit a103007

Please sign in to comment.