AvalonStudio.TextEditor.Document.TextAnchorTree.FindNode C# (CSharp) Method

FindNode() private method

Finds the node at the specified offset. After the method has run, offset is relative to the beginning of the returned node.
private FindNode ( int &offset ) : TextAnchorNode
offset int
return TextAnchorNode
		private TextAnchorNode FindNode(ref int offset)
		{
			var n = root;
			while (true)
			{
				if (n.left != null)
				{
					if (offset < n.left.totalLength)
					{
						n = n.left; // descend into left subtree
						continue;
					}
					offset -= n.left.totalLength; // skip left subtree
				}
				if (!n.IsAlive)
					MarkNodeForDelete(n);
				if (offset < n.length)
				{
					return n; // found correct node
				}
				offset -= n.length; // skip this node
				if (n.right != null)
				{
					n = n.right; // descend into right subtree
				}
				else
				{
					// didn't find any node containing the offset
					return null;
				}
			}
		}