Iterable<TextRange> (@NotNull TextRange original, @NotNull List<? extends TextRange> excludedRanges) { if (!excludedRanges.isEmpty()) { if (excludedRanges.size() > 1) { excludedRanges.sort(RANGE_COMPARATOR); } int enabledRangeStart = original.getStartOffset(); List<TextRange> enabledRanges = new ArrayList<>(); for (TextRange excludedRange : excludedRanges) { if (excludedRange.getEndOffset() < enabledRangeStart) continue; int excludedRangeStart = excludedRange.getStartOffset(); if (excludedRangeStart > original.getEndOffset()) break; if (excludedRangeStart > enabledRangeStart) { enabledRanges.add(new TextRange(enabledRangeStart, excludedRangeStart)); } enabledRangeStart = excludedRange.getEndOffset(); } if (enabledRangeStart < original.getEndOffset()) { enabledRanges.add(new TextRange(enabledRangeStart, original.getEndOffset())); } return enabledRanges; } return Collections.singletonList(original); }