Skip to content

Commit

Permalink
Fix compatibility issue introduced by ktfmt 0.51 (#2172)
Browse files Browse the repository at this point in the history
Co-authored-by: Goooler <wangzongler@gmail.com>
  • Loading branch information
hick209 and Goooler committed Jun 18, 2024
1 parent 025d644 commit f6694ec
Show file tree
Hide file tree
Showing 15 changed files with 245 additions and 102 deletions.
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;
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());
}

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

0 comments on commit f6694ec

Please sign in to comment.