|
26 | 26 |
|
27 | 27 | import com.diffplug.spotless.*; |
28 | 28 |
|
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 | + */ |
30 | 32 | public class KtfmtStep { |
31 | 33 | // prevent direct instantiation |
32 | 34 | private KtfmtStep() {} |
33 | 35 |
|
34 | | - private static final String DEFAULT_VERSION = "0.15"; |
| 36 | + private static final String DEFAULT_VERSION = "0.16"; |
35 | 37 | static final String NAME = "ktfmt"; |
36 | 38 | static final String PACKAGE = "com.facebook"; |
37 | 39 | static final String MAVEN_COORDINATE = PACKAGE + ":ktfmt:"; |
38 | 40 |
|
39 | 41 | /** |
40 | 42 | * Used to allow dropbox style option through formatting options. |
41 | 43 | * |
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> |
43 | 45 | */ |
44 | 46 | public enum Style { |
45 | 47 | DEFAULT, DROPBOX |
46 | 48 | } |
47 | 49 |
|
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"; |
51 | 51 |
|
52 | 52 | /** |
53 | 53 | * The <code>format</code> method is available in the link below. |
54 | 54 | * |
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> |
56 | 56 | */ |
57 | 57 | static final String FORMATTER_METHOD = "format"; |
58 | 58 |
|
@@ -102,15 +102,10 @@ FormatterFunc createFormat() throws Exception { |
102 | 102 | return input -> { |
103 | 103 | try { |
104 | 104 | 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 |
108 | 105 | 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); |
112 | 106 | Method formatterMethod = formatterClazz.getMethod(FORMATTER_METHOD, formattingOptionsClazz, |
113 | 107 | String.class); |
| 108 | + Object formattingOptions = getDropboxStyleFormattingOptions(classLoader); |
114 | 109 | return (String) formatterMethod.invoke(formatterClazz, formattingOptions, input); |
115 | 110 | } else { |
116 | 111 | Method formatterMethod = formatterClazz.getMethod(FORMATTER_METHOD, String.class); |
@@ -121,5 +116,12 @@ FormatterFunc createFormat() throws Exception { |
121 | 116 | } |
122 | 117 | }; |
123 | 118 | } |
| 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 | + } |
124 | 126 | } |
125 | 127 | } |