Skip to content

Commit

Permalink
Added option to toggle the violation's line number in source file URI
Browse files Browse the repository at this point in the history
  • Loading branch information
ethauvin committed Feb 10, 2024
1 parent 8218c4c commit 9d301b5
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/main/java/rife/bld/extension/PmdOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public class PmdOperation extends AbstractOperation<PmdOperation> {
* The path of the ignore file
*/
Path ignoreFile_;
/**
* The include line number toggle.
*/
boolean includeLineNumber_ = true;
/**
* The incremental analysis toggle.
*/
Expand Down Expand Up @@ -300,6 +304,18 @@ public PmdOperation ignoreFile(Path ignoreFile) {
return this;
}

/**
* Enables or disables including the line number for the beginning of the violation in the analyzed source file URI.
* <p>
* While clicking on the URI works in IntelliJ IDEA, Visual Studio Code, etc.; it might not in terminal emulators.
* <p>
* Default: {@code TRUE}
*/
public PmdOperation includeLineNumber(boolean includeLineNumber) {
includeLineNumber_ = includeLineNumber;
return this;
}

/**
* Enables or disables incremental analysis.
*/
Expand Down Expand Up @@ -399,14 +415,17 @@ public int performPmdAnalysis(String commandName, PMDConfiguration config) throw
}
var numErrors = report.getViolations().size();
if (numErrors > 0) {
var msg = String.format(
"[%s] %d rule violations were found. See the report at: %s", commandName, numErrors,
config.getReportFilePath().toUri());
for (var v : report.getViolations()) {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.log(Level.WARNING, "[{0}] {1}:{2}:\n\t{3} ({4})\n\t\t--> {5}",
final String msg;
if (includeLineNumber_) {
msg = "[{0}] {1}:{2}\n\t{3} ({4})\n\t\t--> {5}";
} else {
msg = "\"[{0}] {1} (line: {2})\\n\\t{3} ({4})\\n\\t\\t--> {5}\"";
}
LOGGER.log(Level.WARNING, msg,
new Object[]{commandName,
v.getFileId().getAbsolutePath(),
v.getFileId().getUriString(),
v.getBeginLine(),
v.getRule().getName(),
v.getRule().getExternalInfoUrl() //TODO bug in PMD?
Expand All @@ -415,11 +434,15 @@ public int performPmdAnalysis(String commandName, PMDConfiguration config) throw
v.getDescription()});
}
}

var violations = String.format(
"[%s] %d rule violations were found. See the report at: %s", commandName, numErrors,
config.getReportFilePath().toUri());
if (config.isFailOnViolation()) {
throw new RuntimeException(msg); // NOPMD
throw new RuntimeException(violations); // NOPMD
} else {
if (LOGGER.isLoggable(Level.WARNING)) {
LOGGER.warning(msg);
LOGGER.warning(violations);
}
}
} else {
Expand Down

0 comments on commit 9d301b5

Please sign in to comment.