LeafElement (int offset) { TreeElement element = this; if (element.getTreeParent() == null && offset >= element.getTextLength()) return null; startFind: while (true) { TreeElement child = element.getFirstChildNode(); TreeElement lastChild = element.getLastChildNode(); int elementTextLength = element.getTextLength(); boolean fwd = lastChild == null || elementTextLength / 2 > offset; if (!fwd) { child = lastChild; offset = elementTextLength - offset; } while (child != null) { int textLength = child.getTextLength(); if (textLength > offset || !fwd && textLength >= offset) { if (child instanceof LeafElement) { if (child instanceof ForeignLeafPsiElement) { child = fwd ? child.getTreeNext() : child.getTreePrev(); continue; } return (LeafElement)child; } offset = fwd ? offset : textLength - offset; element = child; continue startFind; } offset -= textLength; child = fwd ? child.getTreeNext() : child.getTreePrev(); } return null; } }