RangeMarker (@Nullable RangeMarker old, @NotNull TextRange scope, int textLength, @NotNull Document document) { if (scope.equalsToRange(0, textLength)) return WHOLE_FILE_DIRTY_MARKER; if (old == null) { return document.createRangeMarker(scope); } if (old == WHOLE_FILE_DIRTY_MARKER) return old; TextRange oldRange = old.getTextRange(); TextRange union = scope.union(oldRange); if (old.isValid() && union.equals(oldRange)) { return old; } if (union.getEndOffset() > textLength) { union = union.intersection(new TextRange(0, textLength)); } assert union != null; return document.createRangeMarker(union); }