The TextAnchor class references an offset (a position between two characters). It automatically updates the offset when text is inserted/removed in front of the anchor.

Use the Offset property to get the offset from a text anchor. Use the TextDocument.CreateAnchor method to create an anchor from an offset.

The document will automatically update all text anchors; and because it uses weak references to do so, the garbage collector can simply collect the anchor object when you don't need it anymore.

Moreover, the document is able to efficiently update a large number of anchors without having to look at each anchor object individually. Updating the offsets of all anchors usually only takes time logarithmic to the number of anchors. Retrieving the Offset property also runs in O(lg N).

If you want to track a segment, you can use the AnchorSegment class which implements ISegment using two text anchors.

Inheritance: ITextAnchor
Ejemplo n.º 1
0
		public TextAnchor CreateAnchor(int offset)
		{
			Log("CreateAnchor(" + offset + ")");
			var anchor = new TextAnchor(document);
			anchor.node = new TextAnchorNode(anchor);
			if (root == null)
			{
				// creating the first text anchor
				root = anchor.node;
				root.totalLength = root.length = offset;
			}
			else if (offset >= root.totalLength)
			{
				// append anchor at end of tree
				anchor.node.totalLength = anchor.node.length = offset - root.totalLength;
				InsertAsRight(root.RightMost, anchor.node);
			}
			else
			{
				// insert anchor in middle of tree
				var n = FindNode(ref offset);
				Debug.Assert(offset < n.length);
				// split segment 'n' at offset
				anchor.node.totalLength = anchor.node.length = offset;
				n.length -= offset;
				InsertBefore(n, anchor.node);
			}
			DeleteMarkedNodes();
			return anchor;
		}
Ejemplo n.º 2
0
		internal int totalLength; // totalLength = length + left.totalLength + right.totalLength

		public TextAnchorNode(TextAnchor anchor) : base(anchor)
		{
		}