Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to configure management of trailing commas in ktfmt #2177

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Added option `manageTrailingCommas` to `ktfmt`. ([#2177](https://github.com/diffplug/spotless/pull/2177))
* Added option `manageTrailingCommas` to `ktfmt`. ([#2177](https://github.com/diffplug/spotless/pull/2177))

Extra spaces was inserted, I'll fix them in a followup.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


## [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
@@ -1,5 +1,5 @@
/*
* Copyright 2022 DiffPlug
* Copyright 2022-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down 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.DROPBOX, options);
StepHarness.forStep(step).testResource("kotlin/ktfmt/trailing-commas.dirty", "kotlin/ktfmt/trailing-commas.clean");
}

@Test
void equality() throws Exception {
new SerializableEqualityTester() {
Expand Down
Loading