/// <summary> Constructor. </summary> /// <param name="node"> The node for which this instance wraps. </param> internal Markup(MarkupNodeReference node) { _node = node; // force the absolute index to be recalculated on demand _changeIndex = node.Owner.ChangeIndex.Previous(); _absoluteIndex = 0; }
/// <summary> Marks the given range to have the given markup and data. </summary> /// <exception cref="ArgumentOutOfRangeException"> Thrown when one or more arguments are outside /// the required range. </exception> /// <param name="range"> The range of text to mark. </param> /// <param name="descriptor"> The type of markup to apply to the range. </param> /// <param name="markupData"> The markup data to store with the markup. </param> public Markup MarkRange(Range range, IMarkupDescriptor descriptor, IMarkupData markupData) { if (range.StartIndex < 0) { throw new ArgumentOutOfRangeException(nameof(range)); } if (range.EndIndex > _owner.Length) { throw new ArgumentOutOfRangeException(nameof(range)); } var newFragment = new MarkupNodeReference(this, descriptor, markupData) { Length = range.Length }; int previousNodeAbsoluteIndex; var previousNode = GetClosestNodeBefore(range.StartIndex, out previousNodeAbsoluteIndex); LinkedListNode <MarkupNodeReference> node; if (previousNode == null) { var nextNode = _subFragments.First; if (nextNode != null) { // because we're at the beginning, the offset of the next fragment was an absolute index, thus // we now convert it to a relative index by offsetting our start index. nextNode.Value.RelativeOffsetSinceLastNode -= range.StartIndex; } node = _subFragments.AddFirst(newFragment); newFragment.RelativeOffsetSinceLastNode = range.StartIndex; } else { var nextNode = previousNode.Next; // fix up the next node's relative offset if need be if (nextNode != null) { var nextFragment = nextNode.Value; int absoluteIndexOfNextNode = nextFragment.RelativeOffsetSinceLastNode + previousNodeAbsoluteIndex; nextFragment.RelativeOffsetSinceLastNode = absoluteIndexOfNextNode - range.StartIndex; } newFragment.RelativeOffsetSinceLastNode = range.StartIndex - previousNodeAbsoluteIndex; node = _subFragments.AddAfter(previousNode, newFragment); } newFragment.Node = node; ChangeIndex = ChangeIndex.Next(); return(newFragment.Markup); }
/// <summary> Updates the collection based on the given text-changed event. </summary> /// <param name="changeEvent"> The event for which the collection of entries should be updated. </param> internal void UpdateFromEvent(RangeModification changeEvent) { if (changeEvent.WasAdded) { UpdateFromInsert(changeEvent); } else { UpdateFromDelete(changeEvent); } // todo update dirty things ChangeIndex = ChangeIndex.Next(); }
/// <summary> Sets the absolute index of the given instance. </summary> /// <param name="absoluteIndex"> The current absolute index of the node. </param> internal void SetAbsoluteIndex(int absoluteIndex) { _absoluteIndex = absoluteIndex; _changeIndex = _node.Owner.ChangeIndex; }