void (@NotNull CharSequence generatedText) { int shift = 0; ListIterator<TextRange> iterator = myOuterAndRemoveRanges.listIterator(); while (iterator.hasNext()) { TextRange range = iterator.next(); if (range instanceof RangeToRemove) { CharSequence insertedString = generatedText.subSequence(range.getStartOffset() - shift, range.getEndOffset() - shift); iterator.set(new RangeToRemove(range.getStartOffset(), insertedString)); // only add insertedString shift -= range.getLength(); } else { shift += range.getLength(); } } }