]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix treesit-node-field-name and friends (bug#66674)
authorYuan Fu <casouri@gmail.com>
Mon, 11 Dec 2023 00:23:44 +0000 (16:23 -0800)
committerEshel Yaron <me@eshelyaron.com>
Thu, 21 Dec 2023 20:51:17 +0000 (21:51 +0100)
So turns out ts_node_field_name_for_child takes a named node index,
but we were passing it normal index that counts both named and
anonymous nodes.  That's what makes the field name all wrong in
treesit explorer.

* doc/lispref/parsing.texi:
(Accessing Node Information): Update docstring.
* lisp/treesit.el (treesit-node-index): Add some unrelated comment.
(treesit-node-field-name): Get named node index rather than all node
index.
* src/treesit.c (Ftreesit_node_field_name_for_child): Update
docstring, use ts_node_named_child_count.

(cherry picked from commit 9874561f39e62c1c9fada6c2e013f93d9ea65729)

doc/lispref/parsing.texi
lisp/treesit.el
src/treesit.c

index df81a805e67e0b8c6cc693279be07d5924662f83..36238c1e1d746030ee4322e7910a3ca9b0216919 100644 (file)
@@ -1071,8 +1071,8 @@ This function returns the field name of the @var{n}'th child of
 @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
index 9f885985f3b727d5cbd4b15a6f5d971259f8f198..e146e40020bbb22cdffb7e389b0bd819522f1dc2 100644 (file)
@@ -385,6 +385,7 @@ If NAMED is non-nil, collect named child only."
   "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))
@@ -392,7 +393,7 @@ If NAMED is non-nil, count named child only."
 (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)))
 
 (defun treesit-node-get (node instructions)
index b8de95ec69126dd201ee925a9af89a537c7b3b21..c85038e70cfdf0168a549800e1485ac403828115 100644 (file)
@@ -2063,9 +2063,8 @@ DEFUN ("treesit-node-field-name-for-child",
 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))
@@ -2079,7 +2078,7 @@ N could be negative, e.g., -1 represents the last child.  */)
 
   /* 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)