Search in sources :

Example 1 with RefElement

use of com.intellij.codeInspection.reference.RefElement in project intellij-community by JetBrains.

the class OfflineDescriptorResolveResult method createDescriptor.

@Nullable
private static CommonProblemDescriptor createDescriptor(@Nullable RefEntity element, @NotNull OfflineProblemDescriptor offlineDescriptor, @NotNull InspectionToolWrapper toolWrapper, @NotNull InspectionToolPresentation presentation) {
    if (toolWrapper instanceof GlobalInspectionToolWrapper) {
        final LocalInspectionToolWrapper localTool = ((GlobalInspectionToolWrapper) toolWrapper).getSharedLocalInspectionToolWrapper();
        if (localTool != null) {
            final CommonProblemDescriptor descriptor = createDescriptor(element, offlineDescriptor, localTool, presentation);
            if (descriptor != null) {
                return descriptor;
            }
        }
        return createRerunGlobalToolDescriptor((GlobalInspectionToolWrapper) toolWrapper, element);
    }
    if (!(toolWrapper instanceof LocalInspectionToolWrapper))
        return null;
    final InspectionManager inspectionManager = InspectionManager.getInstance(presentation.getContext().getProject());
    final OfflineProblemDescriptor offlineProblemDescriptor = offlineDescriptor;
    if (element instanceof RefElement) {
        final PsiElement psiElement = ((RefElement) element).getElement();
        if (psiElement != null) {
            ProblemDescriptor descriptor = ProgressManager.getInstance().runProcess(() -> runLocalTool(psiElement, inspectionManager, offlineProblemDescriptor, (LocalInspectionToolWrapper) toolWrapper), new DaemonProgressIndicator());
            if (descriptor != null)
                return descriptor;
        }
        return null;
    }
    final List<String> hints = offlineProblemDescriptor.getHints();
    CommonProblemDescriptor descriptor = inspectionManager.createProblemDescriptor(offlineProblemDescriptor.getDescription(), (QuickFix) null);
    final QuickFix[] quickFixes = getFixes(descriptor, hints, presentation);
    if (quickFixes != null) {
        descriptor = inspectionManager.createProblemDescriptor(offlineProblemDescriptor.getDescription(), quickFixes);
    }
    return descriptor;
}
Also used : GlobalInspectionToolWrapper(com.intellij.codeInspection.ex.GlobalInspectionToolWrapper) DaemonProgressIndicator(com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator) OfflineProblemDescriptor(com.intellij.codeInspection.offline.OfflineProblemDescriptor) OfflineProblemDescriptor(com.intellij.codeInspection.offline.OfflineProblemDescriptor) RefElement(com.intellij.codeInspection.reference.RefElement) LocalInspectionToolWrapper(com.intellij.codeInspection.ex.LocalInspectionToolWrapper) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with RefElement

use of com.intellij.codeInspection.reference.RefElement in project intellij-community by JetBrains.

the class SuppressActionSequentialTask method suppress.

private void suppress(@NotNull final PsiElement element, @Nullable final CommonProblemDescriptor descriptor, @NotNull final SuppressIntentionAction action, @NotNull final RefEntity refEntity, InspectionToolWrapper wrapper, @NotNull final SuppressableInspectionTreeNode node) {
    if (action instanceof SuppressIntentionActionFromFix && !(descriptor instanceof ProblemDescriptor)) {
        LOG.info("local suppression fix for specific problem descriptor:  " + wrapper.getTool().getClass().getName());
    }
    final Project project = element.getProject();
    ApplicationManager.getApplication().runWriteAction(() -> {
        PsiDocumentManager.getInstance(project).commitAllDocuments();
        try {
            PsiElement container = null;
            if (action instanceof SuppressIntentionActionFromFix) {
                container = ((SuppressIntentionActionFromFix) action).getContainer(element);
            }
            if (container == null) {
                container = element;
            }
            if (action.isAvailable(project, null, element)) {
                action.invoke(project, null, element);
            }
            final Set<GlobalInspectionContextImpl> globalInspectionContexts = ((InspectionManagerEx) InspectionManager.getInstance(element.getProject())).getRunningContexts();
            for (GlobalInspectionContextImpl context : globalInspectionContexts) {
                context.ignoreElement(wrapper.getTool(), container);
                if (descriptor != null) {
                    context.getPresentation(wrapper).ignoreCurrentElementProblem(refEntity, descriptor);
                }
            }
            final RefElement containerRef = refEntity.getRefManager().getReference(container);
            final Set<Object> suppressedNodes = myContext.getView().getSuppressedNodes(wrapper.getShortName());
            if (containerRef != null) {
                Queue<RefEntity> toIgnoreInView = new Queue<>(1);
                toIgnoreInView.addLast(containerRef);
                while (!toIgnoreInView.isEmpty()) {
                    final RefEntity entity = toIgnoreInView.pullFirst();
                    if (node instanceof ProblemDescriptionNode) {
                        final CommonProblemDescriptor[] descriptors = myContext.getPresentation(wrapper).getIgnoredElements().get(entity);
                        if (descriptors != null) {
                            Collections.addAll(suppressedNodes, descriptors);
                        }
                    } else {
                        suppressedNodes.add(entity);
                    }
                    final List<RefEntity> children = entity.getChildren();
                    for (RefEntity child : children) {
                        toIgnoreInView.addLast(child);
                    }
                }
            }
            if (node instanceof ProblemDescriptionNode) {
                suppressedNodes.add(descriptor);
            }
        } catch (IncorrectOperationException e1) {
            LOG.error(e1);
        }
    });
    node.removeSuppressActionFromAvailable(mySuppressAction);
}
Also used : InspectionManagerEx(com.intellij.codeInspection.ex.InspectionManagerEx) GlobalInspectionContextImpl(com.intellij.codeInspection.ex.GlobalInspectionContextImpl) RefElement(com.intellij.codeInspection.reference.RefElement) Project(com.intellij.openapi.project.Project) ProblemDescriptionNode(com.intellij.codeInspection.ui.ProblemDescriptionNode) RefEntity(com.intellij.codeInspection.reference.RefEntity) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Queue(com.intellij.util.containers.Queue) PsiElement(com.intellij.psi.PsiElement)

Example 3 with RefElement

use of com.intellij.codeInspection.reference.RefElement in project intellij-community by JetBrains.

the class InspectionResultsView method initOccurenceNavigator.

private OccurenceNavigatorSupport initOccurenceNavigator() {
    return new OccurenceNavigatorSupport(myTree) {

        @Override
        @Nullable
        protected Navigatable createDescriptorForNode(DefaultMutableTreeNode node) {
            if (node instanceof InspectionTreeNode && ((InspectionTreeNode) node).isExcluded(myExcludedInspectionTreeNodesManager)) {
                return null;
            }
            if (node instanceof RefElementNode) {
                final RefElementNode refNode = (RefElementNode) node;
                if (refNode.hasDescriptorsUnder())
                    return null;
                final RefEntity element = refNode.getElement();
                if (element == null || !element.isValid())
                    return null;
                final CommonProblemDescriptor problem = refNode.getDescriptor();
                if (problem != null) {
                    return navigate(problem);
                }
                if (element instanceof RefElement) {
                    return getOpenFileDescriptor((RefElement) element);
                }
            } else if (node instanceof ProblemDescriptionNode) {
                boolean isValid;
                if (((ProblemDescriptionNode) node).isValid()) {
                    if (((ProblemDescriptionNode) node).isQuickFixAppliedFromView()) {
                        isValid = ((ProblemDescriptionNode) node).calculateIsValid();
                    } else {
                        isValid = true;
                    }
                } else {
                    isValid = false;
                }
                return isValid ? navigate(((ProblemDescriptionNode) node).getDescriptor()) : InspectionResultsViewUtil.getNavigatableForInvalidNode((ProblemDescriptionNode) node);
            }
            return null;
        }

        @Nullable
        private Navigatable navigate(final CommonProblemDescriptor descriptor) {
            return getSelectedNavigatable(descriptor);
        }

        @Override
        public String getNextOccurenceActionName() {
            return InspectionsBundle.message("inspection.action.go.next");
        }

        @Override
        public String getPreviousOccurenceActionName() {
            return InspectionsBundle.message("inspection.actiongo.prev");
        }
    };
}
Also used : RefElement(com.intellij.codeInspection.reference.RefElement) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) RefEntity(com.intellij.codeInspection.reference.RefEntity)

Example 4 with RefElement

use of com.intellij.codeInspection.reference.RefElement in project intellij-community by JetBrains.

the class InspectionResultsViewComparator method compareEntities.

private static int compareEntities(final RefEntity entity1, final RefEntity entity2) {
    if (entity1 instanceof RefElement && entity2 instanceof RefElement) {
        final SmartPsiElementPointer p1 = ((RefElement) entity1).getPointer();
        final SmartPsiElementPointer p2 = ((RefElement) entity2).getPointer();
        if (p1 != null && p2 != null) {
            final VirtualFile file1 = p1.getVirtualFile();
            final VirtualFile file2 = p2.getVirtualFile();
            if (file1 != null && Comparing.equal(file1, file2) && file1.isValid()) {
                final int positionComparing = PsiUtilCore.compareElementsByPosition(((RefElement) entity1).getElement(), ((RefElement) entity2).getElement());
                if (positionComparing != 0) {
                    return positionComparing;
                }
            }
        }
    }
    if (entity1 instanceof RefFile && entity2 instanceof RefFile) {
        final VirtualFile file1 = ((RefFile) entity1).getPointer().getVirtualFile();
        final VirtualFile file2 = ((RefFile) entity2).getPointer().getVirtualFile();
        if (file1 != null && file2 != null) {
            if (file1.equals(file2))
                return 0;
            final int cmp = compareEntitiesByName(entity1, entity2);
            if (cmp != 0)
                return cmp;
            return file1.hashCode() - file2.hashCode();
        }
    }
    if (entity1 != null && entity2 != null) {
        return compareEntitiesByName(entity1, entity2);
    }
    if (entity1 != null)
        return -1;
    return entity2 != null ? 1 : 0;
}
Also used : RefElement(com.intellij.codeInspection.reference.RefElement) VirtualFile(com.intellij.openapi.vfs.VirtualFile) SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer) RefFile(com.intellij.codeInspection.reference.RefFile)

Example 5 with RefElement

use of com.intellij.codeInspection.reference.RefElement in project intellij-community by JetBrains.

the class InspectionResultsViewUtil method getNavigatableForInvalidNode.

@Nullable
static Navigatable getNavigatableForInvalidNode(ProblemDescriptionNode node) {
    RefEntity element = node.getElement();
    while (element != null && !element.isValid()) {
        element = element.getOwner();
    }
    if (!(element instanceof RefElement))
        return null;
    PsiElement containingElement = ((RefElement) element).getElement();
    if (!(containingElement instanceof NavigatablePsiElement) || !containingElement.isValid())
        return null;
    final int lineNumber = node.getLineNumber();
    if (lineNumber != -1) {
        final PsiFile containingFile = containingElement.getContainingFile();
        if (containingFile != null) {
            final VirtualFile file = containingFile.getVirtualFile();
            final Document document = FileDocumentManager.getInstance().getDocument(file);
            if (document != null && document.getLineCount() > lineNumber) {
                return new OpenFileDescriptor(containingElement.getProject(), file, lineNumber, 0);
            }
        }
    }
    return (Navigatable) containingElement;
}
Also used : RefElement(com.intellij.codeInspection.reference.RefElement) VirtualFile(com.intellij.openapi.vfs.VirtualFile) RefEntity(com.intellij.codeInspection.reference.RefEntity) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) Document(com.intellij.openapi.editor.Document) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) PsiElement(com.intellij.psi.PsiElement) NavigatablePsiElement(com.intellij.psi.NavigatablePsiElement) Navigatable(com.intellij.pom.Navigatable) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

RefElement (com.intellij.codeInspection.reference.RefElement)25 RefEntity (com.intellij.codeInspection.reference.RefEntity)16 PsiElement (com.intellij.psi.PsiElement)11 NotNull (org.jetbrains.annotations.NotNull)7 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Nullable (org.jetbrains.annotations.Nullable)4 EntryPoint (com.intellij.codeInspection.reference.EntryPoint)3 PsiFile (com.intellij.psi.PsiFile)3 Element (org.jdom.Element)3 UnusedDeclarationInspectionBase (com.intellij.codeInspection.deadCode.UnusedDeclarationInspectionBase)2 GlobalInspectionToolWrapper (com.intellij.codeInspection.ex.GlobalInspectionToolWrapper)2 LocalInspectionToolWrapper (com.intellij.codeInspection.ex.LocalInspectionToolWrapper)2 RefClass (com.intellij.codeInspection.reference.RefClass)2 RefJavaUtil (com.intellij.codeInspection.reference.RefJavaUtil)2 RefManagerImpl (com.intellij.codeInspection.reference.RefManagerImpl)2 Project (com.intellij.openapi.project.Project)2 Navigatable (com.intellij.pom.Navigatable)2 SearchScope (com.intellij.psi.search.SearchScope)2 HashSet (com.intellij.util.containers.HashSet)2 THashSet (gnu.trove.THashSet)2