Function: treesit-node-parent

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

Signature

(treesit-node-parent NODE)

Documentation

Return the immediate parent of NODE.

Return nil if NODE has no parent. If NODE is nil, return nil.

Other relevant functions are documented in the treesit group.

View in manual

Shortdoc

;; treesit
(treesit-node-parent node)
    e.g. => #<treesit-node (declaration) in 1-11>

Source Code

// Defined in /usr/src/emacs/src/treesit.c
{
  if (NILP (node)) return Qnil;
  treesit_check_node (node);
  treesit_initialize ();

  Lisp_Object return_value = Qnil;

  TSNode treesit_node = XTS_NODE (node)->node;
  Lisp_Object parser = XTS_NODE (node)->parser;
  TSTreeCursor cursor;
  /* See the comments to treesit_cursor_helper about the algorithm for
     finding the parent node.  The complexity is roughly proportional
     to the square root of the current node's depth in the parse tree,
     and we punt if the tree is too deep.  */
  if (!treesit_cursor_helper (&cursor, treesit_node, parser))
    return return_value;

  if (ts_tree_cursor_goto_parent (&cursor))
  {
    TSNode parent = ts_tree_cursor_current_node (&cursor);
    return_value = make_treesit_node (parser, parent);
  }
  ts_tree_cursor_delete (&cursor);
  return return_value;
}