@var{node}. It returns @code{nil} if there is no @var{n}'th child, or
the @var{n}'th child doesn't have a field name.
-Note that @var{n} counts both named and anonymous children, and
-@var{n} can be negative, e.g., @minus{}1 represents the last child.
+Note that @var{n} counts named nodes only, and @var{n} can be
+negative, e.g., @minus{}1 represents the last child.
@end defun
@defun treesit-node-child-count node &optional named
"Return the index of NODE in its parent.
If NAMED is non-nil, count named child only."
(let ((count 0))
+ ;; TODO: Use next-sibling as it's more efficient.
(while (setq node (treesit-node-prev-sibling node named))
(cl-incf count))
count))
(defun treesit-node-field-name (node)
"Return the field name of NODE as a child of its parent."
(when-let ((parent (treesit-node-parent node))
- (idx (treesit-node-index node)))
+ (idx (treesit-node-index node t)))
(treesit-node-field-name-for-child parent idx)))
;;; Query API supplement
Return nil if there's no Nth child, or if it has no field.
If NODE is nil, return nil.
-N counts all children, i.e., named ones and anonymous ones.
-
-N could be negative, e.g., -1 represents the last child. */)
+Note that N counts named nodes only. Also, N could be negative, e.g.,
+-1 represents the last child. */)
(Lisp_Object node, Lisp_Object n)
{
if (NILP (node))
/* Process negative index. */
if (idx < 0)
- idx = ts_node_child_count (treesit_node) + idx;
+ idx = ts_node_named_child_count (treesit_node) + idx;
if (idx < 0)
return Qnil;
if (idx > UINT32_MAX)