예제 #1
0
 /// <summary>
 /// Updates the start and end offsets of all segments stored in this collection.
 /// </summary>
 /// <param name="change">OffsetChangeMapEntry instance describing the change to the document.</param>
 public void UpdateOffsets(OffsetChangeMapEntry change)
 {
     if (_isConnectedToDocument)
     {
         throw new InvalidOperationException("This TextSegmentCollection will automatically update offsets; do not call UpdateOffsets manually!");
     }
     UpdateOffsetsInternal(change);
     CheckProperties();
 }
예제 #2
0
 void ChangeDocument(OffsetChangeMapEntry change)
 {
     tree.UpdateOffsets(change);
     foreach (TestTextSegment s in expectedSegments)
     {
         int endOffset = s.ExpectedOffset + s.ExpectedLength;
         s.ExpectedOffset = change.GetNewOffset(s.ExpectedOffset, AnchorMovementType.AfterInsertion);
         s.ExpectedLength = Math.Max(0, change.GetNewOffset(endOffset, AnchorMovementType.BeforeInsertion) - s.ExpectedOffset);
     }
 }
예제 #3
0
 private void UpdateOffsetsInternal(OffsetChangeMapEntry change)
 {
     // Special case pure insertions, because they don't always cause a text segment to increase in size when the replaced region
     // is inside a segment (when offset is at start or end of a text semgent).
     if (change.RemovalLength == 0)
     {
         InsertText(change.Offset, change.InsertionLength);
     }
     else
     {
         ReplaceText(change);
     }
 }
예제 #4
0
        private void ReplaceText(OffsetChangeMapEntry change)
        {
            Debug.Assert(change.RemovalLength > 0);
            var offset = change.Offset;

            foreach (var segment in FindOverlappingSegments(offset, change.RemovalLength))
            {
                if (segment.StartOffset <= offset)
                {
                    if (segment.EndOffset >= offset + change.RemovalLength)
                    {
                        // Replacement inside segment: adjust segment length
                        segment.Length += change.InsertionLength - change.RemovalLength;
                    }
                    else
                    {
                        // Replacement starting inside segment and ending after segment end: set segment end to removal position
                        //segment.EndOffset = offset;
                        segment.Length = offset - segment.StartOffset;
                    }
                }
                else
                {
                    // Replacement starting in front of text segment and running into segment.
                    // Keep segment.EndOffset constant and move segment.StartOffset to the end of the replacement
                    var remainingLength = segment.EndOffset - (offset + change.RemovalLength);
                    RemoveSegment(segment);
                    segment.StartOffset = offset + change.RemovalLength;
                    segment.Length      = Math.Max(0, remainingLength);
                    AddSegment(segment);
                }
            }
            // move start offsets of all segments > offset
            TextSegment node = FindFirstSegmentWithStartAfter(offset + 1);

            if (node != null)
            {
                Debug.Assert(node.NodeLength >= change.RemovalLength);
                node.NodeLength += change.InsertionLength - change.RemovalLength;
                UpdateAugmentedData(node);
            }
        }
예제 #5
0
        public void HandleTextChange(OffsetChangeMapEntry entry, DelayedEvents delayedEvents)
        {
            //Log("HandleTextChange(" + entry + ")");
            if (entry.RemovalLength == 0)
            {
                // This is a pure insertion.
                // Unlike a replace with removal, a pure insertion can result in nodes at the same location
                // to split depending on their MovementType.
                // Thus, we handle this case on a separate code path
                // (the code below looks like it does something similar, but it can only split
                // the set of deletion survivors, not all nodes at an offset)
                InsertText(entry.Offset, entry.InsertionLength, entry.DefaultAnchorMovementIsBeforeInsertion);
                return;
            }
            // When handling a replacing text change, we need to:
            // - find all anchors in the deleted segment and delete them / move them to the appropriate
            //   surviving side.
            // - adjust the segment size between the left and right side

            int offset = entry.Offset;
            int remainingRemovalLength = entry.RemovalLength;

            // if the text change is happening after the last anchor, we don't have to do anything
            if (_root == null || offset >= _root.TotalLength)
            {
                return;
            }
            TextAnchorNode node = FindNode(ref offset);
            TextAnchorNode firstDeletionSurvivor = null;

            // go forward through the tree and delete all nodes in the removal segment
            while (node != null && offset + remainingRemovalLength > node.Length)
            {
                TextAnchor anchor = (TextAnchor)node.Target;
                if (anchor != null && (anchor.SurviveDeletion || entry.RemovalNeverCausesAnchorDeletion))
                {
                    if (firstDeletionSurvivor == null)
                    {
                        firstDeletionSurvivor = node;
                    }
                    // This node should be deleted, but it wants to survive.
                    // We'll just remove the deleted length segment, so the node will be positioned
                    // in front of the removed segment.
                    remainingRemovalLength -= node.Length - offset;
                    node.Length             = offset;
                    offset = 0;
                    UpdateAugmentedData(node);
                    node = node.Successor;
                }
                else
                {
                    // delete node
                    TextAnchorNode s = node.Successor;
                    remainingRemovalLength -= node.Length;
                    RemoveNode(node);
                    // we already deleted the node, don't delete it twice
                    _nodesToDelete.Remove(node);
                    anchor?.OnDeleted(delayedEvents);
                    node = s;
                }
            }
            // 'node' now is the first anchor after the deleted segment.
            // If there are no anchors after the deleted segment, 'node' is null.

            // firstDeletionSurvivor was set to the first node surviving deletion.
            // Because all non-surviving nodes up to 'node' were deleted, the node range
            // [firstDeletionSurvivor, node) now refers to the set of all deletion survivors.

            // do the remaining job of the removal
            if (node != null)
            {
                node.Length -= remainingRemovalLength;
                Debug.Assert(node.Length >= 0);
            }
            if (entry.InsertionLength > 0)
            {
                // we are performing a replacement
                if (firstDeletionSurvivor != null)
                {
                    // We got deletion survivors which need to be split into BeforeInsertion
                    // and AfterInsertion groups.
                    // Take care that we don't regroup everything at offset, but only the deletion
                    // survivors - from firstDeletionSurvivor (inclusive) to node (exclusive).
                    // This ensures that nodes immediately before or after the replaced segment
                    // stay where they are (independent from their MovementType)
                    PerformInsertText(firstDeletionSurvivor, node, entry.InsertionLength, entry.DefaultAnchorMovementIsBeforeInsertion);
                }
                else if (node != null)
                {
                    // No deletion survivors:
                    // just perform the insertion
                    node.Length += entry.InsertionLength;
                }
            }
            if (node != null)
            {
                UpdateAugmentedData(node);
            }
            DeleteMarkedNodes();
        }