Skip to content

Commit 63ff3b3

Browse files
authored
Support ktfmt 0.47 or newer (#2045 closes #2043)
2 parents 224f8f9 + f41f12c commit 63ff3b3

File tree

7 files changed

+41
-15
lines changed

7 files changed

+41
-15
lines changed

CHANGES.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1313
### Added
1414
* `FileSignature.Promised` and `JarState.Promised` to facilitate round-trip serialization for the Gradle configuration cache. ([#1945](https://github.com/diffplug/spotless/pull/1945))
1515
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
16-
16+
### Changes
17+
* Bump default `ktfmt` version to latest `0.46` -> `0.47`. ([#2045](https://github.com/diffplug/spotless/pull/2045))
1718
### Removed
1819
* **BREAKING** Remove `JarState.getMavenCoordinate(String prefix)`. ([#1945](https://github.com/diffplug/spotless/pull/1945))
1920
* **BREAKING** Replace `PipeStepPair` with `FenceStep`. ([#1954](https://github.com/diffplug/spotless/pull/1954))

lib/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ dependencies {
101101
jacksonCompileOnly "com.fasterxml.jackson.core:jackson-databind:$VER_JACKSON"
102102
jacksonCompileOnly "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:$VER_JACKSON"
103103
// ktfmt
104-
ktfmtCompileOnly "com.facebook:ktfmt:0.46"
104+
ktfmtCompileOnly "com.facebook:ktfmt:0.47"
105105
ktfmtCompileOnly("com.google.googlejavaformat:google-java-format") {
106106
version {
107107
strictly '1.7' // for JDK 8 compatibility

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

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 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.
@@ -15,6 +15,8 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktfmt;
1717

18+
import java.lang.reflect.Method;
19+
1820
import javax.annotation.Nonnull;
1921
import javax.annotation.Nullable;
2022

@@ -54,7 +56,7 @@ public String apply(@Nonnull String input) throws Exception {
5456
return Formatter.format(createFormattingOptions(), input);
5557
}
5658

57-
private FormattingOptions createFormattingOptions() {
59+
private FormattingOptions createFormattingOptions() throws Exception {
5860
FormattingOptions formattingOptions;
5961
switch (style) {
6062
case DEFAULT:
@@ -74,13 +76,26 @@ private FormattingOptions createFormattingOptions() {
7476
}
7577

7678
if (ktfmtFormattingOptions != null) {
77-
formattingOptions = formattingOptions.copy(
78-
formattingOptions.getStyle(),
79-
ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()),
80-
ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()),
81-
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getContinuationIndent()),
82-
ktfmtFormattingOptions.getRemoveUnusedImport().orElse(formattingOptions.getRemoveUnusedImports()),
83-
formattingOptions.getDebuggingPrintOpsAfterFormatting());
79+
try {
80+
formattingOptions = formattingOptions.copy(
81+
formattingOptions.getStyle(),
82+
ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()),
83+
ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()),
84+
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getContinuationIndent()),
85+
ktfmtFormattingOptions.getRemoveUnusedImport().orElse(formattingOptions.getRemoveUnusedImports()),
86+
formattingOptions.getDebuggingPrintOpsAfterFormatting(),
87+
formattingOptions.getManageTrailingCommas());
88+
} catch (NoSuchMethodError e) {
89+
//noinspection JavaReflectionMemberAccess, ABI change from ktfmt 0.47
90+
Method copyMethod = formattingOptions.getClass().getMethod("copy", FormattingOptions.Style.class, int.class, int.class, int.class, boolean.class, boolean.class);
91+
formattingOptions = (FormattingOptions) copyMethod.invoke(formattingOptions,
92+
formattingOptions.getStyle(),
93+
ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()),
94+
ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()),
95+
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getContinuationIndent()),
96+
ktfmtFormattingOptions.getRemoveUnusedImport().orElse(formattingOptions.getRemoveUnusedImports()),
97+
formattingOptions.getDebuggingPrintOpsAfterFormatting());
98+
}
8499
}
85100

86101
return formattingOptions;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838
public class KtfmtStep implements RoundedStep {
3939
private static final long serialVersionUID = 1L;
40-
private static final String DEFAULT_VERSION = "0.46";
40+
private static final String DEFAULT_VERSION = "0.47";
4141
private static final String NAME = "ktfmt";
4242
private static final String MAVEN_COORDINATE = "com.facebook:ktfmt:";
4343

plugin-gradle/CHANGES.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Fixed
77
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))
8-
8+
### Changes
9+
* Bump default `ktfmt` version to latest `0.46` -> `0.47`. ([#2045](https://github.com/diffplug/spotless/pull/2045))
910
### Added
1011
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
1112

plugin-maven/CHANGES.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Fixed
77
* Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990))
8-
8+
### Changes
9+
* Bump default `ktfmt` version to latest `0.46` -> `0.47`. ([#2045](https://github.com/diffplug/spotless/pull/2045))
910
### Added
1011
* Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031))
1112

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 DiffPlug
2+
* Copyright 2016-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.
@@ -26,6 +26,14 @@ void behavior() throws Exception {
2626
StepHarness.forStep(step).testResource("kotlin/ktfmt/basic.dirty", "kotlin/ktfmt/basic.clean");
2727
}
2828

29+
@Test
30+
void behaviorWithOptions() {
31+
KtfmtStep.KtfmtFormattingOptions options = new KtfmtStep.KtfmtFormattingOptions();
32+
options.setMaxWidth(100);
33+
FormatterStep step = KtfmtStep.create(KtfmtStep.defaultVersion(), TestProvisioner.mavenCentral(), KtfmtStep.Style.GOOGLE, options);
34+
StepHarness.forStep(step).testResource("kotlin/ktfmt/basic.dirty", "kotlin/ktfmt/basic.clean");
35+
}
36+
2937
@Test
3038
void dropboxStyle_0_18() throws Exception {
3139
FormatterStep step = KtfmtStep.create("0.18", TestProvisioner.mavenCentral(), KtfmtStep.Style.DROPBOX, null);

0 commit comments

Comments
 (0)