T (T element, @NotNull T root, boolean isDown) { if (isDown) { T child = myWalker.getFirstChild(element); if (child != null) return child; } // up while (element != root && element != null) { T next = myWalker.getNextSibling(element); elementFinished(element); if (next != null) { Object nextPrev = myWalker.getPrevSibling(next); if (nextPrev != element) { String msg = "Element: " + element + "; next: " + next + "; next.prev: " + nextPrev; while (true) { T top = myWalker.getParent(element); if (top == null || top == root) break; element = top; } assert false : msg + " Top:" + element; } return next; } element = myWalker.getParent(element); } if (element != null) { elementFinished(element); } return null; }