Object (Object target) { if (target instanceof PsiElement && !((PsiElement)target).isValid()) return target; if (target instanceof PsiDirectory directory) { for (VirtualFile file = directory.getVirtualFile(), next; ; file = next) { VirtualFile[] children = file.getChildren(); VirtualFile child = children.length == 1 ? children[0] : null; //noinspection AssignmentToForLoopParameter next = child != null && child.isDirectory() && !child.is(VFileProperty.SYMLINK) ? child : null; if (next == null) return ObjectUtils.notNull(directory.getManager().findDirectory(file), directory); } } return target; }