int (@NotNull PsiFile file, final @NotNull ASTNode element, boolean includeNonSpace, int recursionLevel) { if (recursionLevel > TOO_BIG_WALK_THRESHOLD) return 0; if (element.getTreePrev() != null) { ASTNode prev = element.getTreePrev(); ASTNode lastCompositePrev; while (prev instanceof CompositeElement && !TreeUtil.isStrongWhitespaceHolder(prev.getElementType())) { lastCompositePrev = prev; prev = prev.getLastChildNode(); if (prev == null) { // element.prev is "empty composite" return getIndentInner(file, lastCompositePrev, includeNonSpace, recursionLevel + 1); } } String text = prev.getText(); int index = Math.max(text.lastIndexOf('\n'), text.lastIndexOf('\r')); if (index >= 0) { return getIndent(file, text.substring(index + 1), includeNonSpace); } if (includeNonSpace) { return getIndentInner(file, prev, true, recursionLevel + 1) + getIndent(file, text, true); } ASTNode parent = prev.getTreeParent(); ASTNode child = prev; while (parent != null) { if (child.getTreePrev() != null) break; child = parent; parent = parent.getTreeParent(); } if (parent == null) { return getIndent(file, text, false); } else { return getIndentInner(file, prev, false, recursionLevel + 1); } } else { if (element.getTreeParent() == null) { return 0; } return getIndentInner(file, element.getTreeParent(), includeNonSpace, recursionLevel + 1); } }