]> git.eshelyaron.com Git - emacs.git/commitdiff
Use treesit-node-match-p in treesit-parent-until/while
authorYuan Fu <casouri@gmail.com>
Mon, 5 Feb 2024 03:26:42 +0000 (19:26 -0800)
committerEshel Yaron <me@eshelyaron.com>
Wed, 7 Feb 2024 10:51:04 +0000 (11:51 +0100)
* lisp/treesit.el (treesit-parent-until): Use treesit-node-match-p.
(treesit-parent-while): Update docstring.
* doc/lispref/parsing.texi (Retrieving Nodes): Update docstring.

(cherry picked from commit be6de56906f0d1c09a0fad4f5165d864dddbc3ee)

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

index 5d79c4b27f4fd5da7574169c939ccd640cc791c6..ac11f88ae4d35f7c2ed3697d2a272aef048d4463 100644 (file)
@@ -916,8 +916,10 @@ nodes.
 
 @defun treesit-parent-until node predicate &optional include-node
 This function repeatedly finds the parents of @var{node}, and returns
-the parent that satisfies @var{pred}, a function that takes a node as
-argument and returns a boolean that indicates a match.  If no parent
+the parent that satisfies @var{pred}.  @var{pred} can be either a
+function that takes a node as argument and returns @code{t} or
+@code{nil}, or a regexp matching node type names, or other valid
+predicates described in @var{treesit-thing-settings}.  If no parent
 satisfies @var{pred}, this function returns @code{nil}.
 
 Normally this function only looks at the parents of @var{node} but not
@@ -927,11 +929,12 @@ function returns @var{node} if @var{node} satisfies @var{pred}.
 
 @defun treesit-parent-while node pred
 This function goes up the tree starting from @var{node}, and keeps
-doing so as long as the nodes satisfy @var{pred}, a function that
-takes a node as argument.  That is, this function returns the highest
-parent of @var{node} that still satisfies @var{pred}.  Note that if
-@var{node} satisfies @var{pred} but its immediate parent doesn't,
-@var{node} itself is returned.
+doing so as long as the nodes satisfy @var{pred}.  That is, this
+function returns the highest parent of @var{node} that still satisfies
+@var{pred}.  Note that if @var{node} satisfies @var{pred} but its
+immediate parent doesn't, @var{node} itself is returned.
+
+@var{pred} is the same as in @code{treesit-parent-until} above.
 @end defun
 
 @defun treesit-node-top-level node &optional type
index 93b6b56534da28cd05f7d42e90c766f5e97e6b41..f179204d89c0b8051c429c09481e0d561381bd6f 100644 (file)
@@ -344,14 +344,13 @@ ancestor node which satisfies the predicate PRED; then it
 returns that ancestor node.  It returns nil if no ancestor
 node was found that satisfies PRED.
 
-PRED should be a function that takes one argument, the node to
-examine, and returns a boolean value indicating whether that
-node is a match.
+PRED can be a predicate function, a regexp matching node type,
+and more; see docstring of `treesit-thing-settings'.
 
 If INCLUDE-NODE is non-nil, return NODE if it satisfies PRED."
   (let ((node (if include-node node
                 (treesit-node-parent node))))
-    (while (and node (not (funcall pred node)))
+    (while (and node (not (treesit-node-match-p node pred)))
       (setq node (treesit-node-parent node)))
     node))
 
@@ -364,9 +363,8 @@ no longer satisfies the predicate PRED; it returns the last
 examined node that satisfies PRED.  If no node satisfies PRED, it
 returns nil.
 
-PRED should be a function that takes one argument, the node to
-examine, and returns a boolean value indicating whether that
-node is a match."
+PRED can be a predicate function, a regexp matching node type,
+and more; see docstring of `treesit-thing-settings'."
   (let ((last nil))
     (while (and node (funcall pred node))
       (setq last node