Skip to content

Commit f6694ec

Browse files
hick209Goooler
andauthored
Fix compatibility issue introduced by ktfmt 0.51 (#2172)
Co-authored-by: Goooler <wangzongler@gmail.com>
1 parent 025d644 commit f6694ec

File tree

15 files changed

+245
-102
lines changed

15 files changed

+245
-102
lines changed

CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Changes
1414
* Bump default `ktlint` version to latest `1.2.1` -> `1.3.0`. ([#2165](https://github.com/diffplug/spotless/pull/2165))
15+
* Bump default `ktfmt` version to latest `0.49` -> `0.51`. ([#2172](https://github.com/diffplug/spotless/pull/2172))
16+
* Renamed property `ktfmt` option `removeUnusedImport` -> `removeUnusedImports` to match `ktfmt`. ([#2172](https://github.com/diffplug/spotless/pull/2172))
17+
### Fixed
18+
* Fix compatibility issue introduced by `ktfmt` `0.51`. ([#2172](https://github.com/diffplug/spotless/issues/2172))
1519

1620
## [3.0.0.BETA1] - 2024-06-04
1721
### Added

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.49"
104+
ktfmtCompileOnly "com.facebook:ktfmt:0.51"
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

+12-30
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package com.diffplug.spotless.glue.ktfmt;
1717

18-
import java.lang.reflect.Method;
19-
2018
import javax.annotation.Nonnull;
2119
import javax.annotation.Nullable;
2220

@@ -34,15 +32,15 @@ public final class KtfmtFormatterFunc implements FormatterFunc {
3432
private final KtfmtFormattingOptions ktfmtFormattingOptions;
3533

3634
public KtfmtFormatterFunc() {
37-
this(KtfmtStyle.DEFAULT, null);
35+
this(KtfmtStyle.META, null);
3836
}
3937

4038
public KtfmtFormatterFunc(@Nonnull KtfmtStyle style) {
4139
this(style, null);
4240
}
4341

4442
public KtfmtFormatterFunc(@Nullable KtfmtFormattingOptions ktfmtFormattingOptions) {
45-
this(KtfmtStyle.DEFAULT, ktfmtFormattingOptions);
43+
this(KtfmtStyle.META, ktfmtFormattingOptions);
4644
}
4745

4846
public KtfmtFormatterFunc(@Nonnull KtfmtStyle style, @Nullable KtfmtFormattingOptions ktfmtFormattingOptions) {
@@ -59,11 +57,8 @@ public String apply(@Nonnull String input) throws Exception {
5957
private FormattingOptions createFormattingOptions() throws Exception {
6058
FormattingOptions formattingOptions;
6159
switch (style) {
62-
case DEFAULT:
63-
formattingOptions = new FormattingOptions();
64-
break;
65-
case DROPBOX:
66-
formattingOptions = Formatter.DROPBOX_FORMAT;
60+
case META:
61+
formattingOptions = Formatter.META_FORMAT;
6762
break;
6863
case GOOGLE:
6964
formattingOptions = Formatter.GOOGLE_FORMAT;
@@ -72,30 +67,17 @@ private FormattingOptions createFormattingOptions() throws Exception {
7267
formattingOptions = Formatter.KOTLINLANG_FORMAT;
7368
break;
7469
default:
75-
throw new IllegalStateException("Unknown formatting option");
70+
throw new IllegalStateException("Unknown formatting option " + style);
7671
}
7772

7873
if (ktfmtFormattingOptions != null) {
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-
}
74+
formattingOptions = formattingOptions.copy(
75+
ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()),
76+
ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()),
77+
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getContinuationIndent()),
78+
formattingOptions.getManageTrailingCommas(),
79+
ktfmtFormattingOptions.getRemoveUnusedImport().orElse(formattingOptions.getRemoveUnusedImports()),
80+
formattingOptions.getDebuggingPrintOpsAfterFormatting());
9981
}
10082

10183
return formattingOptions;

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

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

1818
public enum KtfmtStyle {
19-
DEFAULT, DROPBOX, GOOGLE, KOTLIN_LANG
19+
META, GOOGLE, KOTLIN_LANG
2020
}

0 commit comments

Comments
 (0)