diff --git a/CHANGELOG.md b/CHANGELOG.md index ff25d6f..02003b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,9 @@ # PMDPlugin Changelog ## [Unreleased] +### Added +- Support for running PMD task in background + ## [1.8.27] ### Added - Support for idea 2022.x and 2023.x diff --git a/gradle.properties b/gradle.properties index 081970e..9ea63c7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ # pluginGroup = com.intellij.plugins.bodhi.pmd pluginName = PMDPlugin -pluginVersion = 1.8.27 +pluginVersion = 1.8.28 # See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html # for insight into build numbers and IntelliJ Platform versions. diff --git a/src/main/java/com/intellij/plugins/bodhi/pmd/core/PMDProgressRenderer.java b/src/main/java/com/intellij/plugins/bodhi/pmd/core/PMDProgressRenderer.java index 3864961..a9c59f2 100644 --- a/src/main/java/com/intellij/plugins/bodhi/pmd/core/PMDProgressRenderer.java +++ b/src/main/java/com/intellij/plugins/bodhi/pmd/core/PMDProgressRenderer.java @@ -24,10 +24,6 @@ public String defaultFileExtension() { return null; } - @Override - public void start() throws IOException { - } - @Override public void startFileAnalysis(DataSource dataSource) { processedFiles++; @@ -36,12 +32,19 @@ public void startFileAnalysis(DataSource dataSource) { } @Override - public void renderFileReport(Report report) throws IOException { + public void flush() { + } + + @Override + public void start() throws IOException { + } + @Override + public void renderFileReport(Report report) throws IOException { } @Override public void end() throws IOException { - } + } diff --git a/src/main/java/com/intellij/plugins/bodhi/pmd/core/PMDResultCollector.java b/src/main/java/com/intellij/plugins/bodhi/pmd/core/PMDResultCollector.java index d00b97d..5b6d624 100644 --- a/src/main/java/com/intellij/plugins/bodhi/pmd/core/PMDResultCollector.java +++ b/src/main/java/com/intellij/plugins/bodhi/pmd/core/PMDResultCollector.java @@ -57,7 +57,6 @@ public static Report getReport() { * * @param files The files to run PMD on * @param ruleSetPath The path of the ruleSet to run - * @param progressRenderer * @return list of results */ public List runPMDAndGetResults(List files, String ruleSetPath, PMDProjectComponent comp) { @@ -69,7 +68,6 @@ public List runPMDAndGetResults(List files, String ru * * @param files The files to run PMD on * @param ruleSetPath The path of the ruleSet to run - * @param progress Object to report progress to * @return list of results */ public List runPMDAndGetResults(List files, String ruleSetPath, PMDProjectComponent comp, PMDProgressRenderer progressRenderer) { @@ -92,7 +90,7 @@ public List runPMDAndGetResults(List files, String ru PMDJsonExportingRenderer exportingRenderer = addExportRenderer(options); if (exportingRenderer != null) renderers.add(exportingRenderer); - if (progressRenderer != null) renderers.add(progressRenderer ); + if (progressRenderer != null) renderers.add(progressRenderer); try (PmdAnalysis pmd = PmdAnalysis.create(pmdConfig)) { files.forEach(file -> pmd.files().addFile(file.toPath())); @@ -161,7 +159,7 @@ public static String isValidRuleSet(String path) { try { RuleSet rs = new RuleSetLoader().loadFromResource(path); - if (rs.getRules().size() != 0) { + if (!rs.getRules().isEmpty()) { pathToRuleSet.put(path, rs); return ""; } diff --git a/src/main/java/com/intellij/plugins/bodhi/pmd/core/UselessSuppressionsHelper.java b/src/main/java/com/intellij/plugins/bodhi/pmd/core/UselessSuppressionsHelper.java index d82518d..46420c1 100644 --- a/src/main/java/com/intellij/plugins/bodhi/pmd/core/UselessSuppressionsHelper.java +++ b/src/main/java/com/intellij/plugins/bodhi/pmd/core/UselessSuppressionsHelper.java @@ -34,9 +34,9 @@ * @author jborgers */ public class UselessSuppressionsHelper { - static final Pattern NEXT_METHOD_NAME_PATTERN = Pattern.compile("\\R*^\\s*[\\w\\s<>]+\\s+([\\w]+)\\("); - static final Pattern NEXT_FIELD_NAME_PATTERN = Pattern.compile("\\R*\\s*[\\w\\s<>,?]+\\s+([\\w]+)\\s*[=;]"); - static final Pattern COMMENT_PATTERN = Pattern.compile("(\\/\\/.*|\\/\\*(?s:.)*?\\*\\/)"); // Matches single-line and multi-line comments + static final Pattern NEXT_METHOD_NAME_PATTERN = Pattern.compile("\\R*^\\s*[\\w\\s<>]+\\s+(\\w+)\\("); + static final Pattern NEXT_FIELD_NAME_PATTERN = Pattern.compile("\\R*\\s*[\\w\\s<>,?]+\\s+(\\w+)\\s*[=;]"); + static final Pattern COMMENT_PATTERN = Pattern.compile("(//.*|/\\*(?s:.)*?\\*/)"); // Matches single-line and multi-line comments static final String NO_METHOD = ""; final Map> classMethodToRuleNameOfSuppressedViolationsMap = new HashMap<>(); final Map> classMethodToRuleNameOfViolationsMap = new HashMap<>(); @@ -55,7 +55,7 @@ public class UselessSuppressionsHelper { void storeRuleNameForMethod(Report.SuppressedViolation suppressed) { RuleViolation violation = suppressed.getRuleViolation(); - if (!violation.getMethodName().isEmpty()) { + if (violation.getMethodName() != null && !violation.getMethodName().isEmpty()) { // store for method String methodKey = violation.getPackageName() + "-" + violation.getClassName() + "-" + violation.getMethodName(); Set suppressedMethodRuleNames = classMethodToRuleNameOfSuppressedViolationsMap.get(methodKey); @@ -136,8 +136,6 @@ private void addNodeIfUseless(List uselessSuppressions, P uselessSuppressions.add(new PMDUselessSuppression(pmdViolation, annotatedRuleName)); } } - } else { - // cannot deal with other, non-pmd suppressions } } } @@ -179,18 +177,20 @@ ViolatingAnnotationHolder getAnnotationContext(PMDViolation annotationViolation) if (virtualFile != null) { ApplicationManager.getApplication().runReadAction(() -> { Document doc = FileDocumentManager.getInstance().getDocument(virtualFile); - int startOffset = doc.getLineStartOffset(annotationViolation.getBeginLine() - 1) + annotationViolation.getBeginColumn(); - int endOffset = doc.getLineStartOffset(annotationViolation.getEndLine() - 1) + annotationViolation.getEndColumn() - 1; - String violatingAnnotation = doc.getText(new TextRange(startOffset, endOffset)); - String methodName; - if (!annotationViolation.getMethodName().isEmpty()) { // an annotation inside a method - methodName = annotationViolation.getMethodName(); - } else { - int startAfter = doc.getLineStartOffset(annotationViolation.getEndLine()); - String after = doc.getText(new TextRange(startAfter, doc.getTextLength() - 1)); - methodName = findMethodName(after); + if (doc != null) { + int startOffset = doc.getLineStartOffset(annotationViolation.getBeginLine() - 1) + annotationViolation.getBeginColumn(); + int endOffset = doc.getLineStartOffset(annotationViolation.getEndLine() - 1) + annotationViolation.getEndColumn() - 1; + String violatingAnnotation = doc.getText(new TextRange(startOffset, endOffset)); + String methodName; + if (!annotationViolation.getMethodName().isEmpty()) { // an annotation inside a method + methodName = annotationViolation.getMethodName(); + } else { + int startAfter = doc.getLineStartOffset(annotationViolation.getEndLine()); + String after = doc.getText(new TextRange(startAfter, doc.getTextLength() - 1)); + methodName = findMethodName(after); + } + annotationContextResult = new ViolatingAnnotationHolder(violatingAnnotation, methodName); } - annotationContextResult = new ViolatingAnnotationHolder(violatingAnnotation, methodName); }); } return annotationContextResult;