TextRange (@NotNull TextRange textRange, @NotNull Document document) { int startLine = document.getLineNumber(textRange.getStartOffset()); int endLine = document.getLineNumber(Math.min(textRange.getEndOffset(), document.getTextLength())); return new TextRange(document.getLineStartOffset(startLine), document.getLineEndOffset(endLine)); }