Function: treesit-node-child

treesit-node-child is a function defined in treesit.c.

Signature

(treesit-node-child NODE N &optional NAMED)

Documentation

Return the Nth child of NODE.

Return nil if there is no Nth child. If NAMED is non-nil, look for named child only. NAMED defaults to nil. If NODE is nil, return nil.

N could be negative, e.g., -1 represents the last child.

Other relevant functions are documented in the treesit group.

View in manual

Shortdoc

;; treesit
(treesit-node-child node 0)
    e.g. => #<treesit-node (primitive_type) in 1-4>

Source Code

// Defined in /usr/src/emacs/src/treesit.c
{
  if (NILP (node)) return Qnil;
  treesit_check_node (node);
  CHECK_INTEGER (n);
  EMACS_INT idx = XFIXNUM (n);

  treesit_initialize ();

  TSNode treesit_node = XTS_NODE (node)->node;
  TSNode child;

  /* Process negative index.  */
  if (idx < 0)
    {
      if (NILP (named))
	idx = ts_node_child_count (treesit_node) + idx;
      else
	idx = ts_node_named_child_count (treesit_node) + idx;
    }
  if (idx < 0)
    return Qnil;
  if (idx > UINT32_MAX)
    xsignal1 (Qargs_out_of_range, n);

  if (NILP (named))
    child = ts_node_child (treesit_node, (uint32_t) idx);
  else
    child = ts_node_named_child (treesit_node, (uint32_t) idx);

  if (ts_node_is_null (child))
    return Qnil;

  return make_treesit_node (XTS_NODE (node)->parser, child);
}