public void InsertSliceWithChanges(int position, TextWithChanges slice, bool sendToEditor) { if (_editor != null && sendToEditor) { CowList<CharWithPosition> sliceText = slice.GetCurrentSlice(0, slice.GetActualLength()); StringBuilder sb = new StringBuilder(); foreach (CharWithPosition cwp in sliceText) { sb.Append(cwp.Char); } _editor.InsertText(position, sb.ToString(), true); } int oldPosition = ConvertActualPositionToOld(position); CowList<TextChange> newChanges = new CowList<TextChange>(); // Select changes before the insert position. int i; for (i = 0; i < _changes.Count; i++) { if (_changes[i].Position < oldPosition) { newChanges.Add(_changes[i]); } else { break; } } // Add changes from the inserted slice. foreach (TextChange change in slice._changes) { newChanges.Add(change.Moved(oldPosition)); } // Add changes that were after the insert position. for (; i < _changes.Count; i++) { newChanges.Add(_changes[i].Moved(slice._text.Length)); } _changes = newChanges; _text = _text.Substring(0, oldPosition) + slice._text + _text.Substring(oldPosition); }