emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

feature/tree-sitter 6cf2a9c55d 4/7: Make treesit-search-forward-goto acc


From: Yuan Fu
Subject: feature/tree-sitter 6cf2a9c55d 4/7: Make treesit-search-forward-goto accept a NODE argument
Date: Sun, 23 Oct 2022 22:07:03 -0400 (EDT)

branch: feature/tree-sitter
commit 6cf2a9c55dbf8dfbc93409340065cf02881502b1
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    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.
---
 doc/lispref/parsing.texi | 12 ++++++++----
 lisp/treesit.el          | 22 +++++++++++-----------
 2 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/doc/lispref/parsing.texi b/doc/lispref/parsing.texi
index 8919a3ea70..bd80357957 100644
--- a/doc/lispref/parsing.texi
+++ b/doc/lispref/parsing.texi
@@ -702,10 +702,14 @@ answering questions like ``what is the first node after 
@var{start} in
 the buffer that satisfies some condition?''
 @end defun
 
-@defun treesit-search-forward-goto predicate &optional start backward all
-This function moves point to the start or end of the next node in
-the buffer that matches @var{predicate}.  If @var{start} is
-non-nil, stop at the beginning rather than the end of a node.
+@defun treesit-search-forward-goto node predicate &optional start backward all
+This function moves point to the start or end of the next node after
+@var{node} in the buffer that matches @var{predicate}.  If @var{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 @var{node}.
 
 Arguments @var{predicate}, @var{backward} and @var{all} are the same
 as in @code{treesit-search-forward}.
diff --git a/lisp/treesit.el b/lisp/treesit.el
index f8ab96ddb2..b391667b1b 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -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-->|



reply via email to

[Prev in Thread] Current Thread [Next in Thread]