Skip to content

Commit 8715815

Browse files
fix: add trailingCommaManagementStrategy support for maven plugin
1 parent 8cba0cc commit 8715815

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,12 @@ public KtfmtFormattingOptions(
171171
@Nullable Integer blockIndent,
172172
@Nullable Integer continuationIndent,
173173
@Nullable Boolean removeUnusedImports,
174-
@Nullable Boolean manageTrailingCommas) {
174+
@Nullable TrailingCommaManagementStrategy trailingCommaManagementStrategy) {
175175
this.maxWidth = maxWidth;
176176
this.blockIndent = blockIndent;
177177
this.continuationIndent = continuationIndent;
178178
this.removeUnusedImports = removeUnusedImports;
179+
this.trailingCommaManagementStrategy = trailingCommaManagementStrategy;
179180
}
180181

181182
public void setMaxWidth(int maxWidth) {
@@ -280,9 +281,12 @@ FormatterFunc createFormat() throws Exception {
280281
final Constructor<?> optionsConstructor = ktfmtFormattingOptionsClass.getConstructor(
281282
Integer.class, Integer.class, Integer.class, Boolean.class, ktfmtTrailingCommaManagmentStrategyClass);
282283

283-
// TODO: use Enum.valueOf like for ktfmt style for trailing comma management strategy value
284+
final Object ktfmtTrailingCommaManagementStrategy =
285+
options.trailingCommaManagementStrategy == null
286+
? null
287+
: Enum.valueOf((Class<? extends Enum>) ktfmtTrailingCommaManagmentStrategyClass, options.trailingCommaManagementStrategy.name());
284288
final Object ktfmtFormattingOptions = optionsConstructor.newInstance(
285-
options.maxWidth, options.blockIndent, options.continuationIndent, options.removeUnusedImports, options.trailingCommaManagementStrategy);
289+
options.maxWidth, options.blockIndent, options.continuationIndent, options.removeUnusedImports, ktfmtTrailingCommaManagementStrategy);
286290
if (style == null) {
287291
final Constructor<?> constructor = formatterFuncClass.getConstructor(ktfmtFormattingOptionsClass);
288292
return (FormatterFunc) constructor.newInstance(ktfmtFormattingOptions);
@@ -332,14 +336,14 @@ private void validateStyle() {
332336
*/
333337
private String getKtfmtStyleOption(Style style) {
334338
switch (style) {
335-
case META:
336-
return "META";
337-
case GOOGLE:
338-
return "GOOGLE";
339-
case KOTLINLANG:
340-
return "KOTLIN_LANG";
341-
default:
342-
throw new IllegalStateException("Unsupported style: " + style);
339+
case META:
340+
return "META";
341+
case GOOGLE:
342+
return "GOOGLE";
343+
case KOTLINLANG:
344+
return "KOTLIN_LANG";
345+
default:
346+
throw new IllegalStateException("Unsupported style: " + style);
343347
}
344348
}
345349
}

plugin-maven/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T
473473
<blockIndent>4</blockIndent> <!-- optional -->
474474
<continuationIndent>8</continuationIndent> <!-- optional -->
475475
<removeUnusedImports>false</removeUnusedImports> <!-- optional -->
476-
<manageTrailingCommas>true</manageTrailingCommas> <!-- optional -->
476+
<trailingCommaManagementStrategy>COMPLETE</trailingCommaManagementStrategy> <!-- optional -->
477477
</ktfmt>
478478
```
479479

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.diffplug.spotless.kotlin.KtfmtStep;
2222
import com.diffplug.spotless.kotlin.KtfmtStep.KtfmtFormattingOptions;
2323
import com.diffplug.spotless.kotlin.KtfmtStep.Style;
24+
import com.diffplug.spotless.kotlin.KtfmtStep.TrailingCommaManagementStrategy;
2425
import com.diffplug.spotless.maven.FormatterStepConfig;
2526
import com.diffplug.spotless.maven.FormatterStepFactory;
2627

@@ -45,13 +46,13 @@ public class Ktfmt implements FormatterStepFactory {
4546
private Boolean removeUnusedImports;
4647

4748
@Parameter
48-
private Boolean manageTrailingCommas;
49+
private TrailingCommaManagementStrategy trailingCommaManagementStrategy;
4950

5051
@Override
5152
public FormatterStep newFormatterStep(FormatterStepConfig config) {
5253
String version = this.version != null ? this.version : KtfmtStep.defaultVersion();
5354
Style style = this.style != null ? Style.valueOf(this.style) : null;
54-
KtfmtFormattingOptions options = new KtfmtFormattingOptions(maxWidth, blockIndent, continuationIndent, removeUnusedImports, manageTrailingCommas);
55+
KtfmtFormattingOptions options = new KtfmtFormattingOptions(maxWidth, blockIndent, continuationIndent, removeUnusedImports, trailingCommaManagementStrategy);
5556
return KtfmtStep.create(version, config.getProvisioner(), style, options);
5657
}
5758
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@ void testKtfmtStyleWithMaxWidthOption() throws Exception {
7474
}
7575

7676
@Test
77-
// TODO
7877
void testKtfmtWithManageTrailingCommasOption() throws Exception {
79-
writePomWithKotlinSteps("<ktfmt><version>0.49</version><style>DROPBOX</style><manageTrailingCommas>true</manageTrailingCommas></ktfmt>");
78+
writePomWithKotlinSteps("<ktfmt><version>0.49</version><style>DROPBOX</style><trailingCommaManagementStrategy>COMPLETE</trailingCommaManagementStrategy></ktfmt>");
8079

8180
setFile("src/main/kotlin/main.kt").toResource("kotlin/ktfmt/trailing-commas.dirty");
8281
mavenRunner().withArguments("spotless:apply").runNoError();
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
fun myFunction() {
22
val location =
33
restTemplate.postForLocation(
4-
"/v1/my-api", mapOf("name" to "some-name", "url" to "https://www.google.com"))
4+
"/v1/my-api",
5+
mapOf("name" to "some-name", "url" to "https://www.google.com"),
6+
)
57
return location
68
}

0 commit comments

Comments
 (0)