TextRange (@NotNull TextRange range) { int startOffset = range.getStartOffset(); int endOffset = range.getEndOffset(); final int docLength = myDocument.getTextLength(); if (endOffset > docLength) { LOG.warn("The given range " + endOffset + " exceeds the document length " + docLength); return new TextRange(Math.min(startOffset, docLength), docLength); } return range; }