void (@NotNull ModUpdateFileText.Fragment newFragment) { int insertionPoint = 0; int intersect = 0; for (int i = myFragments.size() - 1; i >= 0; i--) { ModUpdateFileText.Fragment fragment = myFragments.get(i); if (fragment.offset() > newFragment.offset() + newFragment.oldLength()) { myFragments.set(i, fragment.shift(newFragment.newLength() - newFragment.oldLength())); } else if (fragment.intersects(newFragment)) { intersect++; } else { insertionPoint = i + 1; break; } } List<ModUpdateFileText.Fragment> intersected = myFragments.subList(insertionPoint, insertionPoint + intersect); if (!intersected.isEmpty()) { ModUpdateFileText.Fragment first = intersected.get(0); ModUpdateFileText.Fragment last = intersected.get(intersected.size() - 1); int diff = intersected.stream().mapToInt(f -> f.newLength() - f.oldLength()).sum(); ModUpdateFileText.Fragment mergedFragment = new ModUpdateFileText.Fragment(first.offset(), last.offset() + last.newLength() - diff - first.offset(), last.offset() + last.newLength() - first.offset()); newFragment = mergedFragment.mergeWithNext(newFragment); } intersected.clear(); intersected.add(newFragment); }