TreeVisitor (@NotNull HierarchyNodeDescriptor descriptor) { PsiElement element = descriptor.getPsiElement(); if (element == null) return path -> TreeVisitor.Action.INTERRUPT; PsiManager psiManager = element.getManager(); return path -> { Object component = path.getLastPathComponent(); DefaultMutableTreeNode node = (DefaultMutableTreeNode)component; Object object = node.getUserObject(); HierarchyNodeDescriptor current = (HierarchyNodeDescriptor)object; PsiElement currentPsiElement = current.getPsiElement(); if (psiManager.areElementsEquivalent(currentPsiElement, element)) return TreeVisitor.Action.INTERRUPT; return isAncestor(myProject, current, descriptor) ? TreeVisitor.Action.CONTINUE : TreeVisitor.Action.SKIP_CHILDREN; }; }