From 9d301b5ef6b7d1d45f045cd25efd77f2256a86be Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Fri, 9 Feb 2024 21:07:22 -0800 Subject: [PATCH] Added option to toggle the violation's line number in source file URI --- .../java/rife/bld/extension/PmdOperation.java | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/main/java/rife/bld/extension/PmdOperation.java b/src/main/java/rife/bld/extension/PmdOperation.java index a7aaf05..f55c13e 100644 --- a/src/main/java/rife/bld/extension/PmdOperation.java +++ b/src/main/java/rife/bld/extension/PmdOperation.java @@ -81,6 +81,10 @@ public class PmdOperation extends AbstractOperation { * The path of the ignore file */ Path ignoreFile_; + /** + * The include line number toggle. + */ + boolean includeLineNumber_ = true; /** * The incremental analysis toggle. */ @@ -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. + *

+ * While clicking on the URI works in IntelliJ IDEA, Visual Studio Code, etc.; it might not in terminal emulators. + *

+ * Default: {@code TRUE} + */ + public PmdOperation includeLineNumber(boolean includeLineNumber) { + includeLineNumber_ = includeLineNumber; + return this; + } + /** * Enables or disables incremental analysis. */ @@ -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? @@ -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 {