From 642ae3c73d642efe9087f51d33d75c8c65ebda3e Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Mon, 7 Oct 2024 14:27:51 +0200 Subject: [PATCH] Kotlin formatting auto-detection should only sample Kotlin sources --- .../openrewrite/kotlin/style/Autodetect.java | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/openrewrite/kotlin/style/Autodetect.java b/src/main/java/org/openrewrite/kotlin/style/Autodetect.java index d9b48243..eac9fe93 100644 --- a/src/main/java/org/openrewrite/kotlin/style/Autodetect.java +++ b/src/main/java/org/openrewrite/kotlin/style/Autodetect.java @@ -18,6 +18,7 @@ import com.fasterxml.jackson.annotation.JsonCreator; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.Value; import org.jspecify.annotations.Nullable; import org.openrewrite.SourceFile; @@ -69,7 +70,7 @@ public static class Detector { private final FindTrailingCommaVisitor findTrailingComma = new FindTrailingCommaVisitor(); public void sample(SourceFile cu) { - if (cu instanceof JavaSourceFile) { + if (cu instanceof K.CompilationUnit) { findImportLayout.visitNonNull(cu, 0); findIndent.visitNonNull(cu, indentStatistics); findSpaces.visitNonNull(cu, spacesStatistics); @@ -174,16 +175,11 @@ private static class IndentStatistics { private int multilineAlignedToFirstArgument = 0; private int multilineNotAlignedToFirstArgument = 0; + @Getter private int depth = 0; - private int continuationDepth = 1; - - public int getDepth() { - return depth; - } - public int getContinuationDepth() { - return continuationDepth; - } + @Getter + private int continuationDepth = 1; public void incrementDepth() { depth++; @@ -621,13 +617,9 @@ public ImportLayoutStyle getImportLayoutStyle() { // Add static imports at the top if it's the standard. boolean addNewLine = false; - addNewLine = false; // There are no non-static imports, add a block of all other import. if (!insertAllOthers) { - if (addNewLine) { - builder = builder.blankLine(); - } builder = builder.importAllOthers(); // Add java/javax if they're missing from the block that is being used as a template. @@ -1145,7 +1137,7 @@ public J.If visitIf(J.If iff, SpacesStatistics stats) { public J.Lambda visitLambda(J.Lambda lambda, SpacesStatistics stats) { List parameters = lambda.getParameters().getParameters(); if (parameters.size() > 1) { - List> paddedParameters = lambda.getParameters().getPadding().getParams(); + List> paddedParameters = lambda.getParameters().getPadding().getParameters(); for (int i = 0; i < paddedParameters.size() - 1; i++) { stats.beforeComma += hasSpace(paddedParameters.get(i).getAfter()); }