From 5fea517a6027a15f8daf2a549bcb0a09771dac5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nivaldo=20Bondan=C3=A7a?= Date: Fri, 14 Jun 2024 16:37:46 +0000 Subject: [PATCH] [ktfmt] Add option to configure management of trailing commas --- CHANGES.md | 2 ++ .../glue/ktfmt/KtfmtFormatterFunc.java | 4 +-- .../glue/ktfmt/KtfmtFormattingOptions.java | 28 ++++++++++++++----- .../diffplug/spotless/kotlin/KtfmtStep.java | 17 ++++++++--- plugin-gradle/CHANGES.md | 2 ++ plugin-gradle/README.md | 1 + .../gradle/spotless/KotlinExtensionTest.java | 1 + plugin-maven/CHANGES.md | 2 ++ plugin-maven/README.md | 1 + .../diffplug/spotless/maven/kotlin/Ktfmt.java | 5 +++- .../spotless/maven/kotlin/KtfmtTest.java | 9 ++++++ .../kotlin/ktfmt/trailing-commas.clean | 11 ++++++++ .../kotlin/ktfmt/trailing-commas.dirty | 8 ++++++ .../spotless/kotlin/KtfmtStepTest.java | 8 ++++++ 14 files changed, 85 insertions(+), 14 deletions(-) create mode 100644 testlib/src/main/resources/kotlin/ktfmt/trailing-commas.clean create mode 100644 testlib/src/main/resources/kotlin/ktfmt/trailing-commas.dirty diff --git a/CHANGES.md b/CHANGES.md index 68764ad9bf..cb6341da7e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +16,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Renamed property `ktfmt` option `removeUnusedImport` -> `removeUnusedImports` to match `ktfmt`. ([#2172](https://github.com/diffplug/spotless/pull/2172)) ### Fixed * Fix compatibility issue introduced by `ktfmt` `0.51`. ([#2172](https://github.com/diffplug/spotless/issues/2172)) +### Added +* Added option `manageTrailingCommas` to `ktfmt`. ([#2177](https://github.com/diffplug/spotless/pull/2177)) ## [3.0.0.BETA1] - 2024-06-04 ### Added diff --git a/lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormatterFunc.java b/lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormatterFunc.java index 50925a5a5d..bb4c9050bd 100644 --- a/lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormatterFunc.java +++ b/lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormatterFunc.java @@ -75,8 +75,8 @@ private FormattingOptions createFormattingOptions() throws Exception { ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()), ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()), ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getContinuationIndent()), - formattingOptions.getManageTrailingCommas(), - ktfmtFormattingOptions.getRemoveUnusedImport().orElse(formattingOptions.getRemoveUnusedImports()), + ktfmtFormattingOptions.getManageTrailingCommas().orElse(formattingOptions.getManageTrailingCommas()), + ktfmtFormattingOptions.getRemoveUnusedImports().orElse(formattingOptions.getRemoveUnusedImports()), formattingOptions.getDebuggingPrintOpsAfterFormatting()); } diff --git a/lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormattingOptions.java b/lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormattingOptions.java index ff8e522aae..1e9136a0b6 100644 --- a/lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormattingOptions.java +++ b/lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormattingOptions.java @@ -32,17 +32,22 @@ public final class KtfmtFormattingOptions { private Integer continuationIndent; @Nullable - private Boolean removeUnusedImport; + private Boolean removeUnusedImports; + + @Nullable + private Boolean manageTrailingCommas; public KtfmtFormattingOptions( @Nullable Integer maxWidth, @Nullable Integer blockIndent, @Nullable Integer continuationIndent, - @Nullable Boolean removeUnusedImport) { + @Nullable Boolean removeUnusedImports, + @Nullable Boolean manageTrailingCommas) { this.maxWidth = maxWidth; this.blockIndent = blockIndent; this.continuationIndent = continuationIndent; - this.removeUnusedImport = removeUnusedImport; + this.removeUnusedImports = removeUnusedImports; + this.manageTrailingCommas = manageTrailingCommas; } @Nonnull @@ -61,8 +66,13 @@ public Optional getContinuationIndent() { } @Nonnull - public Optional getRemoveUnusedImport() { - return Optional.ofNullable(removeUnusedImport); + public Optional getRemoveUnusedImports() { + return Optional.ofNullable(removeUnusedImports); + } + + @Nonnull + public Optional getManageTrailingCommas() { + return Optional.ofNullable(manageTrailingCommas); } public void setMaxWidth(int maxWidth) { @@ -86,7 +96,11 @@ public void setContinuationIndent(int continuationIndent) { this.continuationIndent = continuationIndent; } - public void setRemoveUnusedImport(boolean removeUnusedImport) { - this.removeUnusedImport = removeUnusedImport; + public void setRemoveUnusedImports(boolean removeUnusedImports) { + this.removeUnusedImports = removeUnusedImports; + } + + public void setManageTrailingCommas(boolean manageTrailingCommas) { + this.manageTrailingCommas = manageTrailingCommas; } } diff --git a/lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java b/lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java index 5e1cd7f00a..7f69be302f 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java @@ -126,17 +126,22 @@ public static class KtfmtFormattingOptions implements Serializable { @Nullable private Boolean removeUnusedImports = null; + @Nullable + private Boolean manageTrailingCommas = null; + public KtfmtFormattingOptions() {} public KtfmtFormattingOptions( @Nullable Integer maxWidth, @Nullable Integer blockIndent, @Nullable Integer continuationIndent, - @Nullable Boolean removeUnusedImports) { + @Nullable Boolean removeUnusedImports, + @Nullable Boolean manageTrailingCommas) { this.maxWidth = maxWidth; this.blockIndent = blockIndent; this.continuationIndent = continuationIndent; this.removeUnusedImports = removeUnusedImports; + this.manageTrailingCommas = manageTrailingCommas; } public void setMaxWidth(int maxWidth) { @@ -154,6 +159,10 @@ public void setContinuationIndent(int continuationIndent) { public void setRemoveUnusedImports(boolean removeUnusedImports) { this.removeUnusedImports = removeUnusedImports; } + + public void setManageTrailingCommas(boolean manageTrailingCommas) { + this.manageTrailingCommas = manageTrailingCommas; + } } /** Creates a step which formats everything - code, import order, and unused imports. */ @@ -228,9 +237,9 @@ FormatterFunc createFormat() throws Exception { } final Constructor optionsConstructor = ktfmtFormattingOptionsClass.getConstructor( - Integer.class, Integer.class, Integer.class, Boolean.class); + Integer.class, Integer.class, Integer.class, Boolean.class, Boolean.class); final Object ktfmtFormattingOptions = optionsConstructor.newInstance( - options.maxWidth, options.blockIndent, options.continuationIndent, options.removeUnusedImports); + options.maxWidth, options.blockIndent, options.continuationIndent, options.removeUnusedImports, options.manageTrailingCommas); if (style == null) { final Constructor constructor = formatterFuncClass.getConstructor(ktfmtFormattingOptionsClass); return (FormatterFunc) constructor.newInstance(ktfmtFormattingOptions); @@ -365,7 +374,7 @@ private Object getCustomFormattingOptions(Class formatterClass) throws Except /* continuationIndent = */ Optional.ofNullable(options.continuationIndent).orElse((Integer) formattingOptionsClass.getMethod("getContinuationIndent").invoke(formattingOptions)), /* removeUnusedImports = */ Optional.ofNullable(options.removeUnusedImports).orElse((Boolean) formattingOptionsClass.getMethod("getRemoveUnusedImports").invoke(formattingOptions)), /* debuggingPrintOpsAfterFormatting = */ (Boolean) formattingOptionsClass.getMethod("getDebuggingPrintOpsAfterFormatting").invoke(formattingOptions), - /* manageTrailingCommas */ (Boolean) formattingOptionsClass.getMethod("getManageTrailingCommas").invoke(formattingOptions)); + /* manageTrailingCommas */ Optional.ofNullable(options.manageTrailingCommas).orElse((Boolean) formattingOptionsClass.getMethod("getManageTrailingCommas").invoke(formattingOptions))); } } diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 2968e11403..78cb7da04e 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -9,6 +9,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Renamed property `ktfmt` option `removeUnusedImport` -> `removeUnusedImports` to match `ktfmt`. ([#2172](https://github.com/diffplug/spotless/pull/2172)) ### Fixed * Fix compatibility issue introduced by `ktfmt` `0.51`. ([#2172](https://github.com/diffplug/spotless/issues/2172)) +### Added +* Added option `manageTrailingCommas` to `ktfmt`. ([#2177](https://github.com/diffplug/spotless/pull/2177)) ## [7.0.0.BETA1] - 2024-06-04 ### Added diff --git a/plugin-gradle/README.md b/plugin-gradle/README.md index e10a333e8c..0696ced59e 100644 --- a/plugin-gradle/README.md +++ b/plugin-gradle/README.md @@ -399,6 +399,7 @@ spotless { it.setBlockIndent(4) it.setContinuationIndent(4) it.setRemoveUnusedImports(false) + it.setManageTrailingCommas(false) } } } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java index c2293ab76d..ff39cea174 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java @@ -59,6 +59,7 @@ void integrationKtfmtDropboxStyleWithPublicApi() throws IOException { " it.setBlockIndent(4)", " it.setContinuationIndent(4)", " it.setRemoveUnusedImports(false)", + " it.setManageTrailingCommas(false)", " }", " }", "}"); diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index d5b511d4c3..756afea7e7 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -9,6 +9,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Renamed property `ktfmt` option `removeUnusedImport` -> `removeUnusedImports` to match `ktfmt`. ([#2172](https://github.com/diffplug/spotless/pull/2172)) ### Fixed * Fix compatibility issue introduced by `ktfmt` `0.51`. ([#2172](https://github.com/diffplug/spotless/issues/2172)) +### Added +* Added option `manageTrailingCommas` to `ktfmt`. ([#2177](https://github.com/diffplug/spotless/pull/2177)) ## [2.44.0.BETA1] - 2024-06-04 ### Added diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 5ba1ae7db8..9404792220 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -402,6 +402,7 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T 4 8 false + true ``` diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktfmt.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktfmt.java index 7bd420f1ee..4c5c4628d3 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktfmt.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktfmt.java @@ -44,11 +44,14 @@ public class Ktfmt implements FormatterStepFactory { @Parameter private Boolean removeUnusedImports; + @Parameter + private Boolean manageTrailingCommas; + @Override public FormatterStep newFormatterStep(FormatterStepConfig config) { String version = this.version != null ? this.version : KtfmtStep.defaultVersion(); Style style = this.style != null ? Style.valueOf(this.style) : null; - KtfmtFormattingOptions options = new KtfmtFormattingOptions(maxWidth, blockIndent, continuationIndent, removeUnusedImports); + KtfmtFormattingOptions options = new KtfmtFormattingOptions(maxWidth, blockIndent, continuationIndent, removeUnusedImports, manageTrailingCommas); return KtfmtStep.create(version, config.getProvisioner(), style, options); } } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtfmtTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtfmtTest.java index 48b43bfcb1..68d99c8186 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtfmtTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtfmtTest.java @@ -71,4 +71,13 @@ void testKtfmtStyleWithMaxWidthOption() throws Exception { mavenRunner().withArguments("spotless:apply").runNoError(); assertFile("src/main/kotlin/main.kt").sameAsResource("kotlin/ktfmt/max-width-dropbox.clean"); } + + @Test + void testKtfmtWithManageTrailingCommasOption() throws Exception { + writePomWithKotlinSteps("0.49true"); + + setFile("src/main/kotlin/main.kt").toResource("kotlin/ktfmt/trailing-commas.dirty"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile("src/main/kotlin/main.kt").sameAsResource("kotlin/ktfmt/trailing-commas.clean"); + } } diff --git a/testlib/src/main/resources/kotlin/ktfmt/trailing-commas.clean b/testlib/src/main/resources/kotlin/ktfmt/trailing-commas.clean new file mode 100644 index 0000000000..4de079c2c7 --- /dev/null +++ b/testlib/src/main/resources/kotlin/ktfmt/trailing-commas.clean @@ -0,0 +1,11 @@ +import a.* + +fun foo( + paramA: String, + paramB: Int, + anotherParamIsHere: Float, + myLongParamNameIsHere: CustomType, + lastParam: Boolean, +) = Unit + +fun bar(myLongParamNameIsHereButDoesNotWrap: Int) = Unit \ No newline at end of file diff --git a/testlib/src/main/resources/kotlin/ktfmt/trailing-commas.dirty b/testlib/src/main/resources/kotlin/ktfmt/trailing-commas.dirty new file mode 100644 index 0000000000..ea6e7b2de2 --- /dev/null +++ b/testlib/src/main/resources/kotlin/ktfmt/trailing-commas.dirty @@ -0,0 +1,8 @@ +import a.* + +fun foo(paramA: String,paramB : Int , anotherParamIsHere: Float ,myLongParamNameIsHere: CustomType +,lastParam: Boolean ) = Unit + +fun bar( + myLongParamNameIsHereButDoesNotWrap: Int, +) = Unit \ No newline at end of file diff --git a/testlib/src/test/java/com/diffplug/spotless/kotlin/KtfmtStepTest.java b/testlib/src/test/java/com/diffplug/spotless/kotlin/KtfmtStepTest.java index f4f6e0b651..1c9596e9e3 100644 --- a/testlib/src/test/java/com/diffplug/spotless/kotlin/KtfmtStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/kotlin/KtfmtStepTest.java @@ -62,6 +62,14 @@ void dropboxStyle_0_50() throws Exception { StepHarness.forStep(step).testResource("kotlin/ktfmt/basic.dirty", "kotlin/ktfmt/basic-dropboxstyle.clean"); } + @Test + void behaviorWithTrailingCommas() throws Exception { + KtfmtStep.KtfmtFormattingOptions options = new KtfmtStep.KtfmtFormattingOptions(); + options.setManageTrailingCommas(true); + FormatterStep step = KtfmtStep.create("0.49", TestProvisioner.mavenCentral(), KtfmtStep.Style.GOOGLE, options); + StepHarness.forStep(step).testResource("kotlin/ktfmt/trailing-commas.dirty", "kotlin/ktfmt/trailing-commas.clean"); + } + @Test void equality() throws Exception { new SerializableEqualityTester() {