Skip to content

Commit cb16b8f

Browse files
author
Nelson Osacky
committed
Add line and column numbers to error messages in ktlint.
Fixes #249
1 parent c346971 commit cb16b8f

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Diff for: lib/src/main/java/com/diffplug/spotless/kotlin/KtLintStep.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,17 @@ FormatterFunc createFormat() throws Exception {
101101
Class<?> function2Interface = classLoader.loadClass("kotlin.jvm.functions.Function2");
102102
Class<?> lintErrorClass = classLoader.loadClass("com.github.shyiko.ktlint.core.LintError");
103103
Method detailGetter = lintErrorClass.getMethod("getDetail");
104+
Method lineGetter = lintErrorClass.getMethod("getLine");
105+
Method colGetter = lintErrorClass.getMethod("getCol");
104106
Object formatterCallback = Proxy.newProxyInstance(classLoader, new Class[]{function2Interface},
105107
(proxy, method, args) -> {
106108
Object lintError = args[0]; // com.github.shyiko.ktlint.core.LintError
107109
boolean corrected = (Boolean) args[1];
108110
if (!corrected) {
109111
String detail = (String) detailGetter.invoke(lintError);
110-
throw new AssertionError(detail);
112+
int line = (Integer) lineGetter.invoke(lintError);
113+
int col = (Integer) colGetter.invoke(lintError);
114+
throw new AssertionError("Error on line: " + line + ", column: " + col + "\n" + detail);
111115
}
112116
return null;
113117
});

Diff for: testlib/src/test/java/com/diffplug/spotless/kotlin/KtLintStepTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public void behavior() throws Exception {
3333
.testResource("kotlin/ktlint/basic.dirty", "kotlin/ktlint/basic.clean")
3434
.testException("kotlin/ktlint/unsolvable.dirty", assertion -> {
3535
assertion.isInstanceOf(AssertionError.class);
36-
assertion.hasMessage("Wildcard import");
36+
assertion.hasMessage("Error on line: 1, column: 1\n" +
37+
"Wildcard import");
3738
});
3839
}
3940

0 commit comments

Comments
 (0)