Skip to content

Commit

Permalink
Default FormatterFunc.lint now traps errors from the formatter itse…
Browse files Browse the repository at this point in the history
…lf, and tries to parse a line number from the error message.
  • Loading branch information
nedtwigg committed Jan 14, 2022
1 parent d4ec76b commit 7a1e72a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
6 changes: 2 additions & 4 deletions lib/src/main/java/com/diffplug/spotless/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ public String compute(String unix, File file) {
unix = LineEnding.toUnix(formatted);
}
} catch (Throwable e) {
String relativePath = rootDir.relativize(file.toPath()).toString();
exceptionPolicy.handleError(e, step, relativePath);
// we ignore exceptions in format because we collect them in lint
}
}
return unix;
Expand All @@ -192,8 +191,7 @@ public List<Lint> lint(String content, File file) {
totalLints.addAll(lints);
}
} catch (Throwable e) {
String relativePath = rootDir.relativize(file.toPath()).toString();
exceptionPolicy.handleError(e, step, relativePath);
totalLints.add(Lint.createFromThrowable(step, e));
}
}
if (totalLints.isEmpty()) {
Expand Down
8 changes: 6 additions & 2 deletions lib/src/main/java/com/diffplug/spotless/FormatterFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ default String apply(String unix, File file) throws Exception {
return apply(unix);
}

/** Calculates a list of lints against the given content. */
default List<Lint> lint(String content, File file) {
/**
* Calculates a list of lints against the given content.
* By default, that's just an throwables thrown by the lint.
*/
default List<Lint> lint(String content, File file) throws Exception {
apply(content, file);
return Collections.emptyList();
}

Expand Down
39 changes: 39 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/Lint.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,43 @@ public static void toFile(List<Lint> lints, File file) throws IOException {
byte[] content = toString(lints).getBytes(StandardCharsets.UTF_8);
Files.write(file.toPath(), content);
}

/** Attempts to parse a line number from the given exception. */
static Lint createFromThrowable(FormatterStep step, Throwable e) {
Throwable current = e;
while (current != null) {
String message = current.getMessage();
int lineNumber = lineNumberFor(message);
if (lineNumber != -1) {
return Lint.create(step.getName(), msgFrom(message), lineNumber);
}
current = current.getCause();
}
return Lint.create(step.getName(), ThrowingEx.stacktrace(e), 1);
}

private static int lineNumberFor(String message) {
if (message == null) {
return -1;
}
int firstColon = message.indexOf(':');
if (firstColon == -1) {
return -1;
}
String candidateNum = message.substring(0, firstColon);
try {
return Integer.parseInt(candidateNum);
} catch (NumberFormatException e) {
return -1;
}
}

private static String msgFrom(String message) {
for (int i = 0; i < message.length(); ++i) {
if (Character.isLetter(message.charAt(i))) {
return message.substring(i);
}
}
return "";
}
}
13 changes: 12 additions & 1 deletion lib/src/main/java/com/diffplug/spotless/ThrowingEx.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 DiffPlug
* Copyright 2016-2022 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,9 @@
*/
package com.diffplug.spotless;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
* Basic functional interfaces which throw exception, along with
* static helper methods for calling them.
Expand Down Expand Up @@ -142,4 +145,12 @@ public WrappedAsRuntimeException(Throwable e) {
super(e);
}
}

public static String stacktrace(Throwable e) {
StringWriter out = new StringWriter();
PrintWriter writer = new PrintWriter(out);
e.printStackTrace(writer);
writer.flush();
return out.toString();
}
}

0 comments on commit 7a1e72a

Please sign in to comment.