|
17 | 17 |
|
18 | 18 | import java.io.IOException;
|
19 | 19 | import java.io.Serializable;
|
| 20 | +import java.lang.reflect.Constructor; |
20 | 21 | import java.lang.reflect.InvocationTargetException;
|
21 | 22 | import java.lang.reflect.Method;
|
22 | 23 | import java.lang.reflect.Proxy;
|
|
33 | 34 | import com.diffplug.spotless.Provisioner;
|
34 | 35 | import com.diffplug.spotless.ThrowingEx;
|
35 | 36 |
|
36 |
| -/** Wraps up [ktlint](https://github.com/shyiko/ktlint) as a FormatterStep. */ |
| 37 | +/** Wraps up [ktlint](https://github.com/pinterest/ktlint) as a FormatterStep. */ |
37 | 38 | public class KtLintStep {
|
38 | 39 | // prevent direct instantiation
|
39 | 40 | private KtLintStep() {}
|
40 | 41 |
|
41 |
| - private static final Pattern VERSION_PRE_0_32 = Pattern.compile("0\\.(\\d+)\\.\\d+"); |
42 |
| - private static final String DEFAULT_VERSION = "0.32.0"; |
| 42 | + private static final Pattern VERSION_MATCHER = Pattern.compile("0\\.(\\d+)\\.\\d+"); |
| 43 | + private static final String DEFAULT_VERSION = "0.34.2"; |
43 | 44 | static final String NAME = "ktlint";
|
44 | 45 | static final String PACKAGE_PRE_0_32 = "com.github.shyiko";
|
45 | 46 | static final String PACKAGE = "com.pinterest";
|
@@ -87,18 +88,21 @@ static final class State implements Serializable {
|
87 | 88 | /** The jar that contains the eclipse formatter. */
|
88 | 89 | final JarState jarState;
|
89 | 90 | private final TreeMap<String, String> userData;
|
| 91 | + private final boolean useParams; |
90 | 92 |
|
91 | 93 | State(String version, Provisioner provisioner, boolean isScript, Map<String, String> userData) throws IOException {
|
92 | 94 | this.userData = new TreeMap<>(userData);
|
93 | 95 | String coordinate;
|
94 |
| - Matcher matcher = VERSION_PRE_0_32.matcher(version); |
95 |
| - if (matcher.matches() && Integer.parseInt(matcher.group(1)) < 32) { |
| 96 | + Matcher matcher = VERSION_MATCHER.matcher(version); |
| 97 | + boolean matches = matcher.matches(); |
| 98 | + if (matches && Integer.parseInt(matcher.group(1)) < 32) { |
96 | 99 | coordinate = MAVEN_COORDINATE_PRE_0_32;
|
97 | 100 | this.pkg = PACKAGE_PRE_0_32;
|
98 | 101 | } else {
|
99 | 102 | coordinate = MAVEN_COORDINATE;
|
100 | 103 | this.pkg = PACKAGE;
|
101 | 104 | }
|
| 105 | + this.useParams = matches && Integer.parseInt(matcher.group(1)) >= 34; |
102 | 106 | this.jarState = JarState.from(coordinate + version, provisioner);
|
103 | 107 | this.isScript = isScript;
|
104 | 108 | }
|
@@ -135,18 +139,56 @@ FormatterFunc createFormat() throws Exception {
|
135 | 139 | // grab the KtLint singleton
|
136 | 140 | Class<?> ktlintClass = classLoader.loadClass(pkg + ".ktlint.core.KtLint");
|
137 | 141 | Object ktlint = ktlintClass.getDeclaredField("INSTANCE").get(null);
|
138 |
| - // and its format method |
139 |
| - String formatterMethodName = isScript ? "formatScript" : "format"; |
140 |
| - Method formatterMethod = ktlintClass.getMethod(formatterMethodName, String.class, Iterable.class, Map.class, function2Interface); |
141 |
| - |
142 |
| - return input -> { |
143 |
| - try { |
144 |
| - String formatted = (String) formatterMethod.invoke(ktlint, input, ruleSets, userData, formatterCallback); |
145 |
| - return formatted; |
146 |
| - } catch (InvocationTargetException e) { |
147 |
| - throw ThrowingEx.unwrapCause(e); |
148 |
| - } |
149 |
| - }; |
| 142 | + FormatterFunc formatterFunc; |
| 143 | + if (useParams) { |
| 144 | + // |
| 145 | + // In KtLint 0.34+ there is a new "format(params: Params)" function. We create an |
| 146 | + // instance of the Params class with our configuration and invoke it here. |
| 147 | + // |
| 148 | + |
| 149 | + // grab the Params class |
| 150 | + Class<?> paramsClass = classLoader.loadClass(pkg + ".ktlint.core.KtLint$Params"); |
| 151 | + // and its constructor |
| 152 | + Constructor<?> constructor = paramsClass.getConstructor( |
| 153 | + /* fileName, nullable */ String.class, |
| 154 | + /* text */ String.class, |
| 155 | + /* ruleSets */ Iterable.class, |
| 156 | + /* userData */ Map.class, |
| 157 | + /* callback */ function2Interface, |
| 158 | + /* script */ boolean.class, |
| 159 | + /* editorConfigPath, nullable */ String.class, |
| 160 | + /* debug */ boolean.class); |
| 161 | + Method formatterMethod = ktlintClass.getMethod("format", paramsClass); |
| 162 | + formatterFunc = input -> { |
| 163 | + try { |
| 164 | + Object params = constructor.newInstance( |
| 165 | + /* fileName, nullable */ null, |
| 166 | + /* text */ input, |
| 167 | + /* ruleSets */ ruleSets, |
| 168 | + /* userData */ userData, |
| 169 | + /* callback */ formatterCallback, |
| 170 | + /* script */ isScript, |
| 171 | + /* editorConfigPath, nullable */ null, |
| 172 | + /* debug */ false); |
| 173 | + return (String) formatterMethod.invoke(ktlint, params); |
| 174 | + } catch (InvocationTargetException e) { |
| 175 | + throw ThrowingEx.unwrapCause(e); |
| 176 | + } |
| 177 | + }; |
| 178 | + } else { |
| 179 | + // and its format method |
| 180 | + String formatterMethodName = isScript ? "formatScript" : "format"; |
| 181 | + Method formatterMethod = ktlintClass.getMethod(formatterMethodName, String.class, Iterable.class, Map.class, function2Interface); |
| 182 | + formatterFunc = input -> { |
| 183 | + try { |
| 184 | + return (String) formatterMethod.invoke(ktlint, input, ruleSets, userData, formatterCallback); |
| 185 | + } catch (InvocationTargetException e) { |
| 186 | + throw ThrowingEx.unwrapCause(e); |
| 187 | + } |
| 188 | + }; |
| 189 | + } |
| 190 | + |
| 191 | + return formatterFunc; |
150 | 192 | }
|
151 | 193 | }
|
152 | 194 | }
|
0 commit comments