diff --git a/CHANGES.md b/CHANGES.md index 5a9a8204d9..3f0cf6723e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,7 @@ 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] +* Bump default ktfmt from 0.15 to 0.16, and remove duplicated logic for the --dropbox-style option ([#642](https://github.com/diffplug/spotless/pull/648)) ## [2.2.0] - 2020-07-13 ### Added 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 d02ebf554b..59d034f4f5 100644 --- a/lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java +++ b/lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java @@ -26,12 +26,14 @@ import com.diffplug.spotless.*; -/** Wraps up [ktfmt](https://github.com/facebookincubator/ktfmt) as a FormatterStep. */ +/** + * Wraps up [ktfmt](https://github.com/facebookincubator/ktfmt) as a FormatterStep. + */ public class KtfmtStep { // prevent direct instantiation private KtfmtStep() {} - private static final String DEFAULT_VERSION = "0.15"; + private static final String DEFAULT_VERSION = "0.16"; static final String NAME = "ktfmt"; static final String PACKAGE = "com.facebook"; static final String MAVEN_COORDINATE = PACKAGE + ":ktfmt:"; @@ -39,20 +41,18 @@ private KtfmtStep() {} /** * Used to allow dropbox style option through formatting options. * - * @see ktfmt source + * @see ktfmt source */ public enum Style { DEFAULT, DROPBOX } - private static final int MAX_WIDTH_LINE = 100; - private static final int BLOCK_INDENT = 4; - private static final int CONTINUATION_INDENT = 4; + private static final String DROPBOX_STYLE_METHOD = "dropboxStyle"; /** * The format method is available in the link below. * - * @see ktfmt source + * @see ktfmt source */ static final String FORMATTER_METHOD = "format"; @@ -102,15 +102,10 @@ FormatterFunc createFormat() throws Exception { return input -> { try { if (style == DROPBOX) { - // we are duplicating the result of this parsing logic from ktfmt 0.15 - // https://github.com/facebookincubator/ktfmt/blob/59f7ad8d1fde08f3402a013571c9997316083ebf/core/src/main/java/com/facebook/ktfmt/ParsedArgs.kt#L37 - // if the code above changes in a future version, we will need to change this code Class formattingOptionsClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions"); - Object formattingOptions = formattingOptionsClazz.getConstructor( - int.class, int.class, int.class).newInstance( - MAX_WIDTH_LINE, BLOCK_INDENT, CONTINUATION_INDENT); Method formatterMethod = formatterClazz.getMethod(FORMATTER_METHOD, formattingOptionsClazz, String.class); + Object formattingOptions = getDropboxStyleFormattingOptions(classLoader); return (String) formatterMethod.invoke(formatterClazz, formattingOptions, input); } else { Method formatterMethod = formatterClazz.getMethod(FORMATTER_METHOD, String.class); @@ -121,5 +116,12 @@ FormatterFunc createFormat() throws Exception { } }; } + + private Object getDropboxStyleFormattingOptions(ClassLoader classLoader) throws Exception { + Class formattingOptionsCompanionClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions$Companion"); + Object companion = formattingOptionsCompanionClazz.getConstructors()[0].newInstance((Object) null); + Method formattingOptionsMethod = formattingOptionsCompanionClazz.getDeclaredMethod(DROPBOX_STYLE_METHOD); + return formattingOptionsMethod.invoke(companion); + } } } diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 33132ddb26..2deba8579a 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +* Bump default ktfmt from 0.15 to 0.16, and remove duplicated logic for the --dropbox-style option ([#642](https://github.com/diffplug/spotless/pull/648)) ## [5.1.0] - 2020-07-13 ### Added diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java index cecb698afa..7a48c08f6f 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java @@ -147,7 +147,7 @@ public void integration_ktfmt_with_dropbox_style() throws IOException { "repositories { mavenCentral() }", "spotless {", " kotlinGradle {", - " ktfmt('0.15').dropboxStyle()", + " ktfmt().dropboxStyle()", " }", "}"); setFile("configuration.gradle.kts").toResource("kotlin/ktfmt/dropboxstyle.dirty"); diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 840a19ef80..5b6c02fecb 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -4,7 +4,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Changes -* Bump default ktfmt from 0.13 to 0.15 ([#641](https://github.com/diffplug/spotless/issues/641)). +* Bump default ktfmt from 0.13 to 0.16 ([#642](https://github.com/diffplug/spotless/pull/648)). ## [2.0.1] - 2020-07-04 ### Fixed