List<TextRange> (@NotNull List<TextRange> allChangedRanges) { if (allChangedRanges.isEmpty()) return allChangedRanges; List<TextRange> sorted = ContainerUtil.sorted(allChangedRanges, Segment.BY_START_OFFSET_THEN_END_OFFSET); List<TextRange> result = new SmartList<>(); TextRange prev = sorted.get(0); for (TextRange next : sorted) { if (next.getStartOffset() <= prev.getEndOffset()) { int newEndOffset = Math.max(prev.getEndOffset(), next.getEndOffset()); prev = new TextRange(prev.getStartOffset(), newEndOffset); } else { result.add(prev); prev = next; } } result.add(prev); return result; }