Skip to content

Commit

Permalink
Merge pull request #149 from martenbohlin/run-in-background
Browse files Browse the repository at this point in the history
Run in background
  • Loading branch information
amitdev authored Oct 14, 2023
2 parents c6ba828 + 54c15a7 commit f920dfd
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 10 deletions.
21 changes: 13 additions & 8 deletions src/main/java/com/intellij/plugins/bodhi/pmd/PMDInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.vcs.actions.VcsContextFactory;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileFilter;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.plugins.bodhi.pmd.core.PMDProgressRenderer;
import com.intellij.plugins.bodhi.pmd.core.PMDResultCollector;
import com.intellij.plugins.bodhi.pmd.tree.*;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.LinkedList;
Expand Down Expand Up @@ -152,8 +154,9 @@ public void processFiles(Project project, final String ruleSetPaths, final List<
ApplicationManager.getApplication().saveAll();

//Run PMD asynchronously
Runnable runnable = new Runnable() {
public void run() {
ProgressManager.getInstance().run(new Task.Backgroundable(project, "Running PMD", true) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
//Show a progress indicator.
ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
String[] ruleSetPathArray = ruleSetPaths.split(RULE_DELIMITER);
Expand All @@ -165,15 +168,15 @@ public void run() {
rootNode.setFileCount(files.size());
rootNode.setRuleSetCount(ruleSetPathArray.length);
rootNode.setRunning(true);
PMDProgressRenderer progressRenderer = new PMDProgressRenderer(progress, files.size() * ruleSetPathArray.length);
for (String ruleSetPath : ruleSetPathArray) {
//TODO: even better progress
progress.setText("Running : " + ruleSetPath + " on " + files.size() + " file(s)");

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

//Get the tree nodes from result collector
List<PMDRuleSetEntryNode> resultRuleNodes = collector.runPMDAndGetResults(files, ruleSetPath, projectComponent);
List<PMDRuleSetEntryNode> resultRuleNodes = collector.runPMDAndGetResults(files, ruleSetPath, projectComponent, progressRenderer);
// sort rules by priority, rule and suppressed nodes are comparable
resultRuleNodes.sort(null);

Expand All @@ -189,13 +192,15 @@ public void run() {
rootNode.calculateCounts();
resultPanel.reloadResultTree();
}
if (progress.isCanceled()) {
break;
}
}
resultPanel.addProcessingErrorsNodeToRootIfHasAny(); // as last node
rootNode.calculateCounts();
rootNode.setRunning(false);
resultPanel.reloadResultTree();
}
};
ProgressManager.getInstance().runProcessWithProgressSynchronously(runnable, "Running PMD", true, project);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.intellij.plugins.bodhi.pmd.core;

import com.intellij.openapi.progress.ProgressIndicator;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.renderers.AbstractRenderer;
import net.sourceforge.pmd.util.datasource.DataSource;

import java.io.IOException;

public class PMDProgressRenderer extends AbstractRenderer {
private final ProgressIndicator progress;
private final int totalFiles;
private int processedFiles = 0;

public PMDProgressRenderer(ProgressIndicator progress, int totalFiles) {
super("Progress", "Reports progress to IntelliJ");

this.progress = progress;
this.totalFiles = totalFiles;
}

@Override
public String defaultFileExtension() {
return null;
}

@Override
public void start() throws IOException {
}

@Override
public void startFileAnalysis(DataSource dataSource) {
processedFiles++;
progress.setFraction(processedFiles / (double) totalFiles);
progress.setText2(dataSource.getNiceFileName(true, null));
}

@Override
public void renderFileReport(Report report) throws IOException {

}

@Override
public void end() throws IOException {

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.intellij.plugins.bodhi.pmd.core;

import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.plugins.bodhi.pmd.PMDConfigurationForm;
import com.intellij.plugins.bodhi.pmd.PMDProjectComponent;
Expand Down Expand Up @@ -54,11 +55,24 @@ public static Report getReport() {
/**
* Runs the given ruleSet(s) on given set of files and returns the result.
*
* @param files The files to run PMD on
* @param ruleSetPath The path of the ruleSet to run
* @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<PMDRuleSetEntryNode> runPMDAndGetResults(List<File> files, String ruleSetPath, PMDProjectComponent comp) {
return this.runPMDAndGetResults(files, ruleSetPath, comp, null);
}

/**
* Runs the given ruleSet(s) on given set of files and returns the result.
*
* @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<PMDRuleSetEntryNode> runPMDAndGetResults(List<File> files, String ruleSetPath, PMDProjectComponent comp, PMDProgressRenderer progressRenderer) {
Map<String, String> options = comp.getOptions();
Project project = comp.getCurrentProject();

Expand All @@ -78,6 +92,7 @@ public List<PMDRuleSetEntryNode> runPMDAndGetResults(List<File> files, String ru

PMDJsonExportingRenderer exportingRenderer = addExportRenderer(options);
if (exportingRenderer != null) renderers.add(exportingRenderer);
if (progressRenderer != null) renderers.add(progressRenderer );

try (PmdAnalysis pmd = PmdAnalysis.create(pmdConfig)) {
files.forEach(file -> pmd.files().addFile(file.toPath()));
Expand Down

0 comments on commit f920dfd

Please sign in to comment.