public static ITreePath <ITextNode> CreatePathForNodeAt(this ITextNode node, int offset) { if (!node.InRange(offset)) { throw new ArgumentOutOfRangeException(nameof(offset), offset, null); } ITreePath <ITextNode> path = new TreePath <ITextNode>(node); for (var idx = 0; idx < node.Count;) { var child = node[idx]; if (child.InRange(offset)) { path = path.Append(child); // restart for-loop for the new node .. // this is a stack-friendly tail recursion. node = child; idx = 0; } else { idx += 1; } } return(path); }
public static int NodeIndexForOffset(this ITextNode node, int offset) { if (node.Leaf) { return(-1); } if (!node.InRange(offset)) { return(-1); } for (var idx = 0; idx < node.Count; idx += 1) { var n = node[idx]; if (n.InRange(offset)) { return(idx); } } return(-1); }