aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuan Fu2022-05-13 16:39:29 -0700
committerYuan Fu2022-05-13 16:41:50 -0700
commitb2b57eda041e4c30fb169031b6f5dadeab4c7d98 (patch)
tree5f1bda26144faff16d78f2f3efdef2fe3b2ccd88
parent750090fd076e6923c5cee6f67b170416d48694da (diff)
downloademacs-b2b57eda041e4c30fb169031b6f5dadeab4c7d98.tar.gz
emacs-b2b57eda041e4c30fb169031b6f5dadeab4c7d98.zip
Extract out treesit-search-forward
* lisp/treesit.el (treesit-search-forward, treesit-search-beginning, treesit-search-end): New functions. (treesit-traverse-defun): Remove function. (treesit-beginning-of-defun, treesit-end-of-defun): Replace 'treesit-traverse-defun' with 'treesit-search-forward' and fiends. * test/src/treesit-tests.el: Add reminder for tests.
-rw-r--r--lisp/treesit.el123
-rw-r--r--test/src/treesit-tests.el4
2 files changed, 90 insertions, 37 deletions
diff --git a/lisp/treesit.el b/lisp/treesit.el
index 60f375e9d92..1cfdab95ca8 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -832,48 +832,95 @@ indentation (target) is in green, current indentation is in red."
832 (indent-region (point-min) (point-max)) 832 (indent-region (point-min) (point-max))
833 (diff-buffers source-buf (current-buffer))))) 833 (diff-buffers source-buf (current-buffer)))))
834 834
835;;; Navigation 835;;; Search
836 836
837(defvar-local treesit-defun-query nil 837(defun treesit-search-forward (pos-fn arg query &optional lang)
838 "A tree-sitter query that matches function/class definitions. 838 "Search forward for nodes that matches QUERY.
839Capture names don't matter. This variable is used by navigation
840functions like `treesit-beginning-of-defun'.")
841 839
842(defun treesit-traverse-defun (pos-fn arg) 840This is a more primitive function, you might want to use
843 "Move forward/backward to the beginning/end of a defun. 841`treesit-search-beginning' or `treesit-search-end' instead.
844 842
845Defun is defined according to `treesit-defun-pattern'. Move 843QUERY has to capture the node to match. LANG specifies the
846forward/backward ARG time, positive ARG means go forward, 844language in which we search for nodes. If LANG is nil, use the
845first parser in `treesit-parser-list'.
846
847Move forward/backward ARG times, positive ARG means go forward,
847negative ARG means go backward. 848negative ARG means go backward.
848 849
849POS-FN can be either `treesit-node-start' or `treesit-node-end'." 850POS-FN can be either `treesit-node-start' or `treesit-node-end',
850 (unless treesit-defun-query 851or any function that takes a node and returns a position.
851 (error "Variable `treesit-defun-query' is unset")) 852
853If search succeeds, stop at the position returned by POS-FN and
854return the matched node. Return nil if search failed."
852 (cl-loop for idx from 1 to (abs arg) 855 (cl-loop for idx from 1 to (abs arg)
853 for positions = 856 for parser = (if lang
854 (remove 857 (treesit-get-parser-create lang)
855 nil 858 (car treesit-parser-list))
856 (mapcar (lambda (parser) 859 for node =
857 (if-let ((starting-point (point)) 860 (if-let ((starting-point (point))
858 (node (treesit-node-at 861 (node (treesit-node-at (point) parser t)))
859 (point) parser t))) 862 (treesit-traverse-forward-depth-first
860 (funcall 863 node
861 pos-fn 864 (lambda (node)
862 (treesit-traverse-forward-depth-first 865 (and (not (eq (funcall pos-fn node)
863 node 866 starting-point))
864 (lambda (node) 867 (if (> arg 0)
865 (and (not (eq (funcall pos-fn node) 868 ;; Make sure we move forward.
866 starting-point)) 869 (> (funcall pos-fn node) starting-point)
867 (treesit-query-capture 870 ;; Make sure we move backward.
868 node treesit-defun-query))) 871 (< (funcall pos-fn node) starting-point))
869 arg)))) 872 (cl-loop for cap-node in
870 treesit-parser-list)) 873 (mapcar
871 ;; If we can find a defun start, jump to it. 874 #'cdr
872 if positions do (goto-char (apply #'max positions)) 875 (treesit-query-capture node query))
876 if (treesit-node-eq cap-node node)
877 return t)))
878 arg))
879 for pos = (funcall pos-fn node)
880 ;; If we can find a match, jump to it.
881 if pos do (goto-char pos)
873 else return nil 882 else return nil
874 if (eq (point) (point-min)) return nil
875 ;; Return t to indicate that search is successful. 883 ;; Return t to indicate that search is successful.
876 finally return t)) 884 finally return node))
885
886(defun treesit-search-beginning (query arg &optional lang)
887 "Search forward for nodes that matches QUERY.
888
889Stops at the beginning of matched node.
890
891QUERY has to capture the node to match. LANG specifies the
892language in which we search for nodes. If LANG is nil, use the
893first parser in `treesit-parser-list'.
894
895Move forward/backward ARG times, positive ARG means go forward,
896negative ARG means go backward.
897
898If search succeeds, return the matched node. Return nil if
899search failed."
900 (treesit-search-forward #'treesit-node-start arg query lang))
901
902(defun treesit-search-end (query arg &optional lang)
903 "Search forward for nodes that matches QUERY.
904
905Stops at the end of matched node.
906
907QUERY has to capture the node to match. LANG specifies the
908language in which we search for nodes. If LANG is nil, use the
909first parser in `treesit-parser-list'.
910
911Move forward/backward ARG times, positive ARG means go forward,
912negative ARG means go backward.
913
914If search succeeds, return the matched node. Return nil if
915search failed."
916 (treesit-search-forward #'treesit-node-end arg query lang))
917
918;;; Navigation
919
920(defvar-local treesit-defun-query nil
921 "A tree-sitter query that matches function/class definitions.
922Capture names don't matter. This variable is used by navigation
923functions like `treesit-beginning-of-defun'.")
877 924
878(defun treesit-beginning-of-defun (&optional arg) 925(defun treesit-beginning-of-defun (&optional arg)
879 "Move backward to the beginning of a defun. 926 "Move backward to the beginning of a defun.
@@ -881,7 +928,9 @@ POS-FN can be either `treesit-node-start' or `treesit-node-end'."
881With ARG, do it that many times. Negative ARG means move forward 928With ARG, do it that many times. Negative ARG means move forward
882to the ARGth following beginning of defun. Defun is defined 929to the ARGth following beginning of defun. Defun is defined
883according to `treesit-defun-pattern'." 930according to `treesit-defun-pattern'."
884 (treesit-traverse-defun #'treesit-node-start (- arg))) 931 (unless treesit-defun-query
932 (error "Variable `treesit-defun-query' is unset"))
933 (treesit-search-beginning treesit-defun-query (- arg)))
885 934
886(defun treesit-end-of-defun (&optional arg) 935(defun treesit-end-of-defun (&optional arg)
887 "Move forward to the end of a defun. 936 "Move forward to the end of a defun.
@@ -889,7 +938,9 @@ according to `treesit-defun-pattern'."
889With ARG, do it that many times. Negative ARG means move back to 938With ARG, do it that many times. Negative ARG means move back to
890ARGth preceding end of defun. Defun is defined according to 939ARGth preceding end of defun. Defun is defined according to
891`treesit-defun-pattern'." 940`treesit-defun-pattern'."
892 (treesit-traverse-defun #'treesit-node-end arg)) 941 (unless treesit-defun-query
942 (error "Variable `treesit-defun-query' is unset"))
943 (treesit-search-end treesit-defun-query arg))
893 944
894;;; Debugging 945;;; Debugging
895 946
diff --git a/test/src/treesit-tests.el b/test/src/treesit-tests.el
index 65b871693d2..1b20b86bc9e 100644
--- a/test/src/treesit-tests.el
+++ b/test/src/treesit-tests.el
@@ -369,7 +369,9 @@
369;; TODO 369;; TODO
370;; - Functions in treesit.el 370;; - Functions in treesit.el
371;; - treesit-load-name-override-list 371;; - treesit-load-name-override-list
372;; - treesit-traverse-defun 372;; - treesit-search-forward
373;; - treesit-search-beginning
374;; - treesit-search-end
373;; - treesit-beginning-of-defun 375;; - treesit-beginning-of-defun
374;; - treesit-end-of-defun 376;; - treesit-end-of-defun
375 377