export html action: do not traverse tree for excluded elements
authorDmitry Batkovich <dmitry.batkovich@jetbrains.com>
Wed, 4 May 2016 08:21:44 +0000 (11:21 +0300)
committerDmitry Batkovich <dmitry.batkovich@jetbrains.com>
Wed, 4 May 2016 08:21:44 +0000 (11:21 +0300)
java/java-impl/src/com/intellij/codeInspection/deadCode/UnusedDeclarationPresentation.java
platform/lang-impl/src/com/intellij/codeInspection/ex/GlobalInspectionContextImpl.java
platform/lang-impl/src/com/intellij/codeInspection/ui/DefaultInspectionToolPresentation.java
platform/lang-impl/src/com/intellij/codeInspection/ui/ExcludedInspectionTreeNodesManager.java
platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionResultsView.java
platform/lang-impl/src/com/intellij/codeInspection/ui/InspectionToolPresentation.java
platform/lang-impl/src/com/intellij/codeInspection/ui/actions/ExportHTMLAction.java
platform/testFramework/src/com/intellij/testFramework/InspectionTestUtil.java

index 639004bf4586e9b16af7aa704cab40aac3956fb0..2d41d411d3e801ad420eaca4ba0053c54fe4ba22 100644 (file)
@@ -70,6 +70,7 @@ import java.awt.event.KeyEvent;
 import java.awt.event.MouseEvent;
 import java.net.URL;
 import java.util.*;
+import java.util.function.Predicate;
 
 public class UnusedDeclarationPresentation extends DefaultInspectionToolPresentation {
   private final Map<String, Set<RefEntity>> myPackageContents = Collections.synchronizedMap(new HashMap<String, Set<RefEntity>>());
@@ -125,7 +126,7 @@ public class UnusedDeclarationPresentation extends DefaultInspectionToolPresenta
   @Override
   public void exportResults(@NotNull final Element parentNode,
                             @NotNull RefEntity refEntity,
-                            @NotNull Set<CommonProblemDescriptor> excludedDescriptions) {
+                            @NotNull Predicate<CommonProblemDescriptor> excludedDescriptions) {
     if (!(refEntity instanceof RefJavaElement)) return;
     final RefFilter filter = getFilter();
     if (!getIgnoredRefElements().contains(refEntity) && filter.accepts((RefJavaElement)refEntity)) {
index 9eede64bf64b8f6c6b24cba59a744e23b7478855..bfc7d26c7610d5258699dbf7043ef56c3df6fb52 100644 (file)
@@ -255,7 +255,7 @@ public class GlobalInspectionContextImpl extends GlobalInspectionContextBase imp
             try {
               InspectionToolWrapper toolWrapper = state.getTool();
               InspectionToolPresentation presentation = getPresentation(toolWrapper);
-              presentation.exportResults(element, refEntity, Collections.emptySet());
+              presentation.exportResults(element, refEntity, d -> false);
             }
             catch (Throwable e) {
               LOG.error("Problem when exporting: " + refEntity.getExternalName(), e);
index 3513d4247aa923dca27ba6dafeb4931e9ff2edfd..d277b3e84086384f5f1f2462b3393cd4bae9e30c 100644 (file)
@@ -54,6 +54,7 @@ import org.jetbrains.annotations.Nullable;
 
 import java.io.*;
 import java.util.*;
+import java.util.function.Predicate;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -173,12 +174,12 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro
 
   @Override
   public void exportResults(@NotNull final Element parentNode,
-                            @NotNull final Set<RefEntity> excludedEntities,
-                            @NotNull final Set<CommonProblemDescriptor> excludedDescriptors) {
+                            @NotNull final Predicate<RefEntity> excludedEntities,
+                            @NotNull final Predicate<CommonProblemDescriptor> excludedDescriptors) {
     getRefManager().iterate(new RefVisitor(){
       @Override
       public void visitElement(@NotNull RefEntity elem) {
-        if (!excludedEntities.contains(elem)) {
+        if (!excludedEntities.test(elem)) {
           exportResults(parentNode, elem, excludedDescriptors);
         }
       }
@@ -294,7 +295,7 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro
 
   private synchronized void writeOutput(@NotNull final CommonProblemDescriptor[] descriptions, @NotNull RefEntity refElement) {
     final Element parentNode = new Element(InspectionsBundle.message("inspection.problems"));
-    exportResults(descriptions, refElement, parentNode, Collections.emptySet());
+    exportResults(descriptions, refElement, parentNode, d -> false);
     final List list = parentNode.getChildren();
 
     @NonNls final String ext = ".xml";
@@ -474,12 +475,12 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro
   @Override
   public void exportResults(@NotNull final Element parentNode,
                             @NotNull RefEntity refEntity,
-                            @NotNull Set<CommonProblemDescriptor> excludedDescriptors) {
+                            @NotNull Predicate<CommonProblemDescriptor> isDescriptorExcluded) {
     synchronized (lock) {
       if (getProblemElements().containsKey(refEntity)) {
         CommonProblemDescriptor[] descriptions = getDescriptions(refEntity);
         if (descriptions != null) {
-          exportResults(descriptions, refEntity, parentNode, excludedDescriptors);
+          exportResults(descriptions, refEntity, parentNode, isDescriptorExcluded);
         }
       }
     }
@@ -488,9 +489,9 @@ public class DefaultInspectionToolPresentation implements ProblemDescriptionsPro
   private void exportResults(@NotNull final CommonProblemDescriptor[] descriptors,
                              @NotNull RefEntity refEntity,
                              @NotNull Element parentNode,
-                             @NotNull Set<CommonProblemDescriptor> excludedDescriptors) {
+                             @NotNull Predicate<CommonProblemDescriptor> isDescriptorExcluded) {
     for (CommonProblemDescriptor descriptor : descriptors) {
-      if (excludedDescriptors.contains(descriptor)) continue;
+      if (isDescriptorExcluded.test(descriptor)) continue;
       @NonNls final String template = descriptor.getDescriptionTemplate();
       int line = descriptor instanceof ProblemDescriptor ? ((ProblemDescriptor)descriptor).getLineNumber() : -1;
       final PsiElement psiElement = descriptor instanceof ProblemDescriptor ? ((ProblemDescriptor)descriptor).getPsiElement() : null;
index 5250c6566a2df0048d559029d39de6190ab6562c..3362b90900eb8fef314265e0ce770f758c843265 100644 (file)
 package com.intellij.codeInspection.ui;
 
 
+import com.intellij.codeInspection.CommonProblemDescriptor;
+import com.intellij.codeInspection.offlineViewer.OfflineProblemDescriptorNode;
+import com.intellij.codeInspection.offlineViewer.OfflineRefElementNode;
+import com.intellij.codeInspection.reference.RefEntity;
 import com.intellij.util.containers.FactoryMap;
 import gnu.trove.THashSet;
+import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
+import java.util.Collections;
 import java.util.Map;
 import java.util.Set;
 
@@ -32,12 +38,18 @@ public class ExcludedInspectionTreeNodesManager {
     @Nullable
     @Override
     protected Set<Object> create(Class key) {
-      return new THashSet<>();
+      return new THashSet<Object>();
     }
   };
 
+  private final boolean myOffline;
+
+  public ExcludedInspectionTreeNodesManager(boolean offline) {
+    myOffline = offline;
+  }
+
   public synchronized boolean isExcluded(InspectionTreeNode node) {
-    final Set<Object> excluded = myExcludedNodeObjects.get(node.getClass());
+    final Set<?> excluded = myExcludedNodeObjects.get(node.getClass());
     return excluded.contains(node.getUserObject());
   }
 
@@ -48,4 +60,12 @@ public class ExcludedInspectionTreeNodesManager {
   public synchronized void amnesty(InspectionTreeNode node) {
     myExcludedNodeObjects.get(node.getClass()).remove(node.getUserObject());
   }
+
+  public synchronized boolean containsRefEntity(@NotNull RefEntity entity) {
+    return myExcludedNodeObjects.getOrDefault(myOffline ? OfflineRefElementNode.class : RefElementNode.class, Collections.emptySet()).contains(entity);
+  }
+
+  public synchronized boolean containsProblemDescriptor(@NotNull CommonProblemDescriptor descriptor) {
+    return myExcludedNodeObjects.getOrDefault(myOffline ? OfflineProblemDescriptorNode.class : ProblemDescriptionNode.class, Collections.emptySet()).contains(descriptor);
+  }
 }
index ccd8225a27f99f1d9d0c917b7c4de8a1aaebbb99..3b0e308a3e6903b62aa48215014d1d791c10e234 100644 (file)
@@ -23,6 +23,7 @@ import com.intellij.codeHighlighting.HighlightDisplayLevel;
 import com.intellij.codeInsight.daemon.HighlightDisplayKey;
 import com.intellij.codeInspection.*;
 import com.intellij.codeInspection.ex.*;
+import com.intellij.codeInspection.offlineViewer.OfflineInspectionRVContentProvider;
 import com.intellij.codeInspection.reference.RefElement;
 import com.intellij.codeInspection.reference.RefEntity;
 import com.intellij.codeInspection.ui.actions.ExportHTMLAction;
@@ -121,7 +122,7 @@ public class InspectionResultsView extends JPanel implements Disposable, Occuren
   private final ExclusionHandler<InspectionTreeNode> myExclusionHandler;
   private EditorEx myPreviewEditor;
   private InspectionTreeLoadingProgressAware myLoadingProgressPreview;
-  private final ExcludedInspectionTreeNodesManager myExcludedInspectionTreeNodesManager = new ExcludedInspectionTreeNodesManager();
+  private final ExcludedInspectionTreeNodesManager myExcludedInspectionTreeNodesManager;
   private final Set<Object> mySuppressedNodes = new HashSet<>();
   private final ConcurrentMap<InspectionToolWrapper, Set<SuppressIntentionAction>> mySuppressActions = new ConcurrentHashMap<>();
 
@@ -135,6 +136,7 @@ public class InspectionResultsView extends JPanel implements Disposable, Occuren
     myScope = globalInspectionContext.getCurrentScope();
     myGlobalInspectionContext = globalInspectionContext;
     myProvider = provider;
+    myExcludedInspectionTreeNodesManager = new ExcludedInspectionTreeNodesManager(provider instanceof OfflineInspectionRVContentProvider);
 
     myTree = new InspectionTree(myProject, globalInspectionContext, this);
     initTreeListeners();
index 9356ef20eaa26a3c8d4cd061ebd51ed18f8de070..66daf80e24fc1a6203c6acbfad1ea435c1a49666 100644 (file)
@@ -31,6 +31,7 @@ import javax.swing.*;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Set;
+import java.util.function.Predicate;
 
 public interface InspectionToolPresentation extends ProblemDescriptionsProcessor {
 
@@ -64,7 +65,7 @@ public interface InspectionToolPresentation extends ProblemDescriptionsProcessor
   IntentionAction findQuickFixes(@NotNull CommonProblemDescriptor descriptor, final String hint);
   @NotNull
   HTMLComposerImpl getComposer();
-  void exportResults(@NotNull final Element parentNode, @NotNull RefEntity refEntity, Set<CommonProblemDescriptor> excludedDescriptions);
+  void exportResults(@NotNull final Element parentNode, @NotNull RefEntity refEntity, Predicate<CommonProblemDescriptor> isDescriptorExcluded);
   @NotNull
   Set<RefModule> getModuleProblems();
   @Nullable
@@ -88,8 +89,8 @@ public interface InspectionToolPresentation extends ProblemDescriptionsProcessor
                                       @NotNull Map<RefEntity, CommonProblemDescriptor[]> descriptorMap,
                                       @Nullable CommonProblemDescriptor[] allowedDescriptors);
   void exportResults(@NotNull final Element parentNode,
-                     @NotNull final Set<RefEntity> excludedEntities,
-                     @NotNull final Set<CommonProblemDescriptor> excludedDescriptors);
+                     @NotNull final Predicate<RefEntity> isEntityExcluded,
+                     @NotNull final Predicate<CommonProblemDescriptor> isProblemExcluded);
 
   default JComponent getCustomPreviewPanel(RefEntity entity) {
     return null;
index 1073c624995471b8bb8aba9d77c663150e6d12e2..e17e2525331e2779c7f30d28e0969c0c2d0f7015 100644 (file)
 package com.intellij.codeInspection.ui.actions;
 
 import com.intellij.codeEditor.printing.ExportToHTMLSettings;
-import com.intellij.codeInspection.CommonProblemDescriptor;
 import com.intellij.codeInspection.InspectionApplication;
 import com.intellij.codeInspection.InspectionsBundle;
 import com.intellij.codeInspection.ex.GlobalInspectionContextImpl;
 import com.intellij.codeInspection.ex.InspectionToolWrapper;
 import com.intellij.codeInspection.ex.ScopeToolState;
 import com.intellij.codeInspection.ex.Tools;
-import com.intellij.codeInspection.export.*;
-import com.intellij.codeInspection.reference.RefEntity;
+import com.intellij.codeInspection.export.ExportToHTMLDialog;
+import com.intellij.codeInspection.export.InspectionTreeHtmlWriter;
 import com.intellij.codeInspection.ui.*;
 import com.intellij.icons.AllIcons;
 import com.intellij.ide.BrowserUtil;
@@ -53,7 +52,8 @@ import org.jetbrains.annotations.NotNull;
 
 import java.io.File;
 import java.io.IOException;
-import java.util.*;
+import java.util.HashSet;
+import java.util.Set;
 
 /**
  * User: anna
@@ -141,22 +141,9 @@ public class ExportHTMLAction extends AnAction implements DumbAware {
           final Set<InspectionToolWrapper> toolWrappers = getWorkedTools(toolNode);
           for (InspectionToolWrapper wrapper : toolWrappers) {
             InspectionToolPresentation presentation = myView.getGlobalInspectionContext().getPresentation(wrapper);
-            if (!toolNode.isExcluded(myView.getExcludedManager())) {
-              final Set<RefEntity> excludedEntities = new HashSet<>();
-              final Set<CommonProblemDescriptor> excludedDescriptors = new HashSet<>();
-              TreeUtil.traverse(toolNode, o -> {
-                InspectionTreeNode n = (InspectionTreeNode)o;
-                if (n.isExcluded(myView.getExcludedManager())) {
-                  if (n instanceof RefElementNode) {
-                    excludedEntities.add(((RefElementNode)n).getElement());
-                  }
-                  if (n instanceof ProblemDescriptionNode) {
-                    excludedDescriptors.add(((ProblemDescriptionNode)n).getDescriptor());
-                  }
-                }
-                return true;
-              });
-              presentation.exportResults(problems, excludedEntities, excludedDescriptors);
+            final ExcludedInspectionTreeNodesManager excludedManager = myView.getExcludedManager();
+            if (!toolNode.isExcluded(excludedManager)) {
+              presentation.exportResults(problems, excludedManager::containsRefEntity, excludedManager::containsProblemDescriptor);
             }
           }
           PathMacroManager.getInstance(myView.getProject()).collapsePaths(problems);
index e44c9650d9eb5060374c662fbf02d0ae33b5c0e1..e0dd032f0e956120a8cc71fae90ca08ea421af24 100644 (file)
@@ -33,7 +33,6 @@ import java.io.CharArrayReader;
 import java.io.File;
 import java.io.StreamTokenizer;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 
 public class InspectionTestUtil {
@@ -139,7 +138,7 @@ expected:
     InspectionToolPresentation presentation = context.getPresentation(toolWrapper);
 
     presentation.updateContent();  //e.g. dead code need check for reachables
-    presentation.exportResults(root, Collections.emptySet(), Collections.emptySet());
+    presentation.exportResults(root, x -> false, x -> false);
 
     File file = new File(testDir + "/expected.xml");
     try {