int (int offset, boolean lookForward) { int textLength = myDocument.getTextLength(); if (textLength == 0 || offset < 0 || offset > textLength) return -1; int line = myDocument.getLineNumber(offset); LineLayout layout = myTextLayoutCache.getLineLayout(line); int lineStartOffset = myDocument.getLineStartOffset(line); int relativeOffset = layout.findNearestDirectionBoundary(offset - lineStartOffset, lookForward); return relativeOffset < 0 ? -1 : lineStartOffset + relativeOffset; }