Skip to content

Commit 3bce98e

Browse files
authoredJun 18, 2024··
Add option to configure management of trailing commas in ktfmt (#2177)
1 parent f6694ec commit 3bce98e

File tree

14 files changed

+86
-15
lines changed

14 files changed

+86
-15
lines changed
 

‎CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1616
* Renamed property `ktfmt` option `removeUnusedImport` -> `removeUnusedImports` to match `ktfmt`. ([#2172](https://github.com/diffplug/spotless/pull/2172))
1717
### Fixed
1818
* Fix compatibility issue introduced by `ktfmt` `0.51`. ([#2172](https://github.com/diffplug/spotless/issues/2172))
19+
### Added
20+
* Added option `manageTrailingCommas` to `ktfmt`. ([#2177](https://github.com/diffplug/spotless/pull/2177))
1921

2022
## [3.0.0.BETA1] - 2024-06-04
2123
### Added

‎lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormatterFunc.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ private FormattingOptions createFormattingOptions() throws Exception {
7575
ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()),
7676
ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()),
7777
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getContinuationIndent()),
78-
formattingOptions.getManageTrailingCommas(),
79-
ktfmtFormattingOptions.getRemoveUnusedImport().orElse(formattingOptions.getRemoveUnusedImports()),
78+
ktfmtFormattingOptions.getManageTrailingCommas().orElse(formattingOptions.getManageTrailingCommas()),
79+
ktfmtFormattingOptions.getRemoveUnusedImports().orElse(formattingOptions.getRemoveUnusedImports()),
8080
formattingOptions.getDebuggingPrintOpsAfterFormatting());
8181
}
8282

‎lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormattingOptions.java

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 DiffPlug
2+
* Copyright 2022-2024 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,17 +32,22 @@ public final class KtfmtFormattingOptions {
3232
private Integer continuationIndent;
3333

3434
@Nullable
35-
private Boolean removeUnusedImport;
35+
private Boolean removeUnusedImports;
36+
37+
@Nullable
38+
private Boolean manageTrailingCommas;
3639

3740
public KtfmtFormattingOptions(
3841
@Nullable Integer maxWidth,
3942
@Nullable Integer blockIndent,
4043
@Nullable Integer continuationIndent,
41-
@Nullable Boolean removeUnusedImport) {
44+
@Nullable Boolean removeUnusedImports,
45+
@Nullable Boolean manageTrailingCommas) {
4246
this.maxWidth = maxWidth;
4347
this.blockIndent = blockIndent;
4448
this.continuationIndent = continuationIndent;
45-
this.removeUnusedImport = removeUnusedImport;
49+
this.removeUnusedImports = removeUnusedImports;
50+
this.manageTrailingCommas = manageTrailingCommas;
4651
}
4752

4853
@Nonnull
@@ -61,8 +66,13 @@ public Optional<Integer> getContinuationIndent() {
6166
}
6267

6368
@Nonnull
64-
public Optional<Boolean> getRemoveUnusedImport() {
65-
return Optional.ofNullable(removeUnusedImport);
69+
public Optional<Boolean> getRemoveUnusedImports() {
70+
return Optional.ofNullable(removeUnusedImports);
71+
}
72+
73+
@Nonnull
74+
public Optional<Boolean> getManageTrailingCommas() {
75+
return Optional.ofNullable(manageTrailingCommas);
6676
}
6777

6878
public void setMaxWidth(int maxWidth) {
@@ -86,7 +96,11 @@ public void setContinuationIndent(int continuationIndent) {
8696
this.continuationIndent = continuationIndent;
8797
}
8898

89-
public void setRemoveUnusedImport(boolean removeUnusedImport) {
90-
this.removeUnusedImport = removeUnusedImport;
99+
public void setRemoveUnusedImports(boolean removeUnusedImports) {
100+
this.removeUnusedImports = removeUnusedImports;
101+
}
102+
103+
public void setManageTrailingCommas(boolean manageTrailingCommas) {
104+
this.manageTrailingCommas = manageTrailingCommas;
91105
}
92106
}

‎lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,22 @@ public static class KtfmtFormattingOptions implements Serializable {
126126
@Nullable
127127
private Boolean removeUnusedImports = null;
128128

129+
@Nullable
130+
private Boolean manageTrailingCommas = null;
131+
129132
public KtfmtFormattingOptions() {}
130133

131134
public KtfmtFormattingOptions(
132135
@Nullable Integer maxWidth,
133136
@Nullable Integer blockIndent,
134137
@Nullable Integer continuationIndent,
135-
@Nullable Boolean removeUnusedImports) {
138+
@Nullable Boolean removeUnusedImports,
139+
@Nullable Boolean manageTrailingCommas) {
136140
this.maxWidth = maxWidth;
137141
this.blockIndent = blockIndent;
138142
this.continuationIndent = continuationIndent;
139143
this.removeUnusedImports = removeUnusedImports;
144+
this.manageTrailingCommas = manageTrailingCommas;
140145
}
141146

142147
public void setMaxWidth(int maxWidth) {
@@ -154,6 +159,10 @@ public void setContinuationIndent(int continuationIndent) {
154159
public void setRemoveUnusedImports(boolean removeUnusedImports) {
155160
this.removeUnusedImports = removeUnusedImports;
156161
}
162+
163+
public void setManageTrailingCommas(boolean manageTrailingCommas) {
164+
this.manageTrailingCommas = manageTrailingCommas;
165+
}
157166
}
158167

159168
/** Creates a step which formats everything - code, import order, and unused imports. */
@@ -228,9 +237,9 @@ FormatterFunc createFormat() throws Exception {
228237
}
229238

230239
final Constructor<?> optionsConstructor = ktfmtFormattingOptionsClass.getConstructor(
231-
Integer.class, Integer.class, Integer.class, Boolean.class);
240+
Integer.class, Integer.class, Integer.class, Boolean.class, Boolean.class);
232241
final Object ktfmtFormattingOptions = optionsConstructor.newInstance(
233-
options.maxWidth, options.blockIndent, options.continuationIndent, options.removeUnusedImports);
242+
options.maxWidth, options.blockIndent, options.continuationIndent, options.removeUnusedImports, options.manageTrailingCommas);
234243
if (style == null) {
235244
final Constructor<?> constructor = formatterFuncClass.getConstructor(ktfmtFormattingOptionsClass);
236245
return (FormatterFunc) constructor.newInstance(ktfmtFormattingOptions);
@@ -365,7 +374,7 @@ private Object getCustomFormattingOptions(Class<?> formatterClass) throws Except
365374
/* continuationIndent = */ Optional.ofNullable(options.continuationIndent).orElse((Integer) formattingOptionsClass.getMethod("getContinuationIndent").invoke(formattingOptions)),
366375
/* removeUnusedImports = */ Optional.ofNullable(options.removeUnusedImports).orElse((Boolean) formattingOptionsClass.getMethod("getRemoveUnusedImports").invoke(formattingOptions)),
367376
/* debuggingPrintOpsAfterFormatting = */ (Boolean) formattingOptionsClass.getMethod("getDebuggingPrintOpsAfterFormatting").invoke(formattingOptions),
368-
/* manageTrailingCommas */ (Boolean) formattingOptionsClass.getMethod("getManageTrailingCommas").invoke(formattingOptions));
377+
/* manageTrailingCommas */ Optional.ofNullable(options.manageTrailingCommas).orElse((Boolean) formattingOptionsClass.getMethod("getManageTrailingCommas").invoke(formattingOptions)));
369378
}
370379
}
371380

‎plugin-gradle/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
99
* Renamed property `ktfmt` option `removeUnusedImport` -> `removeUnusedImports` to match `ktfmt`. ([#2172](https://github.com/diffplug/spotless/pull/2172))
1010
### Fixed
1111
* Fix compatibility issue introduced by `ktfmt` `0.51`. ([#2172](https://github.com/diffplug/spotless/issues/2172))
12+
### Added
13+
* Added option `manageTrailingCommas` to `ktfmt`. ([#2177](https://github.com/diffplug/spotless/pull/2177))
1214

1315
## [7.0.0.BETA1] - 2024-06-04
1416
### Added

‎plugin-gradle/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ spotless {
399399
it.setBlockIndent(4)
400400
it.setContinuationIndent(4)
401401
it.setRemoveUnusedImports(false)
402+
it.setManageTrailingCommas(false)
402403
}
403404
}
404405
}

‎plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinExtensionTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ void integrationKtfmtDropboxStyleWithPublicApi() throws IOException {
5959
" it.setBlockIndent(4)",
6060
" it.setContinuationIndent(4)",
6161
" it.setRemoveUnusedImports(false)",
62+
" it.setManageTrailingCommas(false)",
6263
" }",
6364
" }",
6465
"}");

‎plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
99
* Renamed property `ktfmt` option `removeUnusedImport` -> `removeUnusedImports` to match `ktfmt`. ([#2172](https://github.com/diffplug/spotless/pull/2172))
1010
### Fixed
1111
* Fix compatibility issue introduced by `ktfmt` `0.51`. ([#2172](https://github.com/diffplug/spotless/issues/2172))
12+
### Added
13+
* Added option `manageTrailingCommas` to `ktfmt`. ([#2177](https://github.com/diffplug/spotless/pull/2177))
1214

1315
## [2.44.0.BETA1] - 2024-06-04
1416
### Added

‎plugin-maven/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T
402402
<blockIndent>4</blockIndent> <!-- optional -->
403403
<continuationIndent>8</continuationIndent> <!-- optional -->
404404
<removeUnusedImports>false</removeUnusedImports> <!-- optional -->
405+
<manageTrailingCommas>true</manageTrailingCommas> <!-- optional -->
405406
</ktfmt>
406407
```
407408

‎plugin-maven/src/main/java/com/diffplug/spotless/maven/kotlin/Ktfmt.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ public class Ktfmt implements FormatterStepFactory {
4444
@Parameter
4545
private Boolean removeUnusedImports;
4646

47+
@Parameter
48+
private Boolean manageTrailingCommas;
49+
4750
@Override
4851
public FormatterStep newFormatterStep(FormatterStepConfig config) {
4952
String version = this.version != null ? this.version : KtfmtStep.defaultVersion();
5053
Style style = this.style != null ? Style.valueOf(this.style) : null;
51-
KtfmtFormattingOptions options = new KtfmtFormattingOptions(maxWidth, blockIndent, continuationIndent, removeUnusedImports);
54+
KtfmtFormattingOptions options = new KtfmtFormattingOptions(maxWidth, blockIndent, continuationIndent, removeUnusedImports, manageTrailingCommas);
5255
return KtfmtStep.create(version, config.getProvisioner(), style, options);
5356
}
5457
}

‎plugin-maven/src/test/java/com/diffplug/spotless/maven/kotlin/KtfmtTest.java

+9
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,13 @@ void testKtfmtStyleWithMaxWidthOption() throws Exception {
7171
mavenRunner().withArguments("spotless:apply").runNoError();
7272
assertFile("src/main/kotlin/main.kt").sameAsResource("kotlin/ktfmt/max-width-dropbox.clean");
7373
}
74+
75+
@Test
76+
void testKtfmtWithManageTrailingCommasOption() throws Exception {
77+
writePomWithKotlinSteps("<ktfmt><version>0.49</version><style>DROPBOX</style><manageTrailingCommas>true</manageTrailingCommas></ktfmt>");
78+
79+
setFile("src/main/kotlin/main.kt").toResource("kotlin/ktfmt/trailing-commas.dirty");
80+
mavenRunner().withArguments("spotless:apply").runNoError();
81+
assertFile("src/main/kotlin/main.kt").sameAsResource("kotlin/ktfmt/trailing-commas.clean");
82+
}
7483
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import a.*
2+
3+
fun foo(
4+
paramA: String,
5+
paramB: Int,
6+
anotherParamIsHere: Float,
7+
myLongParamNameIsHere: CustomType,
8+
lastParam: Boolean,
9+
) = Unit
10+
11+
fun bar(myLongParamNameIsHereButDoesNotWrap: Int) = Unit
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import a.*
2+
3+
fun foo(paramA: String,paramB : Int , anotherParamIsHere: Float ,myLongParamNameIsHere: CustomType
4+
,lastParam: Boolean ) = Unit
5+
6+
fun bar(
7+
myLongParamNameIsHereButDoesNotWrap: Int,
8+
) = Unit

‎testlib/src/test/java/com/diffplug/spotless/kotlin/KtfmtStepTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ void dropboxStyle_0_50() throws Exception {
6262
StepHarness.forStep(step).testResource("kotlin/ktfmt/basic.dirty", "kotlin/ktfmt/basic-dropboxstyle.clean");
6363
}
6464

65+
@Test
66+
void behaviorWithTrailingCommas() throws Exception {
67+
KtfmtStep.KtfmtFormattingOptions options = new KtfmtStep.KtfmtFormattingOptions();
68+
options.setManageTrailingCommas(true);
69+
FormatterStep step = KtfmtStep.create("0.49", TestProvisioner.mavenCentral(), KtfmtStep.Style.DROPBOX, options);
70+
StepHarness.forStep(step).testResource("kotlin/ktfmt/trailing-commas.dirty", "kotlin/ktfmt/trailing-commas.clean");
71+
}
72+
6573
@Test
6674
void equality() throws Exception {
6775
new SerializableEqualityTester() {

0 commit comments

Comments
 (0)
Please sign in to comment.