Example #1
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);
					if (anchor != null)
						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();
		}
Example #2
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)
		{
			UpdateOffsets(change.GetNewOffset);
		}
Example #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, string 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.Length, false, true)));
					break;
				case OffsetChangeMappingType.RemoveAndInsert:
					if (length == 0 || text.Length == 0) {
						// only insertion or only removal?
						// OffsetChangeMappingType doesn't matter, just use Normal.
						Replace(offset, length, text, null);
					} else {
						OffsetChangeMap map = new OffsetChangeMap(2);
						map.Add(new OffsetChangeMapEntry(offset, length, 0));
						map.Add(new OffsetChangeMapEntry(offset, 0, text.Length));
						map.Freeze();
						Replace(offset, length, text, map);
					}
					break;
				case OffsetChangeMappingType.CharacterReplace:
					if (length == 0 || text.Length == 0) {
						// only insertion or only removal?
						// OffsetChangeMappingType doesn't matter, just use Normal.
						Replace(offset, length, text, null);
					} else if (text.Length > length) {
						// look at OffsetChangeMappingType.CharacterReplace XML comments on why we need to replace
						// the last character
						OffsetChangeMapEntry entry = new OffsetChangeMapEntry(offset + length - 1, 1, 1 + text.Length - length);
						Replace(offset, length, text, OffsetChangeMap.FromSingleElement(entry));
					} else if (text.Length < length) {
						OffsetChangeMapEntry entry = new OffsetChangeMapEntry(offset + text.Length, length - text.Length, 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");
			}
		}
 /// <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)
 {
     UpdateOffsets(change.GetNewOffset);
 }
Example #5
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);
     }
 }