TextRange (@NotNull Document document, int startOffset, int endOffset) { int textLength = document.getTextLength(); int lineStartOffset = startOffset <= 0 ? 0 : startOffset > textLength ? textLength : document.getLineStartOffset(document.getLineNumber(startOffset)); int lineEndOffset = endOffset <= 0 ? 0 : endOffset >= textLength ? textLength : document.getLineEndOffset(document.getLineNumber(endOffset)); return new ProperTextRange(lineStartOffset, lineEndOffset); }