private void cutChangesIfNecessary(DocumentEvent event,StringBuilder oldText){
  if (myChanges.isEmpty()) {
    return;
  }
  int start=event.getOffset();
  int end=event.getOffset() + event.getOldLength();
  int diff=event.getNewLength() - event.getOldLength();
  int forwardIndex=findIndex(start);
  int backwardIndexStart=forwardIndex - 1;
  if (forwardIndex < 0) {
    backwardIndexStart=myChanges.size() - 1;
  }
 else {
    TIntArrayList indices=new TIntArrayList();
    for (; forwardIndex < myChanges.size(); forwardIndex++) {
      TextChangeImpl change=myChanges.get(forwardIndex);
      if (change.getStart() >= end) {
        change.advance(diff);
      }
 else       if (change.getEnd() <= end) {
        indices.add(forwardIndex);
        int deleteStart=start - change.getStart();
        deleteStart=Math.max(0,deleteStart);
        int deleteEnd=change.getEnd() - change.getStart();
        deleteEnd=Math.min(oldText.length(),Math.max(0,deleteEnd));
        oldText.delete(deleteStart,deleteEnd);
        oldText.insert(0,change.getText());
      }
 else {
        int deleteStart=change.getStart() - start;
        deleteStart=Math.min(oldText.length(),Math.max(0,deleteStart));
        int deleteEnd=oldText.length();
        deleteEnd=Math.min(oldText.length(),Math.max(0,deleteEnd));
        oldText.delete(deleteStart,deleteEnd);
        myChanges.set(forwardIndex,new TextChangeImpl(change.getText(),end + diff,change.getEnd() + diff));
      }
    }
    if (!indices.isEmpty()) {
      for (int i=indices.size() - 1; i >= 0; i--) {
        myChanges.remove(indices.get(i));
      }
    }
  }
  for (int i=Math.min(backwardIndexStart,myChanges.size() - 1); i >= 0; i--) {
    TextChangeImpl change=myChanges.get(i);
    if (change.getEnd() <= start) {
      break;
    }
    CharSequence textToUse=change.getText();
    int symbolsToCut=Math.min(change.getEnd(),end) - start;
    if (textToUse.length() >= symbolsToCut) {
      oldText.insert(symbolsToCut,textToUse.subSequence(textToUse.length() - symbolsToCut,textToUse.length()));
      textToUse=textToUse.subSequence(0,textToUse.length() - symbolsToCut);
    }
    oldText.delete(0,symbolsToCut);
    myChanges.set(i,new TextChangeImpl(textToUse,change.getStart(),start));
    if (change.getEnd() > end) {
      int shift=event.getOffset() + event.getNewLength() - end;
      TextChangeImpl changeTail=new TextChangeImpl("",end + shift,change.getEnd() + shift);
      if (i >= myChanges.size() - 1) {
        myChanges.add(changeTail);
      }
 else {
        myChanges.add(i + 1,changeTail);
      }
    }
  }
}

private void cutChangesIfNecessary ( DocumentEvent event , StringBuilder oldText ) { if ( myChanges . isEmpty ( ) ) { return ; } int start = event . getOffset ( ) ; int end = event . getOffset ( ) + event . getOldLength ( ) ; int diff = event . getNewLength ( ) - event . getOldLength ( ) ; int forwardIndex = findIndex ( start ) ; int backwardIndexStart = forwardIndex - 1 ; if ( forwardIndex < 0 ) { backwardIndexStart = myChanges . size ( ) - 1 ; } else { TIntArrayList indices = new TIntArrayList ( ) ; for ( ; forwardIndex < myChanges . size ( ) ; forwardIndex ++ ) { TextChangeImpl change = myChanges . get ( forwardIndex ) ; if ( change . getStart ( ) >= end ) { change . advance ( diff ) ; } else if ( change . getEnd ( ) <= end ) { indices . add ( forwardIndex ) ; int deleteStart = start - change . getStart ( ) ; deleteStart = Math . max ( 0 , deleteStart ) ; int deleteEnd = change . getEnd ( ) - change . getStart ( ) ; deleteEnd = Math . min ( oldText . length ( ) , Math . max ( 0 , deleteEnd ) ) ; oldText . delete ( deleteStart , deleteEnd ) ; oldText . insert ( 0 , change . getText ( ) ) ; } else { int deleteStart = change . getStart ( ) - start ; deleteStart = Math . min ( oldText . length ( ) , Math . max ( 0 , deleteStart ) ) ; int deleteEnd = oldText . length ( ) ; deleteEnd = Math . min ( oldText . length ( ) , Math . max ( 0 , deleteEnd ) ) ; oldText . delete ( deleteStart , deleteEnd ) ; myChanges . set ( forwardIndex , new TextChangeImpl ( change . getText ( ) , end + diff , change . getEnd ( ) + diff ) ) ; } } if ( ! indices . isEmpty ( ) ) { for ( int i = indices . size ( ) - 1 ; i >= 0 ; i -- ) { myChanges . remove ( indices . get ( i ) ) ; } } } for ( int i = Math . min ( backwardIndexStart , myChanges . size ( ) - 1 ) ; i >= 0 ; i -- ) { TextChangeImpl change = myChanges . get ( i ) ; if ( change . getEnd ( ) <= start ) { break ; } CharSequence textToUse = change . getText ( ) ; int symbolsToCut = Math . min ( change . getEnd ( ) , end ) - start ; if ( textToUse . length ( ) >= symbolsToCut ) { oldText . insert ( symbolsToCut , textToUse . subSequence ( textToUse . length ( ) - symbolsToCut , textToUse . length ( ) ) ) ; textToUse = textToUse . subSequence ( 0 , textToUse . length ( ) - symbolsToCut ) ; } oldText . delete ( 0 , symbolsToCut ) ; myChanges . set ( i , new TextChangeImpl ( textToUse , change . getStart ( ) , start ) ) ; if ( change . getEnd ( ) > end ) { int shift = event . getOffset ( ) + event . getNewLength ( ) - end ; TextChangeImpl changeTail = new TextChangeImpl ( "" , end + shift , change . getEnd ( ) + shift ) ; if ( i >= myChanges . size ( ) - 1 ) { myChanges . add ( changeTail ) ; } else { myChanges . add ( i + 1 , changeTail ) ; } } } }
cuts or removes stored change ranges for the given interval if any .
