]> git.eshelyaron.com Git - emacs.git/commitdiff
Revert "Fix treesit-node-field-name and friends (bug#66674)"
authorYuan Fu <casouri@gmail.com>
Sat, 30 Dec 2023 03:52:07 +0000 (19:52 -0800)
committerEshel Yaron <me@eshelyaron.com>
Tue, 2 Jan 2024 07:36:06 +0000 (08:36 +0100)
This reverts commit 9874561f39e62c1c9fada6c2e013f93d9ea65729.

See bug#67990.  Basically our original code is correct, the error is
in libtree-sitter, which only manifests in certain cases.

https://github.com/tree-sitter/tree-sitter/pull/2104
(cherry picked from commit 530315287254da2e6b0767ad343fa55f79be8536)

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

index 36238c1e1d746030ee4322e7910a3ca9b0216919..df81a805e67e0b8c6cc693279be07d5924662f83 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 named nodes only, and @var{n} can be
-negative, e.g., @minus{}1 represents the last child.
+Note that @var{n} counts both named and anonymous children, 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 89120d49accd102f9567170d8e24760724491a78..d4857dea72e998bdd4e1235bfd1086034e43c417 100644 (file)
@@ -385,7 +385,6 @@ 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))
@@ -393,7 +392,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 t)))
+             (idx (treesit-node-index node)))
     (treesit-node-field-name-for-child parent idx)))
 
 (defun treesit-node-get (node instructions)
index f161b748fd9a0a771d81acfaa54cefdc45d71073..296a404a9d1e00150d7c83df075fa74f18fbefdc 100644 (file)
@@ -2070,8 +2070,9 @@ 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.
 
-Note that N counts named nodes only.  Also, N could be negative, e.g.,
--1 represents the last child.  */)
+N counts all children, i.e., named ones and anonymous ones.
+
+N could be negative, e.g., -1 represents the last child.  */)
   (Lisp_Object node, Lisp_Object n)
 {
   if (NILP (node))
@@ -2085,7 +2086,7 @@ Note that N counts named nodes only.  Also, N could be negative, e.g.,
 
   /* Process negative index.  */
   if (idx < 0)
-    idx = ts_node_named_child_count (treesit_node) + idx;
+    idx = ts_node_child_count (treesit_node) + idx;
   if (idx < 0)
     return Qnil;
   if (idx > UINT32_MAX)