Skip to content

Commit

Permalink
[ktfmt] Add option to configure management of trailing commas
Browse files Browse the repository at this point in the history
  • Loading branch information
hick209 committed Jun 18, 2024
1 parent f6694ec commit 5fea517
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -61,8 +66,13 @@ public Optional<Integer> getContinuationIndent() {
}

@Nonnull
public Optional<Boolean> getRemoveUnusedImport() {
return Optional.ofNullable(removeUnusedImport);
public Optional<Boolean> getRemoveUnusedImports() {
return Optional.ofNullable(removeUnusedImports);
}

@Nonnull
public Optional<Boolean> getManageTrailingCommas() {
return Optional.ofNullable(manageTrailingCommas);
}

public void setMaxWidth(int maxWidth) {
Expand All @@ -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;
}
}
17 changes: 13 additions & 4 deletions lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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. */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)));
}
}

Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ spotless {
it.setBlockIndent(4)
it.setContinuationIndent(4)
it.setRemoveUnusedImports(false)
it.setManageTrailingCommas(false)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void integrationKtfmtDropboxStyleWithPublicApi() throws IOException {
" it.setBlockIndent(4)",
" it.setContinuationIndent(4)",
" it.setRemoveUnusedImports(false)",
" it.setManageTrailingCommas(false)",
" }",
" }",
"}");
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T
<blockIndent>4</blockIndent> <!-- optional -->
<continuationIndent>8</continuationIndent> <!-- optional -->
<removeUnusedImports>false</removeUnusedImports> <!-- optional -->
<manageTrailingCommas>true</manageTrailingCommas> <!-- optional -->
</ktfmt>
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("<ktfmt><version>0.49</version><style>DROPBOX</style><manageTrailingCommas>true</manageTrailingCommas></ktfmt>");

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");
}
}
11 changes: 11 additions & 0 deletions testlib/src/main/resources/kotlin/ktfmt/trailing-commas.clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import a.*

fun foo(
paramA: String,
paramB: Int,
anotherParamIsHere: Float,
myLongParamNameIsHere: CustomType,
lastParam: Boolean,
) = Unit

fun bar(myLongParamNameIsHereButDoesNotWrap: Int) = Unit
8 changes: 8 additions & 0 deletions testlib/src/main/resources/kotlin/ktfmt/trailing-commas.dirty
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import a.*

fun foo(paramA: String,paramB : Int , anotherParamIsHere: Float ,myLongParamNameIsHere: CustomType
,lastParam: Boolean ) = Unit

fun bar(
myLongParamNameIsHereButDoesNotWrap: Int,
) = Unit
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 5fea517

Please sign in to comment.