Skip to content

Commit

Permalink
Merge pull request #648 from franvis/replace_duplicated_ktfmt_logic
Browse files Browse the repository at this point in the history
Update ktfmt version and replace duplicated formatting options logic with new public API for it
  • Loading branch information
nedtwigg committed Jul 17, 2020
2 parents 148ccd2 + 0df5561 commit 43e4e97
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -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
Expand Down
28 changes: 15 additions & 13 deletions lib/src/main/java/com/diffplug/spotless/kotlin/KtfmtStep.java
Expand Up @@ -26,33 +26,33 @@

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:";

/**
* Used to allow dropbox style option through formatting options.
*
* @see <a href="https://github.com/facebookincubator/ktfmt/blob/bfd3f08059eace1562191d542d11c0f9dbd49332/core/src/main/java/com/facebook/ktfmt/Formatter.kt#L47-L73">ktfmt source</a>
* @see <a href="https://github.com/facebookincubator/ktfmt/blob/38486b0fb2edcabeba5540fcb69c6f1fa336c331/core/src/main/java/com/facebook/ktfmt/Formatter.kt#L47-L80">ktfmt source</a>
*/
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 <code>format</code> method is available in the link below.
*
* @see <a href="https://github.com/facebookincubator/ktfmt/blob/bfd3f08059eace1562191d542d11c0f9dbd49332/core/src/main/java/com/facebook/ktfmt/Formatter.kt#L75-L92">ktfmt source</a>
* @see <a href="https://github.com/facebookincubator/ktfmt/blob/38486b0fb2edcabeba5540fcb69c6f1fa336c331/core/src/main/java/com/facebook/ktfmt/Formatter.kt#L82-L99">ktfmt source</a>
*/
static final String FORMATTER_METHOD = "format";

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
}
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Expand Up @@ -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
Expand Down
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion plugin-maven/CHANGES.md
Expand Up @@ -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
Expand Down

0 comments on commit 43e4e97

Please sign in to comment.