Skip to content

Commit

Permalink
Add basic support for grouping results by package (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshiell committed Nov 18, 2024
1 parent a2ef698 commit 154d105
Show file tree
Hide file tree
Showing 15 changed files with 301 additions and 95 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

# CheckStyle-IDEA Changelog

* **5.99.0** New: Added option to group results by package (#3).
* **5.98.0** New: Added Checkstyle 10.20.1.
* **5.97.0** Fixed: Refactored code to fix exception around API dependencies at initialisation (#655).
* **5.97.0** New: Added Checkstyle 10.19.0.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ plugins {
id("org.infernus.idea.checkstyle.build")
}

version = "5.98.0"
version = "5.99.0"

intellijPlatform {
pluginConfiguration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import org.gradle.language.base.plugins.LifecycleBasePlugin;
import org.jetbrains.annotations.NotNull;

import static org.codehaus.groovy.runtime.DefaultGroovyMethods.println;

/**
* The main plugin class. The action starts here.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.infernus.idea.checkstyle.actions;

import org.infernus.idea.checkstyle.toolwindow.ResultGrouping;

public class GroupByFile extends GroupingAction {

public GroupByFile() {
super(ResultGrouping.BY_FILE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.infernus.idea.checkstyle.actions;

import org.infernus.idea.checkstyle.toolwindow.ResultGrouping;

public class GroupByPackage extends GroupingAction {

public GroupByPackage() {
super(ResultGrouping.BY_PACKAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.infernus.idea.checkstyle.actions;

import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.DumbAwareToggleAction;
import com.intellij.openapi.project.Project;
import org.infernus.idea.checkstyle.toolwindow.ResultGrouping;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

import static org.infernus.idea.checkstyle.actions.ToolWindowAccess.*;

abstract class GroupingAction extends DumbAwareToggleAction {

private final ResultGrouping grouping;

GroupingAction(@NotNull final ResultGrouping grouping) {
this.grouping = grouping;
}

@Override
public boolean isSelected(final @NotNull AnActionEvent event) {
final Project project = getEventProject(event);
if (project == null) {
return false;
}

Boolean selected = getFromToolWindowPanel(toolWindow(project), panel -> panel.groupedBy() == grouping);
return Objects.requireNonNullElse(selected, false);
}

@Override
public void setSelected(final @NotNull AnActionEvent event, final boolean selected) {
final Project project = getEventProject(event);
if (project == null) {
return;
}

actOnToolWindowPanel(toolWindow(project), panel -> {
panel.groupBy(grouping);
});
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.EDT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -297,7 +295,7 @@ private void clearProgress() {
*/
private void scrollToError(final TreePath treePath) {
final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
if (treeNode == null || !(treeNode.getUserObject() instanceof ProblemResultTreeNode nodeInfo)) {
if (treeNode == null || !(treeNode.getUserObject() instanceof ProblemResultTreeInfo nodeInfo)) {
return;
}

Expand Down Expand Up @@ -326,11 +324,11 @@ private void scrollToError(final TreePath treePath) {
}, ModalityState.NON_MODAL);
}

private int lineFor(final ProblemResultTreeNode nodeInfo) {
private int lineFor(final ProblemResultTreeInfo nodeInfo) {
return nodeInfo.getProblem().line();
}

private int columnFor(final ProblemResultTreeNode nodeInfo) {
private int columnFor(final ProblemResultTreeInfo nodeInfo) {
return nodeInfo.getProblem().column();
}

Expand Down Expand Up @@ -521,8 +519,8 @@ public void displayErrorResult(final Throwable error) {
clearProgress();
}

private SeverityLevel[] getDisplayedSeverities() {
final List<SeverityLevel> severityLevels = new ArrayList<>();
private Set<SeverityLevel> getDisplayedSeverities() {
final var severityLevels = new HashSet<SeverityLevel>();

if (displayingErrors) {
severityLevels.add(SeverityLevel.Error);
Expand All @@ -536,7 +534,7 @@ private SeverityLevel[] getDisplayedSeverities() {
severityLevels.add(SeverityLevel.Info);
}

return severityLevels.toArray(new SeverityLevel[0]);
return severityLevels;
}

/**
Expand Down Expand Up @@ -586,6 +584,14 @@ public void setDisplayingInfo(final boolean displayingInfo) {
this.displayingInfo = displayingInfo;
}

public void groupBy(final ResultGrouping grouping) {
treeModel.groupBy(grouping);
}

public ResultGrouping groupedBy() {
return treeModel.groupedBy();
}

private PluginConfigurationManager configurationManager() {
return project.getService(PluginConfigurationManager.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.intellij.icons.AllIcons;
import org.infernus.idea.checkstyle.CheckStyleBundle;

public class FileResultTreeNode extends ResultTreeNode {
public class FileResultTreeInfo extends ResultTreeNode {

private final String fileName;
private final int totalProblems;
Expand All @@ -15,7 +15,7 @@ public class FileResultTreeNode extends ResultTreeNode {
* @param fileName the name of the file.
* @param problemCount the number of problems in the file.
*/
public FileResultTreeNode(final String fileName, final int problemCount) {
public FileResultTreeInfo(final String fileName, final int problemCount) {
super(CheckStyleBundle.message("plugin.results.scan-file-result", fileName, problemCount));

if (fileName == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.infernus.idea.checkstyle.toolwindow;

import com.intellij.icons.AllIcons;
import org.infernus.idea.checkstyle.CheckStyleBundle;

public class PackageTreeInfo extends ResultTreeNode {

private final String packageName;
private final int totalProblems;
private int visibleProblems;

/**
* Construct a package node.
*
* @param packageName the name of the package.
* @param problemCount the number of problems in the file.
*/
public PackageTreeInfo(final String packageName, final int problemCount) {
super(CheckStyleBundle.message("plugin.results.scan-file-result", packageName, problemCount));

if (packageName == null) {
throw new IllegalArgumentException("Package name may not be null");
}

this.packageName = packageName;
this.totalProblems = problemCount;
this.visibleProblems = problemCount;

updateDisplayText();

setIcon(AllIcons.Nodes.Package);
}

private void updateDisplayText() {
if (totalProblems == visibleProblems) {
setText(CheckStyleBundle.message("plugin.results.scan-package-result", packageName, totalProblems));
} else {
setText(CheckStyleBundle.message("plugin.results.scan-package-result.filtered", packageName, visibleProblems, totalProblems - visibleProblems));
}
}

void setVisibleProblems(final int visibleProblems) {
this.visibleProblems = visibleProblems;

updateDisplayText();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.infernus.idea.checkstyle.csapi.SeverityLevel;
import org.jetbrains.annotations.NotNull;

public class ProblemResultTreeNode extends ResultTreeNode {
public class ProblemResultTreeInfo extends ResultTreeNode {

private final PsiFile file;
private final Problem problem;
Expand All @@ -19,7 +19,7 @@ public class ProblemResultTreeNode extends ResultTreeNode {
* @param file the file the problem exists in.
* @param problem the problem.
*/
public ProblemResultTreeNode(@NotNull final PsiFile file,
public ProblemResultTreeInfo(@NotNull final PsiFile file,
@NotNull final Problem problem) {
super(CheckStyleBundle.message("plugin.results.file-result",
file.getName(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.infernus.idea.checkstyle.toolwindow;

public enum ResultGrouping {

BY_FILE,
BY_PACKAGE

}
Loading

0 comments on commit 154d105

Please sign in to comment.