Skip to content

Commit ef436c0

Browse files
authored
Merge pull request #88 from jborgers/master
PR for #86, #87 and #89: Make Suppressed warnings and Processing errors visible and navigatable in the pmd results
2 parents a51bea8 + b4288dc commit ef436c0

27 files changed

+1074
-522
lines changed

src/com/intellij/plugins/bodhi/pmd/PMDConfigurationForm.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
package com.intellij.plugins.bodhi.pmd;
22

3+
import com.intellij.openapi.actionSystem.*;
34
import com.intellij.openapi.fileChooser.FileChooser;
45
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
5-
import com.intellij.openapi.vfs.VfsUtilCore;
6-
import com.intellij.openapi.vfs.VirtualFile;
76
import com.intellij.openapi.project.Project;
87
import com.intellij.openapi.ui.DialogBuilder;
98
import com.intellij.openapi.ui.DialogWrapper;
109
import com.intellij.openapi.util.IconLoader;
11-
import com.intellij.openapi.actionSystem.*;
12-
import com.intellij.util.PlatformIcons;
10+
import com.intellij.openapi.vfs.VfsUtilCore;
11+
import com.intellij.openapi.vfs.VirtualFile;
1312
import com.intellij.plugins.bodhi.pmd.core.PMDResultCollector;
1413
import com.intellij.ui.DocumentAdapter;
14+
import com.intellij.util.PlatformIcons;
1515

1616
import javax.swing.*;
17-
import javax.swing.event.*;
18-
import javax.swing.text.BadLocationException;
19-
import javax.swing.text.Document;
17+
import javax.swing.event.ChangeEvent;
18+
import javax.swing.event.ChangeListener;
19+
import javax.swing.event.DocumentEvent;
2020
import javax.swing.table.DefaultTableModel;
2121
import javax.swing.table.TableModel;
22-
import java.util.*;
23-
import java.util.List;
24-
import java.awt.event.ActionListener;
22+
import javax.swing.text.BadLocationException;
23+
import javax.swing.text.Document;
24+
import java.awt.*;
2525
import java.awt.event.ActionEvent;
26+
import java.awt.event.ActionListener;
2627
import java.awt.event.KeyEvent;
27-
import java.awt.*;
2828
import java.io.File;
29+
import java.util.List;
30+
import java.util.*;
2931

3032
/**
3133
* This class represents the UI for settings.
@@ -82,7 +84,7 @@ public JPanel getRootPanel() {
8284
* @param dataProjComp the data provider
8385
*/
8486
public void setDataOnUI(PMDProjectComponent dataProjComp) {
85-
ruleList.setModel(new MyListModel(dataProjComp.getCustomRuleSets()));
87+
ruleList.setModel(new MyListModel(dataProjComp.getCustomRuleSetPaths()));
8688
if (dataProjComp.getOptions().isEmpty()) {
8789
Object[][] dat = new Object[optionNames.length][2];
8890
for (int i = 0; i < optionNames.length; i++) {

src/com/intellij/plugins/bodhi/pmd/PMDInvoker.java

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,14 @@
1616
import com.intellij.openapi.wm.ToolWindow;
1717
import com.intellij.openapi.wm.ToolWindowManager;
1818
import com.intellij.plugins.bodhi.pmd.core.PMDResultCollector;
19-
import com.intellij.plugins.bodhi.pmd.tree.PMDRuleNode;
19+
import com.intellij.plugins.bodhi.pmd.tree.PMDBranchNode;
20+
import com.intellij.plugins.bodhi.pmd.tree.PMDRootNode;
2021

21-
import javax.swing.tree.DefaultMutableTreeNode;
2222
import java.io.File;
2323
import java.util.LinkedList;
2424
import java.util.List;
2525

26-
import static com.intellij.plugins.bodhi.pmd.filter.VirtualFileFilters.and;
27-
import static com.intellij.plugins.bodhi.pmd.filter.VirtualFileFilters.fileHasExtension;
28-
import static com.intellij.plugins.bodhi.pmd.filter.VirtualFileFilters.fileInSources;
29-
import static com.intellij.plugins.bodhi.pmd.filter.VirtualFileFilters.fileInTestSources;
30-
import static com.intellij.plugins.bodhi.pmd.filter.VirtualFileFilters.isDirectory;
31-
import static com.intellij.plugins.bodhi.pmd.filter.VirtualFileFilters.not;
32-
import static com.intellij.plugins.bodhi.pmd.filter.VirtualFileFilters.or;
26+
import static com.intellij.plugins.bodhi.pmd.filter.VirtualFileFilters.*;
3327

3428
/**
3529
* Invokes PMD using the PMDResultCollector and gets results from that. This acts as a
@@ -72,12 +66,12 @@ public static PMDInvoker getInstance() {
7266
* Runs PMD based on the given parameters, and populates result.
7367
*
7468
* @param actionEvent The action event that triggered run
75-
* @param rule The rule(s) to run
69+
* @param ruleSetPaths The ruleSetPath(s) for rules to run
7670
* @param isCustomRuleSet Is it a custom ruleset or not.
7771
*/
78-
public void runPMD(AnActionEvent actionEvent, String rule, boolean isCustomRuleSet) {
79-
//If no rule is selected, nothing to do
80-
if (rule == null || rule.length() == 0) {
72+
public void runPMD(AnActionEvent actionEvent, String ruleSetPaths, boolean isCustomRuleSet) {
73+
//If no ruleSetPath is selected, nothing to do
74+
if (ruleSetPaths == null || ruleSetPaths.length() == 0) {
8175
return;
8276
}
8377
//Show the tool window
@@ -86,7 +80,7 @@ public void runPMD(AnActionEvent actionEvent, String rule, boolean isCustomRuleS
8680
Project project = actionEvent.getData(PlatformDataKeys.PROJECT);
8781
PMDProjectComponent projectComponent = project.getComponent(PMDProjectComponent.class);
8882
PMDResultPanel resultPanel = projectComponent.getResultPanel();
89-
PMDRuleNode rootNodeData = ((PMDRuleNode) resultPanel.getRootNode().getUserObject());
83+
PMDRootNode rootNode = resultPanel.getRootNode();
9084

9185
List<File> files = new LinkedList<File>();
9286
if (actionEvent.getPlace().equals(ActionPlaces.PROJECT_VIEW_POPUP)
@@ -112,7 +106,7 @@ public void runPMD(AnActionEvent actionEvent, String rule, boolean isCustomRuleS
112106

113107
if (selectedFiles == null || selectedFiles.length == 0) {
114108
//toolWindow.displayErrorMessage("Please select a file to process first");
115-
rootNodeData.setFileCount(0);
109+
rootNode.setFileCount(0);
116110
return;
117111
}
118112
VirtualFileFilter filter = and(SUPPORTED_EXTENSIONS, fileInSources(project));
@@ -129,25 +123,25 @@ public void runPMD(AnActionEvent actionEvent, String rule, boolean isCustomRuleS
129123
VirtualFile[] selectedFiles = FileEditorManager.getInstance(project).getSelectedFiles();
130124
if (selectedFiles.length == 0) {
131125
//toolWindow.displayErrorMessage("Please select a file to process first");
132-
rootNodeData.setFileCount(0);
126+
rootNode.setFileCount(0);
133127
return;
134128
}
135129
files.add(new File(selectedFiles[0].getPresentableUrl()));
136130
}
137131

138132
//Got the files, start processing now
139-
processFiles(project, rule, files, isCustomRuleSet, projectComponent);
133+
processFiles(project, ruleSetPaths, files, isCustomRuleSet, projectComponent);
140134
}
141135

142136
/**
143137
* Runs PMD on given files.
144138
* @param project the project
145-
* @param rule The rule(s) to run
139+
* @param ruleSetPaths The ruleSetPath(s) of rules to run
146140
* @param files The files on which to run
147141
* @param isCustomRuleSet Is it a custom ruleset or not.
148142
* @param projectComponent
149143
*/
150-
public void processFiles(Project project, final String rule, final List<File> files, final boolean isCustomRuleSet, final PMDProjectComponent projectComponent) {
144+
public void processFiles(Project project, final String ruleSetPaths, final List<File> files, final boolean isCustomRuleSet, final PMDProjectComponent projectComponent) {
151145
ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(PMDProjectComponent.TOOL_ID);
152146
toolWindow.activate(null);
153147

@@ -159,43 +153,45 @@ public void processFiles(Project project, final String rule, final List<File> fi
159153
public void run() {
160154
//Show a progress indicator.
161155
ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
162-
String[] rules = rule.split(RULE_DELIMITER);
156+
String[] ruleSetPathArray = ruleSetPaths.split(RULE_DELIMITER);
163157
PMDResultPanel resultPanel = projectComponent.getResultPanel();
164158

165-
PMDRuleNode rootNodeData = ((PMDRuleNode) resultPanel.getRootNode().getUserObject());
166-
PMDResultCollector.report = null;
167-
rootNodeData.setFileCount(files.size());
168-
rootNodeData.setRuleSetCount(rules.length);
169-
rootNodeData.setRunning(true);
170-
for (int i = 0; i < rules.length; i++) {
159+
PMDRootNode rootNode = resultPanel.getRootNode();
160+
PMDResultCollector.clearReport();
161+
rootNode.setFileCount(files.size());
162+
rootNode.setRuleSetCount(ruleSetPathArray.length);
163+
rootNode.setRunning(true);
164+
for (String ruleSetPath : ruleSetPathArray) {
171165
//TODO: even better progress
172-
progress.setText("Running : " + rules[i] + " on " + files.size() + " file(s)");
166+
progress.setText("Running : " + ruleSetPath + " on " + files.size() + " file(s)");
173167

174168
//Create a result collector to get results
175169
PMDResultCollector collector = new PMDResultCollector();
176170

177171
//Get the tree nodes from result collector
178-
List<DefaultMutableTreeNode> results = collector.getResults(files, rules[i], projectComponent);
172+
List<PMDBranchNode> resultRuleNodes = collector.runPMDAndGetResults(files, ruleSetPath, projectComponent);
179173

180-
if (results.size() != 0) {
174+
if (resultRuleNodes.size() != 0) {
175+
String ruleSetName;
181176
if (isCustomRuleSet) {
182177
//For custom rulesets, using a separate format for rendering
183-
rules[i] = PMDUtil.getBareFileNameFromPath(rules[i]) + ";" + rules[i];
178+
ruleSetName = PMDUtil.getBareFileNameFromPath(ruleSetPath) + ";" + ruleSetPath;
184179
} else {
185-
rules[i] = PMDUtil.getBareFileNameFromPath(rules[i]);
180+
ruleSetName = PMDUtil.getBareFileNameFromPath(ruleSetPath);
186181
}
187-
DefaultMutableTreeNode node = resultPanel.addNode(rules[i]);
188-
//Add all nodes to the tree
189-
int childCount = 0;
190-
for (DefaultMutableTreeNode pmdResult : results) {
191-
resultPanel.addNode(node, pmdResult);
192-
childCount += ((PMDRuleNode)pmdResult.getUserObject()).getViolationCount();
182+
PMDBranchNode ruleSetNode = resultPanel.addCreateBranchNodeAtRoot(ruleSetName);
183+
//Add all rule nodes to the tree
184+
for (PMDBranchNode resultRuleNode : resultRuleNodes) {
185+
resultPanel.addNode(ruleSetNode, resultRuleNode);
193186
}
194-
((PMDRuleNode)node.getUserObject()).addToViolationCount(childCount);
195-
rootNodeData.addToViolationCount(childCount);
187+
rootNode.calculateCounts();
188+
resultPanel.reloadResultTree();
196189
}
197190
}
198-
rootNodeData.setRunning(false);
191+
resultPanel.addProcessingErrorsNodeToRootIfHasAny(); // as last node
192+
rootNode.calculateCounts();
193+
rootNode.setRunning(false);
194+
resultPanel.reloadResultTree();
199195
}
200196
};
201197
ProgressManager.getInstance().runProcessWithProgressSynchronously(runnable, "Running PMD", true, project);

src/com/intellij/plugins/bodhi/pmd/PMDProjectComponent.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class PMDProjectComponent implements ProjectComponent, PersistentStateCom
5252
private Project currentProject;
5353
private PMDResultPanel resultPanel;
5454
private ToolWindow resultWindow;
55-
private String lastRunRules;
55+
private String lastRunRuleSetPaths;
5656
private boolean lastRunRulesCustom;
5757
private AnActionEvent lastRunActionEvent;
5858
private Set<String> customRuleSetPaths = new LinkedHashSet<>(); // avoid duplicates, maintain order
@@ -114,20 +114,20 @@ void updateCustomRulesMenu() {
114114
PMDCustom actionGroup = (PMDCustom) ActionManager.getInstance().getAction("PMDCustom");
115115
actionGroup.removeAll(); // start clean
116116
boolean hasDuplicate = hasDuplicateBareFileName(customRuleSetPaths);
117-
for (final String rulePath : customRuleSetPaths) {
117+
for (final String ruleSetPath : customRuleSetPaths) {
118118
try {
119-
RuleSet ruleSet = PMDResultCollector.loadRuleSet(rulePath);
119+
RuleSet ruleSet = PMDResultCollector.loadRuleSet(ruleSetPath);
120120
String ruleSetName = ruleSet.getName(); // from the xml
121-
String extFileName = PMDUtil.getExtendedFileNameFromPath(rulePath);
122-
String bareFileName = PMDUtil.getBareFileNameFromPath(rulePath);
121+
String extFileName = PMDUtil.getExtendedFileNameFromPath(ruleSetPath);
122+
String bareFileName = PMDUtil.getBareFileNameFromPath(ruleSetPath);
123123
String actionText = ruleSetName;
124124
if (!ruleSetName.equals(bareFileName) || hasDuplicate) {
125125
actionText += " (" + extFileName + ")";
126126
}
127127
AnAction action = new AnAction(actionText) {
128128
public void actionPerformed(AnActionEvent e) {
129-
PMDInvoker.getInstance().runPMD(e, rulePath, true);
130-
setLastRunActionAndRules(e, rulePath, true);
129+
PMDInvoker.getInstance().runPMD(e, ruleSetPath, true);
130+
setLastRunActionAndRules(e, ruleSetPath, true);
131131
}
132132
};
133133
actionGroup.add(action);
@@ -201,12 +201,12 @@ public Project getCurrentProject() {
201201
}
202202

203203
/**
204-
* Get the last run PMD rules on this project.
204+
* Get the last run PMD rule set paths on this project.
205205
*
206-
* @return the last run rules.
206+
* @return the last run rule set paths.
207207
*/
208-
public String getLastRunRules() {
209-
return lastRunRules;
208+
public String getLastRunRuleSetPaths() {
209+
return lastRunRuleSetPaths;
210210
}
211211

212212
/**
@@ -230,16 +230,16 @@ public AnActionEvent getLastRunAction() {
230230
* Set the last run action event and PMD rule(s). Multiple rules should be delimited by
231231
* PMDInvoker.RULE_DELIMITER.
232232
* @param lastActionEvent the last run action event
233-
* @param lastRunRules The last run rule name
233+
* @param lastRunRuleSetPaths The last run rule set paths
234234
* @param isCustom whether the last run rules are custom rules
235235
*/
236-
public void setLastRunActionAndRules(AnActionEvent lastActionEvent, String lastRunRules, boolean isCustom) {
237-
this.lastRunRules = lastRunRules;
236+
public void setLastRunActionAndRules(AnActionEvent lastActionEvent, String lastRunRuleSetPaths, boolean isCustom) {
237+
this.lastRunRuleSetPaths = lastRunRuleSetPaths;
238238
this.lastRunActionEvent = lastActionEvent;
239239
this.lastRunRulesCustom = isCustom;
240240
}
241241

242-
public List<String> getCustomRuleSets() {
242+
public List<String> getCustomRuleSetPaths() {
243243
return new ArrayList(customRuleSetPaths);
244244
}
245245

0 commit comments

Comments
 (0)