From c1a446d047c211ebe29ceedc1fc18a753465f8cc Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Sun, 30 Mar 2025 16:28:16 +0200 Subject: [PATCH] [inspections] fix Exclude for batch Unused Declaration inspection results (IDEA-269854) GitOrigin-RevId: 442af97a9d1d70627713d924a1290b6f27e632b2 --- platform/lang-impl/api-dump-unreviewed.txt | 1 + .../codeInspection/ex/QuickFixAction.java | 5 ++- .../codeInspection/ui/InspectionTree.java | 36 ++++++++++++------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/platform/lang-impl/api-dump-unreviewed.txt b/platform/lang-impl/api-dump-unreviewed.txt index 92f4c4952017..6151ac62b05b 100644 --- a/platform/lang-impl/api-dump-unreviewed.txt +++ b/platform/lang-impl/api-dump-unreviewed.txt @@ -6287,6 +6287,7 @@ f:com.intellij.codeInspection.ui.InspectionTree - getSelectedDescriptors(com.intellij.openapi.actionSystem.AnActionEvent):com.intellij.codeInspection.CommonProblemDescriptor[] - s:getSelectedElement(com.intellij.openapi.actionSystem.AnActionEvent):com.intellij.psi.PsiElement - getSelectedElements():com.intellij.codeInspection.reference.RefEntity[] +- getSelectedElements(Z):com.intellij.codeInspection.reference.RefEntity[] - getSelectedGroupPath():java.lang.String[] - s:getSelectedRefElements(com.intellij.openapi.actionSystem.AnActionEvent):com.intellij.codeInspection.reference.RefEntity[] - getSelectedToolWrapper(Z):com.intellij.codeInspection.ex.InspectionToolWrapper diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ex/QuickFixAction.java b/platform/lang-impl/src/com/intellij/codeInspection/ex/QuickFixAction.java index dbcbe8133f55..f675105f2b58 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ex/QuickFixAction.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ex/QuickFixAction.java @@ -1,5 +1,4 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. - +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInspection.ex; import com.intellij.codeInsight.FileModificationService; @@ -245,7 +244,7 @@ public abstract class QuickFixAction extends AnAction implements CustomComponent private static RefEntity @NotNull [] getSelectedElements(InspectionResultsView view) { if (view == null) return RefEntity.EMPTY_ELEMENTS_ARRAY; - RefEntity[] selection = view.getTree().getSelectedElements(); + RefEntity[] selection = view.getTree().getSelectedElements(false); PsiDocumentManager.getInstance(view.getProject()).commitAllDocuments(); Arrays.sort(selection, InspectionResultsViewComparator::compareEntities); return selection; diff --git a/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTree.java b/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTree.java index ad8fda71f351..e1e0e90da2da 100644 --- a/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTree.java +++ b/platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionTree.java @@ -1,4 +1,4 @@ -// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package com.intellij.codeInspection.ui; import com.intellij.codeHighlighting.HighlightDisplayLevel; @@ -292,18 +292,26 @@ public final class InspectionTree extends Tree { } public RefEntity @NotNull [] getSelectedElements() { + return getSelectedElements(true); + } + + public RefEntity @NotNull [] getSelectedElements(boolean allowResolved) { TreePath[] selectionPaths = getSelectionPaths(); if (selectionPaths == null) return RefEntity.EMPTY_ELEMENTS_ARRAY; - return getElementsFromSelection(selectionPaths); + return getElementsFromSelection(selectionPaths, allowResolved); } RefEntity @NotNull [] getElementsFromSelection(TreePath @NotNull [] selectionPaths) { + return getElementsFromSelection(selectionPaths, true); + } + + private RefEntity @NotNull [] getElementsFromSelection(TreePath @NotNull [] selectionPaths, boolean allowResolved) { InspectionToolWrapper toolWrapper = getSelectedToolWrapper(true, selectionPaths); if (toolWrapper == null) return RefEntity.EMPTY_ELEMENTS_ARRAY; Set result = new LinkedHashSet<>(); for (TreePath selectionPath : selectionPaths) { final InspectionTreeNode node = (InspectionTreeNode)selectionPath.getLastPathComponent(); - addElementsInNode(node, result); + addElementsInNode(node, result, allowResolved); } return ArrayUtil.reverseArray(result.toArray(RefEntity.EMPTY_ELEMENTS_ARRAY)); } @@ -318,19 +326,21 @@ public final class InspectionTree extends Tree { if (path != null) TreeUtil.promiseSelect(this, path); } - private static void addElementsInNode(@NotNull InspectionTreeNode node, @NotNull Set out) { + private static void addElementsInNode(@NotNull InspectionTreeNode node, @NotNull Set out, boolean allowResolved) { if (!node.isValid()) return; - if (node instanceof RefElementNode) { - final RefEntity element = ((RefElementNode)node).getElement(); - out.add(element); + if (node instanceof RefElementNode refNode) { + if (isNodeValidAndIncluded(refNode, allowResolved)) { + out.add(refNode.getElement()); + } } - if (node instanceof ProblemDescriptionNode) { - final RefEntity element = ((ProblemDescriptionNode)node).getElement(); - out.add(element); + if (node instanceof ProblemDescriptionNode problemNode) { + if (isNodeValidAndIncluded(problemNode, allowResolved)) { + out.add(problemNode.getElement()); + } } for (InspectionTreeNode child : node.getChildren()) { - addElementsInNode(child, out); + addElementsInNode(child, out, allowResolved); } } @@ -465,7 +475,7 @@ public final class InspectionTree extends Tree { return count; } - private static boolean isNodeValidAndIncluded(ProblemDescriptionNode node, boolean allowResolved) { + private static boolean isNodeValidAndIncluded(SuppressableInspectionTreeNode node, boolean allowResolved) { return node.isValid() && (allowResolved || (!node.isExcluded() && !node.isAlreadySuppressedFromView() && @@ -598,7 +608,7 @@ public final class InspectionTree extends Tree { if (nodes != null) { HashSet entities = new HashSet<>(); for (Object node : nodes) { - addElementsInNode((InspectionTreeNode)node, entities); + addElementsInNode((InspectionTreeNode)node, entities, true); } return entities.toArray(entities.toArray(RefEntity.EMPTY_ELEMENTS_ARRAY)); }