Use treesit-node-match-p in treesit-parent-until/while

* lisp/treesit.el (treesit-parent-until): Use treesit-node-match-p.
(treesit-parent-while): Update docstring.
* doc/lispref/parsing.texi (Retrieving Nodes): Update docstring.
This commit is contained in:
Yuan Fu 2024-02-04 19:26:42 -08:00
parent 9dbbf93a4a
commit be6de56906
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
2 changed files with 15 additions and 14 deletions

View 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

View 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