int (int offset, @NotNull Document document) { if (offset < 0 || offset > document.getTextLength()) { return offset; } int lineNumber = document.getLineNumber(offset); return document.getLineStartOffset(lineNumber); }