/// <summary> /// Replaces text. /// </summary> /// <param name="offset">The starting offset of the text to be replaced.</param> /// <param name="length">The length of the text to be replaced.</param> /// <param name="text">The new text.</param> public void Replace(int offset, int length, string text) { if (text == null) { throw new ArgumentNullException("text"); } var textSource = new StringTextSource(text); // Ensure that all changes take place inside an update group. // Will also take care of throwing an exception if inDocumentChanging is set. BeginUpdate(); try { // protect document change against corruption by other changes inside the event handlers inDocumentChanging = true; try { // The range verification must wait until after the BeginUpdate() call because the document // might be modified inside the UpdateStarted event. ThrowIfRangeInvalid(offset, length); DoReplace(offset, length, textSource); } finally { inDocumentChanging = false; } } finally { EndUpdate(); } }
void DoReplace(int offset, int length, ITextSource newText) { if (length == 0 && newText.TextLength == 0) { return; } ITextSource removedText; if (length == 0) { removedText = StringTextSource.Empty; } else if (length < 100) { removedText = new StringTextSource(rope.ToString(offset, length)); } else { // use a rope if the removed string is long removedText = new RopeTextSource(rope.GetRange(offset, length)); } cachedText = null; // reset cache of complete document text fireTextChanged = true; lock (lockObject) { // now update the textBuffer and lineTree if (offset == 0 && length == rope.Length) { // optimize replacing the whole document rope.Clear(); var newRopeTextSource = newText as RopeTextSource; if (newRopeTextSource != null) { rope.InsertRange(0, newRopeTextSource.GetRope()); } else { rope.InsertText(0, newText.Text); } lineManager.Rebuild(); } else { rope.RemoveRange(offset, length); lineManager.Remove(offset, length); #if DEBUG lineTree.CheckProperties(); #endif var newRopeTextSource = newText as RopeTextSource; if (newRopeTextSource != null) { rope.InsertRange(offset, newRopeTextSource.GetRope()); } else { rope.InsertText(offset, newText.Text); } lineManager.Insert(offset, newText); #if DEBUG lineTree.CheckProperties(); #endif } } }
void DoReplace(int offset, int length, ITextSource newText) { if (length == 0 && newText.TextLength == 0) return; ITextSource removedText; if (length == 0) { removedText = StringTextSource.Empty; } else if (length < 100) { removedText = new StringTextSource(rope.ToString(offset, length)); } else { // use a rope if the removed string is long removedText = new RopeTextSource(rope.GetRange(offset, length)); } cachedText = null; // reset cache of complete document text fireTextChanged = true; lock (lockObject) { // now update the textBuffer and lineTree if (offset == 0 && length == rope.Length) { // optimize replacing the whole document rope.Clear(); var newRopeTextSource = newText as RopeTextSource; if (newRopeTextSource != null) rope.InsertRange(0, newRopeTextSource.GetRope()); else rope.InsertText(0, newText.Text); lineManager.Rebuild(); } else { rope.RemoveRange(offset, length); lineManager.Remove(offset, length); #if DEBUG lineTree.CheckProperties(); #endif var newRopeTextSource = newText as RopeTextSource; if (newRopeTextSource != null) rope.InsertRange(offset, newRopeTextSource.GetRope()); else rope.InsertText(offset, newText.Text); lineManager.Insert(offset, newText); #if DEBUG lineTree.CheckProperties(); #endif } } }
/// <summary> /// Replaces text. /// </summary> /// <param name="offset">The starting offset of the text to be replaced.</param> /// <param name="length">The length of the text to be replaced.</param> /// <param name="text">The new text.</param> public void Replace(int offset, int length, string text) { if (text == null) throw new ArgumentNullException("text"); var textSource = new StringTextSource(text); // Ensure that all changes take place inside an update group. // Will also take care of throwing an exception if inDocumentChanging is set. BeginUpdate(); try { // protect document change against corruption by other changes inside the event handlers inDocumentChanging = true; try { // The range verification must wait until after the BeginUpdate() call because the document // might be modified inside the UpdateStarted event. ThrowIfRangeInvalid(offset, length); DoReplace(offset, length, textSource); } finally { inDocumentChanging = false; } } finally { EndUpdate(); } }