void (PsiFile file) { file.accept(new PsiRecursiveElementWalkingVisitor() { @Override public void visitElement(@NotNull PsiElement element) { if (element instanceof ForeignLeafPsiElement) return; try { ensureNodeRangeConsistency(element, file); } catch (Throwable e) { throw new AssertionError("In " + element + " of " + element.getClass(), e); } super.visitElement(element); } private void ensureNodeRangeConsistency(PsiElement parent, PsiFile file) { int parentOffset = parent.getTextRange().getStartOffset(); int childOffset = 0; ASTNode child = parent.getNode().getFirstChildNode(); if (child != null) { while (child != null) { int childLength = checkChildRangeConsistency(file, parentOffset, childOffset, child); childOffset += childLength; child = child.getTreeNext(); } assertEquals(childOffset, parent.getTextLength()); } } private static int checkChildRangeConsistency(PsiFile file, int parentOffset, int childOffset, ASTNode child) { assertEquals(child.getStartOffsetInParent(), childOffset); assertEquals(child.getStartOffset(), childOffset + parentOffset); int childLength = child.getTextLength(); assertEquals(TextRange.from(childOffset + parentOffset, childLength), child.getTextRange()); if (!(child.getPsi() instanceof ForeignLeafPsiElement)) { assertEquals(child.getTextRange().substring(file.getText()), child.getText()); } return childLength; } }); }