Skip to content

Bump ktfmt 0.30 -> 0.31. #1118

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

Merged
merged 2 commits into from
Feb 9, 2022
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 @@ -10,6 +10,8 @@ This document is intended for Spotless developers.
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Changed
* Bump default ktfmt `0.30` -> `0.31` ([#1118](https://github.com/diffplug/spotless/pull/1118)).

## [2.22.1] - 2022-02-01
### Changed
Expand Down
34 changes: 26 additions & 8 deletions lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class KtfmtStep {
// prevent direct instantiation
private KtfmtStep() {}

private static final String DEFAULT_VERSION = "0.30";
private static final String DEFAULT_VERSION = "0.31";
static final String NAME = "ktfmt";
static final String PACKAGE = "com.facebook";
static final String MAVEN_COORDINATE = PACKAGE + ":ktfmt:";
Expand Down Expand Up @@ -120,18 +120,16 @@ static final class State implements Serializable {

FormatterFunc createFormat() throws Exception {
ClassLoader classLoader = jarState.getClassLoader();
Class<?> formatterClazz = classLoader.loadClass(pkg + ".ktfmt.FormatterKt");
return input -> {
try {
if (style == DEFAULT) {
Method formatterMethod = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
return (String) formatterMethod.invoke(formatterClazz, input);
Method formatterMethod = getFormatterClazz(classLoader).getMethod(FORMATTER_METHOD, String.class);
return (String) formatterMethod.invoke(getFormatterClazz(classLoader), input);
} else {
Class<?> formattingOptionsClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions");
Method formatterMethod = formatterClazz.getMethod(FORMATTER_METHOD, formattingOptionsClazz,
Method formatterMethod = getFormatterClazz(classLoader).getMethod(FORMATTER_METHOD, getFormattingOptionsClazz(classLoader),
String.class);
Object formattingOptions = getCustomFormattingOptions(classLoader, style);
return (String) formatterMethod.invoke(formatterClazz, formattingOptions, input);
return (String) formatterMethod.invoke(getFormatterClazz(classLoader), formattingOptions, input);
}
} catch (InvocationTargetException e) {
throw ThrowingEx.unwrapCause(e);
Expand All @@ -146,7 +144,7 @@ private Object getCustomFormattingOptions(ClassLoader classLoader, Style style)

try {
// ktfmt v0.19 and later
return classLoader.loadClass(pkg + ".ktfmt.FormatterKt").getField(style.getFormat()).get(null);
return getFormatterClazz(classLoader).getField(style.getFormat()).get(null);
} catch (NoSuchFieldException ignored) {}

// fallback to old, pre-0.19 ktfmt interface.
Expand All @@ -159,5 +157,25 @@ private Object getCustomFormattingOptions(ClassLoader classLoader, Style style)
throw new IllegalStateException("Versions pre-0.19 can only use Default and Dropbox styles");
}
}

private Class<?> getFormatterClazz(ClassLoader classLoader) throws Exception {
Class<?> formatterClazz;
if (BadSemver.version(version) >= BadSemver.version(0, 31)) {
formatterClazz = classLoader.loadClass(pkg + ".ktfmt.format.Formatter");
} else {
formatterClazz = classLoader.loadClass(pkg + ".ktfmt.FormatterKt");
}
return formatterClazz;
}

private Class<?> getFormattingOptionsClazz(ClassLoader classLoader) throws Exception {
Class<?> formattingOptionsClazz;
if (BadSemver.version(version) >= BadSemver.version(0, 31)) {
formattingOptionsClazz = classLoader.loadClass(pkg + ".ktfmt.format.FormattingOptions");
} else {
formattingOptionsClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions");
}
return formattingOptionsClazz;
}
}
}
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Changed
* Bump default ktfmt `0.30` -> `0.31` ([#1118](https://github.com/diffplug/spotless/pull/1118)).

## [6.2.1] - 2022-02-01
### Changed
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

## [Unreleased]
### Changed
* Bump default ktfmt `0.30` -> `0.31` ([#1118](https://github.com/diffplug/spotless/pull/1118)).

## [2.20.1] - 2022-02-01
* Bump default versions of formatters ([#1095](https://github.com/diffplug/spotless/pull/1095)).
Expand Down