Skip to content

Commit 43e4e97

Browse files
authoredJul 17, 2020
Merge pull request #648 from franvis/replace_duplicated_ktfmt_logic
Update ktfmt version and replace duplicated formatting options logic with new public API for it
2 parents 148ccd2 + 0df5561 commit 43e4e97

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed
 

‎CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
* 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))
1314

1415
## [2.2.0] - 2020-07-13
1516
### Added

‎lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java

+15-13
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,33 @@
2626

2727
import com.diffplug.spotless.*;
2828

29-
/** Wraps up [ktfmt](https://github.com/facebookincubator/ktfmt) as a FormatterStep. */
29+
/**
30+
* Wraps up [ktfmt](https://github.com/facebookincubator/ktfmt) as a FormatterStep.
31+
*/
3032
public class KtfmtStep {
3133
// prevent direct instantiation
3234
private KtfmtStep() {}
3335

34-
private static final String DEFAULT_VERSION = "0.15";
36+
private static final String DEFAULT_VERSION = "0.16";
3537
static final String NAME = "ktfmt";
3638
static final String PACKAGE = "com.facebook";
3739
static final String MAVEN_COORDINATE = PACKAGE + ":ktfmt:";
3840

3941
/**
4042
* Used to allow dropbox style option through formatting options.
4143
*
42-
* @see <a href="https://github.com/facebookincubator/ktfmt/blob/bfd3f08059eace1562191d542d11c0f9dbd49332/core/src/main/java/com/facebook/ktfmt/Formatter.kt#L47-L73">ktfmt source</a>
44+
* @see <a href="https://github.com/facebookincubator/ktfmt/blob/38486b0fb2edcabeba5540fcb69c6f1fa336c331/core/src/main/java/com/facebook/ktfmt/Formatter.kt#L47-L80">ktfmt source</a>
4345
*/
4446
public enum Style {
4547
DEFAULT, DROPBOX
4648
}
4749

48-
private static final int MAX_WIDTH_LINE = 100;
49-
private static final int BLOCK_INDENT = 4;
50-
private static final int CONTINUATION_INDENT = 4;
50+
private static final String DROPBOX_STYLE_METHOD = "dropboxStyle";
5151

5252
/**
5353
* The <code>format</code> method is available in the link below.
5454
*
55-
* @see <a href="https://github.com/facebookincubator/ktfmt/blob/bfd3f08059eace1562191d542d11c0f9dbd49332/core/src/main/java/com/facebook/ktfmt/Formatter.kt#L75-L92">ktfmt source</a>
55+
* @see <a href="https://github.com/facebookincubator/ktfmt/blob/38486b0fb2edcabeba5540fcb69c6f1fa336c331/core/src/main/java/com/facebook/ktfmt/Formatter.kt#L82-L99">ktfmt source</a>
5656
*/
5757
static final String FORMATTER_METHOD = "format";
5858

@@ -102,15 +102,10 @@ FormatterFunc createFormat() throws Exception {
102102
return input -> {
103103
try {
104104
if (style == DROPBOX) {
105-
// we are duplicating the result of this parsing logic from ktfmt 0.15
106-
// https://github.com/facebookincubator/ktfmt/blob/59f7ad8d1fde08f3402a013571c9997316083ebf/core/src/main/java/com/facebook/ktfmt/ParsedArgs.kt#L37
107-
// if the code above changes in a future version, we will need to change this code
108105
Class<?> formattingOptionsClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions");
109-
Object formattingOptions = formattingOptionsClazz.getConstructor(
110-
int.class, int.class, int.class).newInstance(
111-
MAX_WIDTH_LINE, BLOCK_INDENT, CONTINUATION_INDENT);
112106
Method formatterMethod = formatterClazz.getMethod(FORMATTER_METHOD, formattingOptionsClazz,
113107
String.class);
108+
Object formattingOptions = getDropboxStyleFormattingOptions(classLoader);
114109
return (String) formatterMethod.invoke(formatterClazz, formattingOptions, input);
115110
} else {
116111
Method formatterMethod = formatterClazz.getMethod(FORMATTER_METHOD, String.class);
@@ -121,5 +116,12 @@ FormatterFunc createFormat() throws Exception {
121116
}
122117
};
123118
}
119+
120+
private Object getDropboxStyleFormattingOptions(ClassLoader classLoader) throws Exception {
121+
Class<?> formattingOptionsCompanionClazz = classLoader.loadClass(pkg + ".ktfmt.FormattingOptions$Companion");
122+
Object companion = formattingOptionsCompanionClazz.getConstructors()[0].newInstance((Object) null);
123+
Method formattingOptionsMethod = formattingOptionsCompanionClazz.getDeclaredMethod(DROPBOX_STYLE_METHOD);
124+
return formattingOptionsMethod.invoke(companion);
125+
}
124126
}
125127
}

‎plugin-gradle/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
* 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))
67

78
## [5.1.0] - 2020-07-13
89
### Added

‎plugin-gradle/src/test/java/com/diffplug/gradle/spotless/KotlinGradleExtensionTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void integration_ktfmt_with_dropbox_style() throws IOException {
147147
"repositories { mavenCentral() }",
148148
"spotless {",
149149
" kotlinGradle {",
150-
" ktfmt('0.15').dropboxStyle()",
150+
" ktfmt().dropboxStyle()",
151151
" }",
152152
"}");
153153
setFile("configuration.gradle.kts").toResource("kotlin/ktfmt/dropboxstyle.dirty");

‎plugin-maven/CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
44

55
## [Unreleased]
66
### Changes
7-
* Bump default ktfmt from 0.13 to 0.15 ([#641](https://github.com/diffplug/spotless/issues/641)).
7+
* Bump default ktfmt from 0.13 to 0.16 ([#642](https://github.com/diffplug/spotless/pull/648)).
88

99
## [2.0.1] - 2020-07-04
1010
### Fixed

0 commit comments

Comments
 (0)