DefaultMutableTreeNode (final PsiElement element, PsiDirectory parent) { LOG.assertTrue(parent != null, element instanceof PsiFile && ((PsiFile)element).getVirtualFile() != null ? ((PsiFile)element).getVirtualFile().getPath() : element); final VirtualFile parentVirtualFile = parent.getVirtualFile(); Module module = myFileIndex.getModuleForFile(parentVirtualFile); if (element instanceof PsiDirectory psiDirectory && myFlattenPackages) { final PackageDependenciesNode moduleNode = getModuleNode(module); final VirtualFile virtualFile = psiDirectory.getVirtualFile(); final PackageDependenciesNode dirNode = getModuleDirNode(virtualFile, myFileIndex.getModuleForFile(virtualFile), null); dirNode.removeFromParent(); return moduleNode; } DirectoryNode dirNode = myModuleDirNodes.get(parentVirtualFile); if (dirNode == null) return null; DirectoryNode wrapper = dirNode.getWrapper(); while (wrapper != null) { dirNode = wrapper; myModuleDirNodes.put(wrapper.getDirectory(), null); wrapper = dirNode.getWrapper(); } final PackageDependenciesNode[] classOrDirNodes = findNodeForPsiElement(dirNode, element); if (classOrDirNodes != null){ for (PackageDependenciesNode classNode : classOrDirNodes) { classNode.removeFromParent(); } } DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)dirNode.getParent(); DefaultMutableTreeNode node = dirNode; if (element == parent) { myModuleDirNodes.put(parentVirtualFile, null); dirNode.removeFromParent(); node = parentNode; } while (parent != null && node != null && node.getChildCount() == 0) { PsiDirectory directory = parent.getParentDirectory(); parentNode = (DefaultMutableTreeNode)node.getParent(); node.removeFromParent(); if (node instanceof DirectoryNode) { while (node != null) { //clear all compacted links myModuleDirNodes.put(((DirectoryNode)node).getDirectory(), null); node = ((DirectoryNode)node).getCompactedDirNode(); } } else if (node instanceof ModuleNode) { myModuleNodes.put(((ModuleNode)node).getModule(), null); } else if (node instanceof ModuleGroupNode) { myModuleGroupNodes.put(((ModuleGroupNode)node).getModuleGroupName(), null); } node = parentNode; parent = directory; } if (myCompactEmptyMiddlePackages && node instanceof DirectoryNode && node.getChildCount() == 1) { //compact final TreeNode treeNode = node.getChildAt(0); if (treeNode instanceof DirectoryNode){ node.removeAllChildren(); for (int i = treeNode.getChildCount() - 1; i >= 0; i--){ node.add((MutableTreeNode)treeNode.getChildAt(i)); } ((DirectoryNode)node).setCompactedDirNode((DirectoryNode)treeNode); } } return parentNode != null ? parentNode : myRoot; }