public void Replace(int offset, int count, string value, ICSharpCode.NRefactory.Editor.AnchorMovementType anchorMovementType = AnchorMovementType.Default) { if (offset < 0) { throw new ArgumentOutOfRangeException("offset", "must be > 0, was: " + offset); } if (offset > TextLength) { throw new ArgumentOutOfRangeException("offset", "must be <= Length, was: " + offset); } if (count < 0) { throw new ArgumentOutOfRangeException("count", "must be > 0, was: " + count); } //InterruptFoldWorker(); //int oldLineCount = LineCount; var args = new DocumentChangeEventArgs(offset, count > 0 ? GetTextAt(offset, count) : "", value, anchorMovementType); OnTextReplacing(args); value = args.InsertedText.Text; /*UndoOperation operation = null; * if (!isInUndo) * { * operation = new UndoOperation(args); * if (currentAtomicOperation != null) * { * currentAtomicOperation.Add(operation); * } * else * { * OnBeginUndo(); * undoStack.Push(operation); * OnEndUndo(new UndoOperationEventArgs(operation)); * } * redoStack.Clear(); * }*/ buffer.Replace(offset, count, value); //foldSegmentTree.UpdateOnTextReplace(this, args); splitter.TextReplaced(this, args); versionProvider.AppendChange(args); OnTextReplaced(args); }
public static void Run(ITextSource originalXmlFile) { int seed; lock (sharedRnd) { seed = sharedRnd.Next(); } Console.WriteLine(seed); Random rnd = new Random(seed); AXmlParser parser = new AXmlParser(); StringBuilder b = new StringBuilder(originalXmlFile.Text); IncrementalParserState parserState = null; var versionProvider = new TextSourceVersionProvider(); int totalCharactersParsed = 0; int totalCharactersChanged = originalXmlFile.TextLength; TimeSpan incrementalParseTime = TimeSpan.Zero; TimeSpan nonIncrementalParseTime = TimeSpan.Zero; Stopwatch w = new Stopwatch(); for (int iteration = 0; iteration < 100; iteration++) { totalCharactersParsed += b.Length; var textSource = new StringTextSource(b.ToString(), versionProvider.CurrentVersion); w.Restart(); var incrementalResult = parser.ParseIncremental(parserState, textSource, out parserState); w.Stop(); incrementalParseTime += w.Elapsed; w.Restart(); var nonIncrementalResult = parser.Parse(textSource); w.Stop(); nonIncrementalParseTime += w.Elapsed; CompareResults(incrementalResult, nonIncrementalResult); incrementalResult.AcceptVisitor(new ValidationVisitor(textSource)); // Randomly mutate the file: List <TextChangeEventArgs> changes = new List <TextChangeEventArgs>(); int modifications = rnd.Next(0, 25); int offset = 0; for (int i = 0; i < modifications; i++) { if (i == 0 || rnd.Next(0, 10) == 0) { offset = rnd.Next(0, b.Length); } else { offset += rnd.Next(0, Math.Min(10, b.Length - offset)); } int originalOffset = rnd.Next(0, originalXmlFile.TextLength); int insertionLength; int removalLength; switch (rnd.Next(0, 21) / 10) { case 0: removalLength = 0; insertionLength = rnd.Next(0, Math.Min(50, originalXmlFile.TextLength - originalOffset)); break; case 1: removalLength = rnd.Next(0, Math.Min(20, b.Length - offset)); insertionLength = rnd.Next(0, Math.Min(20, originalXmlFile.TextLength - originalOffset)); break; default: removalLength = rnd.Next(0, b.Length - offset); insertionLength = rnd.Next(0, originalXmlFile.TextLength - originalOffset); break; } string removedText = b.ToString(offset, removalLength); b.Remove(offset, removalLength); string insertedText = originalXmlFile.GetText(originalOffset, insertionLength); b.Insert(offset, insertedText); versionProvider.AppendChange(new TextChangeEventArgs(offset, removedText, insertedText)); totalCharactersChanged += insertionLength; } } Console.WriteLine("Incremental parse time: " + incrementalParseTime + " for " + totalCharactersChanged + " characters changed"); Console.WriteLine("Non-Incremental parse time: " + nonIncrementalParseTime + " for " + totalCharactersParsed + " characters"); }
void DoReplace(int offset, int length, ITextSource newText, OffsetChangeMap offsetChangeMap) { if (length == 0 && newText.TextLength == 0) { return; } // trying to replace a single character in 'Normal' mode? // for single characters, 'CharacterReplace' mode is equivalent, but more performant // (we don't have to touch the anchorTree at all in 'CharacterReplace' mode) if (length == 1 && newText.TextLength == 1 && offsetChangeMap == null) { offsetChangeMap = OffsetChangeMap.Empty; } 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)); } DocumentChangeEventArgs args = new DocumentChangeEventArgs(offset, removedText, newText, offsetChangeMap); // fire DocumentChanging event if (Changing != null) { Changing(this, args); } if (textChanging != null) { textChanging(this, args); } undoStack.Push(this, args); cachedText = null; // reset cache of complete document text fireTextChanged = true; DelayedEvents delayedEvents = new DelayedEvents(); lock (lockObject) { // create linked list of checkpoints versionProvider.AppendChange(args); // 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 } } // update text anchors if (offsetChangeMap == null) { anchorTree.HandleTextChange(args.CreateSingleChangeMapEntry(), delayedEvents); } else { foreach (OffsetChangeMapEntry entry in offsetChangeMap) { anchorTree.HandleTextChange(entry, delayedEvents); } } lineManager.ChangeComplete(args); // raise delayed events after our data structures are consistent again delayedEvents.RaiseEvents(); // fire DocumentChanged event if (Changed != null) { Changed(this, args); } if (textChanged != null) { textChanged(this, args); } }