示例#1
0
 /// <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>
 /// <param name="offsetChangeMap">
 ///     The offsetChangeMap determines how offsets inside the old text are mapped to the new text.
 ///     This affects how the anchors and segments inside the replaced region behave.
 ///     If you pass null (the default when using one of the other overloads), the offsets are changed as
 ///     in OffsetChangeMappingType.Normal mode.
 ///     If you pass OffsetChangeMap.Empty, then everything will stay in its old place
 ///     (OffsetChangeMappingType.CharacterReplace mode).
 ///     The offsetChangeMap must be a valid 'explanation' for the document change. See
 ///     <see cref="OffsetChangeMap.IsValidForDocumentChange" />.
 ///     Passing an OffsetChangeMap to the Replace method will automatically freeze it to ensure the thread safety of the
 ///     resulting
 ///     DocumentChangeEventArgs instance.
 /// </param>
 public void Replace(int offset, int length, string text, OffsetChangeMap offsetChangeMap)
 {
     Replace(offset, length, new StringTextSource(text), offsetChangeMap);
 }
示例#2
0
        private 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));
            }
            var 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;
            var 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 (var 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);
            }
        }
示例#3
0
        /// <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>
        /// <param name="offsetChangeMappingType">
        ///     The offsetChangeMappingType determines how offsets inside the old text are mapped to the new text.
        ///     This affects how the anchors and segments inside the replaced region behave.
        /// </param>
        public void Replace(int offset, int length, ITextSource text, OffsetChangeMappingType offsetChangeMappingType)
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            // Please see OffsetChangeMappingType XML comments for details on how these modes work.
            switch (offsetChangeMappingType)
            {
            case OffsetChangeMappingType.Normal:
                Replace(offset, length, text, null);
                break;

            case OffsetChangeMappingType.KeepAnchorBeforeInsertion:
                Replace(offset, length, text, OffsetChangeMap.FromSingleElement(
                            new OffsetChangeMapEntry(offset, length, text.TextLength, false, true)));
                break;

            case OffsetChangeMappingType.RemoveAndInsert:
                if (length == 0 || text.TextLength == 0)
                {
                    // only insertion or only removal?
                    // OffsetChangeMappingType doesn't matter, just use Normal.
                    Replace(offset, length, text, null);
                }
                else
                {
                    var map = new OffsetChangeMap(2);
                    map.Add(new OffsetChangeMapEntry(offset, length, 0));
                    map.Add(new OffsetChangeMapEntry(offset, 0, text.TextLength));
                    map.Freeze();
                    Replace(offset, length, text, map);
                }
                break;

            case OffsetChangeMappingType.CharacterReplace:
                if (length == 0 || text.TextLength == 0)
                {
                    // only insertion or only removal?
                    // OffsetChangeMappingType doesn't matter, just use Normal.
                    Replace(offset, length, text, null);
                }
                else if (text.TextLength > length)
                {
                    // look at OffsetChangeMappingType.CharacterReplace XML comments on why we need to replace
                    // the last character
                    var entry = new OffsetChangeMapEntry(offset + length - 1, 1, 1 + text.TextLength - length);
                    Replace(offset, length, text, OffsetChangeMap.FromSingleElement(entry));
                }
                else if (text.TextLength < length)
                {
                    var entry = new OffsetChangeMapEntry(offset + text.TextLength, length - text.TextLength, 0, true, false);
                    Replace(offset, length, text, OffsetChangeMap.FromSingleElement(entry));
                }
                else
                {
                    Replace(offset, length, text, OffsetChangeMap.Empty);
                }
                break;

            default:
                throw new ArgumentOutOfRangeException("offsetChangeMappingType", offsetChangeMappingType, "Invalid enum value");
            }
        }