1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-15 10:30:25 -08:00

Make treesit-search-forward-goto accept a NODE argument

With NODE argument we can do

(setq node (treesit-search-forward-goto node))

And we can choose what node to pass to it (maybe we want to pass it
the largest node at point, rather than the smallest node, and in case
of multiple parsers, we can choose which parser to use).

* doc/lispref/parsing.texi (Retrieving Node): Update manual.
* lisp/treesit.el (treesit-search-forward-goto): Accept a NODE
argument.
This commit is contained in:
Yuan Fu 2022-10-23 18:29:02 -07:00
parent 524d10c86b
commit 6cf2a9c55d
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
2 changed files with 19 additions and 15 deletions

View file

@ -834,10 +834,10 @@ indentation (target) is in green, current indentation is in red."
;;; Search
(defun treesit-search-forward-goto
(predicate &optional start backward all)
(node predicate &optional start backward all)
"Search forward for a node and move to its end position.
Stops at the first node after point that matches PREDICATE.
Stops at the first node after NODE that matches PREDICATE.
PREDICATE can be either a regexp that matches against each node's
type case-insensitively, or a function that takes a node and
returns nil/non-nil for match/no match.
@ -846,20 +846,20 @@ If a node matches, move to that node and return the node,
otherwise return nil. If START is non-nil, stop at the
beginning rather than the end of a node.
This function guarantees that the matched node it returns makes
progress in terms of buffer position: the start/end position of
the returned node is always greater than that of NODE.
BACKWARD and ALL are the same as in `treesit-search-forward'."
(let ((node (treesit-node-at (point)))
(start-pos (point)))
;; Often the EOF (point-max) is a newline, and `treesit-node-at'
;; will return nil at that point (which is fair). But we need a
;; node as the starting point to traverse the tree. So we try to
;; use the node before point.
(when (and (not node) (eq (point) (point-max)))
(setq node (treesit-node-at (max (1- (point)) (point-min)))))
(when-let ((start-pos (if start
(treesit-node-start node)
(treesit-node-end node))))
;; When searching forward and stopping at beginnings, or search
;; backward stopping at ends, it is possible to "roll back" in
;; position. Take three nodes N1, N2, N3 as an example, if we
;; start at N3, search for forward for beginning, and N1 matches,
;; we would stop at beg of N1, which is backwards! So we skip N1.
;; we would stop at beg of N1, which is backwards! So we skip N1
;; and keep going.
;;
;; |<--------N1------->|
;; |<--N2-->| |<--N3-->|