From f41f12cddfdb53f40b7930eeee43de2d6e794194 Mon Sep 17 00:00:00 2001 From: Goooler Date: Sat, 17 Feb 2024 04:33:42 +0800 Subject: [PATCH] Support ktfmt 0.47 or newer --- CHANGES.md | 3 +- lib/build.gradle | 2 +- .../glue/ktfmt/KtfmtFormatterFunc.java | 33 ++++++++++++++----- .../diffplug/spotless/kotlin/KtfmtStep.java | 2 +- plugin-gradle/CHANGES.md | 3 +- plugin-maven/CHANGES.md | 3 +- .../spotless/kotlin/KtfmtStepTest.java | 10 +++++- 7 files changed, 41 insertions(+), 15 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index b0d80fd7ff..1b21b57858 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,7 +13,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Added * `FileSignature.Promised` and `JarState.Promised` to facilitate round-trip serialization for the Gradle configuration cache. ([#1945](https://github.com/diffplug/spotless/pull/1945)) * Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031)) - +### Changes +* Bump default `ktfmt` version to latest `0.46` -> `0.47`. ([#2045](https://github.com/diffplug/spotless/pull/2045)) ### Removed * **BREAKING** Remove `JarState.getMavenCoordinate(String prefix)`. ([#1945](https://github.com/diffplug/spotless/pull/1945)) * **BREAKING** Replace `PipeStepPair` with `FenceStep`. ([#1954](https://github.com/diffplug/spotless/pull/1954)) diff --git a/lib/build.gradle b/lib/build.gradle index 738e2975fc..8d313f123a 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -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.46" + ktfmtCompileOnly "com.facebook:ktfmt:0.47" ktfmtCompileOnly("com.google.googlejavaformat:google-java-format") { version { strictly '1.7' // for JDK 8 compatibility diff --git a/lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormatterFunc.java b/lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormatterFunc.java index 80f4a42fcd..1d191c05d5 100644 --- a/lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormatterFunc.java +++ b/lib/src/ktfmt/java/com/diffplug/spotless/glue/ktfmt/KtfmtFormatterFunc.java @@ -1,5 +1,5 @@ /* - * Copyright 2022-2023 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. @@ -15,6 +15,8 @@ */ package com.diffplug.spotless.glue.ktfmt; +import java.lang.reflect.Method; + import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -54,7 +56,7 @@ public String apply(@Nonnull String input) throws Exception { return Formatter.format(createFormattingOptions(), input); } - private FormattingOptions createFormattingOptions() { + private FormattingOptions createFormattingOptions() throws Exception { FormattingOptions formattingOptions; switch (style) { case DEFAULT: @@ -74,13 +76,26 @@ private FormattingOptions createFormattingOptions() { } if (ktfmtFormattingOptions != null) { - 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()); + 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()); + } } return formattingOptions; diff --git a/lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java b/lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java index 768cd335cf..4ffdc380b2 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java @@ -37,7 +37,7 @@ */ public class KtfmtStep implements RoundedStep { private static final long serialVersionUID = 1L; - private static final String DEFAULT_VERSION = "0.46"; + private static final String DEFAULT_VERSION = "0.47"; private static final String NAME = "ktfmt"; private static final String MAVEN_COORDINATE = "com.facebook:ktfmt:"; diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index da2466339c..a97304f59b 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -5,7 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed * Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990)) - +### Changes +* Bump default `ktfmt` version to latest `0.46` -> `0.47`. ([#2045](https://github.com/diffplug/spotless/pull/2045)) ### Added * Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031)) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 3e3cc975b3..95661d4ebe 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -5,7 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Fixed * Ignore system git config when running tests ([#1990](https://github.com/diffplug/spotless/issues/1990)) - +### Changes +* Bump default `ktfmt` version to latest `0.46` -> `0.47`. ([#2045](https://github.com/diffplug/spotless/pull/2045)) ### Added * Respect `.editorconfig` settings for formatting shell via `shfmt` ([#2031](https://github.com/diffplug/spotless/pull/2031)) diff --git a/testlib/src/test/java/com/diffplug/spotless/kotlin/KtfmtStepTest.java b/testlib/src/test/java/com/diffplug/spotless/kotlin/KtfmtStepTest.java index 72e0adb4e2..e9458c9d39 100644 --- a/testlib/src/test/java/com/diffplug/spotless/kotlin/KtfmtStepTest.java +++ b/testlib/src/test/java/com/diffplug/spotless/kotlin/KtfmtStepTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2023 DiffPlug + * Copyright 2016-2024 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,14 @@ void behavior() throws Exception { StepHarness.forStep(step).testResource("kotlin/ktfmt/basic.dirty", "kotlin/ktfmt/basic.clean"); } + @Test + void behaviorWithOptions() { + KtfmtStep.KtfmtFormattingOptions options = new KtfmtStep.KtfmtFormattingOptions(); + options.setMaxWidth(100); + FormatterStep step = KtfmtStep.create(KtfmtStep.defaultVersion(), TestProvisioner.mavenCentral(), KtfmtStep.Style.GOOGLE, options); + StepHarness.forStep(step).testResource("kotlin/ktfmt/basic.dirty", "kotlin/ktfmt/basic.clean"); + } + @Test void dropboxStyle_0_18() throws Exception { FormatterStep step = KtfmtStep.create("0.18", TestProvisioner.mavenCentral(), KtfmtStep.Style.DROPBOX, null);