Segment (@NotNull Segment range) { int originalStart = range.getStartOffset(); DocumentEx document = myEditor.getDocument(); int start = DocumentUtil.getLineStartOffset(originalStart, document); if (start < originalStart) { range = new TextRange(start, range.getEndOffset()); } int originalEnd = range.getEndOffset(); int end = DocumentUtil.getLineEndOffset(originalEnd, document); if (end >= originalEnd) { range = new TextRange(range.getStartOffset(), end < document.getTextLength() ? end + 1 : end); } return range; }