Skip to content
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

Fix compatibility issue introduced by ktfmt 0.51 #2172

Merged
merged 7 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Changes
* Bump default `ktlint` version to latest `1.2.1` -> `1.3.0`. ([#2165](https://github.com/diffplug/spotless/pull/2165))
* Bump default `ktfmt` version to latest `0.49` -> `0.51`. ([#2172](https://github.com/diffplug/spotless/pull/2172))
* Renamed property `ktfmt` option `removeUnusedImport` -> `removeUnusedImports` to match `ktfmt`. ([#2172](https://github.com/diffplug/spotless/pull/2172))
### Fixed
* Fix compatibility issue introduced by `ktfmt` `0.51`. ([#2172](https://github.com/diffplug/spotless/issues/2172))

## [3.0.0.BETA1] - 2024-06-04
### Added
Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ dependencies {
jacksonCompileOnly "com.fasterxml.jackson.core:jackson-databind:$VER_JACKSON"
jacksonCompileOnly "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:$VER_JACKSON"
// ktfmt
ktfmtCompileOnly "com.facebook:ktfmt:0.49"
ktfmtCompileOnly "com.facebook:ktfmt:0.51"
ktfmtCompileOnly("com.google.googlejavaformat:google-java-format") {
version {
strictly '1.7' // for JDK 8 compatibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.diffplug.spotless.glue.ktfmt;

import java.lang.reflect.Method;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

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

public KtfmtFormatterFunc() {
this(KtfmtStyle.DEFAULT, null);
this(KtfmtStyle.META, null);
}

public KtfmtFormatterFunc(@Nonnull KtfmtStyle style) {
this(style, null);
}

public KtfmtFormatterFunc(@Nullable KtfmtFormattingOptions ktfmtFormattingOptions) {
this(KtfmtStyle.DEFAULT, ktfmtFormattingOptions);
this(KtfmtStyle.META, ktfmtFormattingOptions);
}

public KtfmtFormatterFunc(@Nonnull KtfmtStyle style, @Nullable KtfmtFormattingOptions ktfmtFormattingOptions) {
Expand All @@ -59,11 +57,8 @@ public String apply(@Nonnull String input) throws Exception {
private FormattingOptions createFormattingOptions() throws Exception {
FormattingOptions formattingOptions;
switch (style) {
case DEFAULT:
formattingOptions = new FormattingOptions();
break;
case DROPBOX:
formattingOptions = Formatter.DROPBOX_FORMAT;
case META:
formattingOptions = Formatter.META_FORMAT;
hick209 marked this conversation as resolved.
Show resolved Hide resolved
break;
case GOOGLE:
formattingOptions = Formatter.GOOGLE_FORMAT;
Expand All @@ -72,30 +67,17 @@ private FormattingOptions createFormattingOptions() throws Exception {
formattingOptions = Formatter.KOTLINLANG_FORMAT;
break;
default:
throw new IllegalStateException("Unknown formatting option");
throw new IllegalStateException("Unknown formatting option " + style);
}

if (ktfmtFormattingOptions != null) {
try {
formattingOptions = formattingOptions.copy(
formattingOptions.getStyle(),
ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()),
ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()),
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getContinuationIndent()),
ktfmtFormattingOptions.getRemoveUnusedImport().orElse(formattingOptions.getRemoveUnusedImports()),
formattingOptions.getDebuggingPrintOpsAfterFormatting(),
formattingOptions.getManageTrailingCommas());
} catch (NoSuchMethodError e) {
//noinspection JavaReflectionMemberAccess, ABI change from ktfmt 0.47
Method copyMethod = formattingOptions.getClass().getMethod("copy", FormattingOptions.Style.class, int.class, int.class, int.class, boolean.class, boolean.class);
formattingOptions = (FormattingOptions) copyMethod.invoke(formattingOptions,
formattingOptions.getStyle(),
ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()),
ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()),
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getContinuationIndent()),
ktfmtFormattingOptions.getRemoveUnusedImport().orElse(formattingOptions.getRemoveUnusedImports()),
formattingOptions.getDebuggingPrintOpsAfterFormatting());
}
formattingOptions = formattingOptions.copy(
ktfmtFormattingOptions.getMaxWidth().orElse(formattingOptions.getMaxWidth()),
ktfmtFormattingOptions.getBlockIndent().orElse(formattingOptions.getBlockIndent()),
ktfmtFormattingOptions.getContinuationIndent().orElse(formattingOptions.getContinuationIndent()),
formattingOptions.getManageTrailingCommas(),
ktfmtFormattingOptions.getRemoveUnusedImport().orElse(formattingOptions.getRemoveUnusedImports()),
formattingOptions.getDebuggingPrintOpsAfterFormatting());
hick209 marked this conversation as resolved.
Show resolved Hide resolved
}

return formattingOptions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 DiffPlug
* Copyright 2022-2024 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,5 +16,5 @@
package com.diffplug.spotless.glue.ktfmt;

public enum KtfmtStyle {
DEFAULT, DROPBOX, GOOGLE, KOTLIN_LANG
META, GOOGLE, KOTLIN_LANG
}
Loading