Skip to content

Commit

Permalink
Always autodetect styles, giving precedence to explicitly configured …
Browse files Browse the repository at this point in the history
…styles
  • Loading branch information
sambsnyd committed Oct 6, 2022
1 parent 4fb50b9 commit 2b008ab
Showing 1 changed file with 4 additions and 17 deletions.
21 changes: 4 additions & 17 deletions src/main/java/org/openrewrite/maven/MavenMojoProjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
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 All @@ -37,7 +34,6 @@
import org.openrewrite.maven.cache.RocksdbMavenPomCache;
import org.openrewrite.maven.internal.RawRepositories;
import org.openrewrite.maven.tree.ProfileActivation;
import org.openrewrite.style.GeneralFormatStyle;
import org.openrewrite.style.NamedStyles;
import org.openrewrite.tree.ParsingExecutionContextView;
import org.openrewrite.xml.tree.Xml;
Expand Down Expand Up @@ -223,7 +219,7 @@ private List<SourceFile> processMainSources(

// JavaParser will add SourceSet Markers to any Java SourceFile, so only adding the project provenance info to
// java source.
List<J.CompilationUnit> parsedJava = ListUtils.map(maybeAutodetectStyles(javaParser.parse(mainJavaSources, baseDir, ctx), styles),
List<J.CompilationUnit> parsedJava = ListUtils.map(autodetectStyles(javaParser.parse(mainJavaSources, baseDir, ctx), styles),
addProvenance(baseDir, projectProvenance, generatedSourcePaths));
logDebug(mavenProject, "Parsed " + parsedJava.size() + " java source files in main scope.");

Expand Down Expand Up @@ -266,7 +262,7 @@ private List<SourceFile> processTestSources(
alreadyParsed.addAll(testJavaSources);

List<J.CompilationUnit> parsedJava = ListUtils.map(
maybeAutodetectStyles(javaParser.parse(testJavaSources, baseDir, ctx), styles),
autodetectStyles(javaParser.parse(testJavaSources, baseDir, ctx), styles),
addProvenance(baseDir, projectProvenance, null));

logDebug(mavenProject, "Parsed " + parsedJava.size() + " java source files in test scope.");
Expand Down Expand Up @@ -496,17 +492,8 @@ private Set<Path> pathsToOtherMavenProjects(MavenProject mavenProject) {
.collect(Collectors.toSet());
}

private List<J.CompilationUnit> maybeAutodetectStyles(List<J.CompilationUnit> sourceFiles, Iterable<NamedStyles> styles) {
ImportLayoutStyle importLayout = NamedStyles.merge(ImportLayoutStyle.class, styles);
SpacesStyle spacesStyle = NamedStyles.merge(SpacesStyle.class, styles);
TabsAndIndentsStyle tabsStyle = NamedStyles.merge(TabsAndIndentsStyle.class, styles);
GeneralFormatStyle generalStyle = NamedStyles.merge(GeneralFormatStyle.class, styles);
if(importLayout != null && spacesStyle != null && tabsStyle != null && generalStyle != null) {
// No need to autodetect if all the styles it would detect are already present
return sourceFiles;
}
Autodetect autodetect = Autodetect.detect(sourceFiles);
return map(sourceFiles, cu -> cu.withMarkers(cu.getMarkers().add(autodetect)));
private List<J.CompilationUnit> autodetectStyles(List<J.CompilationUnit> sourceFiles, Iterable<NamedStyles> styles) {
return map(sourceFiles, cu -> cu.withMarkers(cu.getMarkers().add(Autodetect.detect(sourceFiles))));
}

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

0 comments on commit 2b008ab

Please sign in to comment.