Skip to content

Commit 8134fc3

Browse files
fix: add support for ktfmt's TrailingCommaManagementStrategy (WIP)
NOTE: also updated JVM version to 17
1 parent 9dfd5e5 commit 8134fc3

File tree

11 files changed

+176
-86
lines changed

11 files changed

+176
-86
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ artifactIdMaven=spotless-maven-plugin
2121
artifactIdGradle=spotless-plugin-gradle
2222

2323
# Build requirements
24-
VER_JAVA=11
24+
VER_JAVA=17
2525
VER_JSR_305=3.0.2
2626

2727
# Dependencies provided by Spotless plugin

gradle/java-publish.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ javadoc {
7777
//
7878
// Thus, no javadoc warnings.
7979
options.addStringOption('Xdoclint:none', '-quiet')
80-
options.addStringOption('source', '11')
80+
options.addStringOption('source', '17')
8181
// setup the header
8282
options.header javadocInfo
8383
// setup links

lib/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ dependencies {
104104
jacksonCompileOnly "com.fasterxml.jackson.core:jackson-databind:$VER_JACKSON"
105105
jacksonCompileOnly "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:$VER_JACKSON"
106106
// ktfmt
107-
ktfmtCompileOnly "com.facebook:ktfmt:0.53"
107+
ktfmtCompileOnly "com.facebook:ktfmt:0.58"
108108
ktfmtCompileOnly("com.google.googlejavaformat:google-java-format") {
109109
version {
110110
strictly '1.7' // for JDK 8 compatibility

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

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,23 @@ public String apply(@Nonnull String input) throws Exception {
5555
}
5656

5757
private FormattingOptions createFormattingOptions() throws Exception {
58-
FormattingOptions formattingOptions;
59-
switch (style) {
60-
case META:
61-
formattingOptions = Formatter.META_FORMAT;
62-
break;
63-
case GOOGLE:
64-
formattingOptions = Formatter.GOOGLE_FORMAT;
65-
break;
66-
case KOTLIN_LANG:
67-
formattingOptions = Formatter.KOTLINLANG_FORMAT;
68-
break;
69-
default:
70-
throw new IllegalStateException("Unknown formatting option " + style);
71-
}
58+
FormattingOptions formattingOptions = switch (style) {
59+
case META -> Formatter.META_FORMAT;
60+
case GOOGLE -> Formatter.GOOGLE_FORMAT;
61+
case KOTLIN_LANG -> Formatter.KOTLINLANG_FORMAT;
62+
default -> throw new IllegalStateException("Unknown formatting option " + style);
63+
};
7264

7365
if (ktfmtFormattingOptions != null) {
7466
formattingOptions = formattingOptions.copy(
75-
ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()),
76-
ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()),
77-
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getContinuationIndent()),
78-
ktfmtFormattingOptions.getManageTrailingCommas().orElse(formattingOptions.getManageTrailingCommas()),
79-
ktfmtFormattingOptions.getRemoveUnusedImports().orElse(formattingOptions.getRemoveUnusedImports()),
80-
formattingOptions.getDebuggingPrintOpsAfterFormatting());
67+
ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()),
68+
ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()),
69+
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getContinuationIndent()),
70+
ktfmtFormattingOptions.getTrailingCommaManagementStrategy()
71+
.map(KtfmtTrailingCommaManagementStrategy::toFormatterTrailingCommaManagementStrategy)
72+
.orElse(formattingOptions.getTrailingCommaManagementStrategy()),
73+
ktfmtFormattingOptions.getRemoveUnusedImports().orElse(formattingOptions.getRemoveUnusedImports()),
74+
formattingOptions.getDebuggingPrintOpsAfterFormatting());
8175
}
8276

8377
return formattingOptions;

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ public final class KtfmtFormattingOptions {
3535
private Boolean removeUnusedImports;
3636

3737
@Nullable
38-
private Boolean manageTrailingCommas;
38+
private KtfmtTrailingCommaManagementStrategy trailingCommaManagementStrategy;
3939

4040
public KtfmtFormattingOptions(
41-
@Nullable Integer maxWidth,
42-
@Nullable Integer blockIndent,
43-
@Nullable Integer continuationIndent,
44-
@Nullable Boolean removeUnusedImports,
45-
@Nullable Boolean manageTrailingCommas) {
41+
@Nullable Integer maxWidth,
42+
@Nullable Integer blockIndent,
43+
@Nullable Integer continuationIndent,
44+
@Nullable Boolean removeUnusedImports,
45+
@Nullable KtfmtTrailingCommaManagementStrategy trailingCommaManagementStrategy) {
4646
this.maxWidth = maxWidth;
4747
this.blockIndent = blockIndent;
4848
this.continuationIndent = continuationIndent;
4949
this.removeUnusedImports = removeUnusedImports;
50-
this.manageTrailingCommas = manageTrailingCommas;
50+
this.trailingCommaManagementStrategy = trailingCommaManagementStrategy;
5151
}
5252

5353
@Nonnull
@@ -71,8 +71,8 @@ public Optional<Boolean> getRemoveUnusedImports() {
7171
}
7272

7373
@Nonnull
74-
public Optional<Boolean> getManageTrailingCommas() {
75-
return Optional.ofNullable(manageTrailingCommas);
74+
public Optional<KtfmtTrailingCommaManagementStrategy> getTrailingCommaManagementStrategy() {
75+
return Optional.ofNullable(trailingCommaManagementStrategy);
7676
}
7777

7878
public void setMaxWidth(int maxWidth) {
@@ -100,7 +100,7 @@ public void setRemoveUnusedImports(boolean removeUnusedImports) {
100100
this.removeUnusedImports = removeUnusedImports;
101101
}
102102

103-
public void setManageTrailingCommas(boolean manageTrailingCommas) {
104-
this.manageTrailingCommas = manageTrailingCommas;
103+
public void setTrailingCommaManagementStrategy(@Nullable KtfmtTrailingCommaManagementStrategy trailingCommaManagementStrategy) {
104+
this.trailingCommaManagementStrategy = trailingCommaManagementStrategy;
105105
}
106106
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.diffplug.spotless.glue.ktfmt;
2+
3+
import com.facebook.ktfmt.format.TrailingCommaManagementStrategy;
4+
5+
public enum KtfmtTrailingCommaManagementStrategy {
6+
NONE,
7+
ONLY_ADD,
8+
COMPLETE;
9+
10+
public TrailingCommaManagementStrategy toFormatterTrailingCommaManagementStrategy() {
11+
return switch (this) {
12+
case NONE -> TrailingCommaManagementStrategy.NONE;
13+
case ONLY_ADD -> TrailingCommaManagementStrategy.ONLY_ADD;
14+
case COMPLETE -> TrailingCommaManagementStrategy.COMPLETE;
15+
};
16+
}
17+
}

0 commit comments

Comments
 (0)