]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/treesit.el: Use forward-list-default-function for C-M-n
authorJuri Linkov <juri@linkov.net>
Mon, 6 Jan 2025 18:02:18 +0000 (20:02 +0200)
committerEshel Yaron <me@eshelyaron.com>
Wed, 8 Jan 2025 08:36:57 +0000 (09:36 +0100)
(treesit--forward-list-with-default): New internal function
with body from 'treesit-forward-sexp-list' (bug#73404).
(treesit-forward-sexp-list, treesit-forward-list):
Replace body with a call to the shared implementation
'treesit--forward-list-with-default' using the
corresponding default function as an argument.

(cherry picked from commit 997b9b8d38fd8a770fb1e0b3c85abae2c0d6cdd3)

lisp/treesit.el

index 3e6a109b60522dad88975360198307563a11baa9..49ccc53aa62b1402910f9e89459264c896f0f5e7 100644 (file)
@@ -2461,18 +2461,12 @@ across atoms (such as symbols or words) inside the list."
         ;; then functions like `up-list' will signal "at top level".
         (treesit--scan-error pred arg))))
 
-(defun treesit-forward-sexp-list (&optional arg)
-  "Alternative tree-sitter implementation for `forward-sexp-function'.
-
-Whereas `treesit-forward-sexp' moves across both lists and atoms
-using `sexp' in `treesit-thing-settings', this function uses
-`sexp-list' in `treesit-thing-settings' to move only across lists.
-But to move across atoms (such as symbols or words) inside the list
-it uses `forward-sexp-default-function' as long as it doesn't go
-outside of the boundaries of the current list.
+(defun treesit--forward-list-with-default (arg default-function)
+  "Move forward across a list.
+Fall back to DEFAULT-FUNCTION as long as it doesn't cross
+the boundaries of the list.
 
-ARG is described in the docstring of `forward-sexp-function'."
-  (interactive "^p")
+ARG is described in the docstring of `forward-list'."
   (let* ((pred (or treesit-sexp-type-regexp 'sexp-list))
          (arg (or arg 1))
          (cnt arg)
@@ -2481,7 +2475,7 @@ ARG is described in the docstring of `forward-sexp-function'."
       (let* ((default-pos
               (condition-case _
                   (save-excursion
-                    (forward-sexp-default-function inc)
+                    (funcall default-function inc)
                     (point))
                 (scan-error nil)))
              (sibling (if (> arg 0)
@@ -2489,7 +2483,7 @@ ARG is described in the docstring of `forward-sexp-function'."
                         (treesit-thing-prev (point) pred)))
              (current (when default-pos
                         (treesit-thing-at (point) pred t))))
-        ;; Use 'forward-sexp-default-function' only if it doesn't go
+        ;; Use the default function only if it doesn't go
         ;; over the sibling and doesn't go out of the current group.
         (or (when (and default-pos
                        (or (null sibling)
@@ -2498,8 +2492,8 @@ ARG is described in the docstring of `forward-sexp-function'."
                              (>= default-pos (treesit-node-end sibling))))
                        (or (null current)
                            (if (> arg 0)
-                               (< default-pos (treesit-node-end current))
-                             (> default-pos (treesit-node-start current)))))
+                               (<= default-pos (treesit-node-end current))
+                             (>= default-pos (treesit-node-start current)))))
               (goto-char default-pos))
             (when sibling
               (goto-char (if (> arg 0)
@@ -2508,6 +2502,20 @@ ARG is described in the docstring of `forward-sexp-function'."
             (treesit--scan-error pred arg)))
       (setq cnt (- cnt inc)))))
 
+(defun treesit-forward-sexp-list (&optional arg)
+  "Alternative tree-sitter implementation for `forward-sexp-function'.
+
+Whereas `treesit-forward-sexp' moves across both lists and atoms
+using `sexp' in `treesit-thing-settings', this function uses
+`sexp-list' in `treesit-thing-settings' to move only across lists.
+But to move across atoms (such as symbols or words) inside the list
+it uses `forward-sexp-default-function' as long as it doesn't go
+outside of the boundaries of the current list.
+
+ARG is described in the docstring of `forward-sexp-function'."
+  (interactive "^p")
+  (treesit--forward-list-with-default arg 'forward-sexp-default-function))
+
 (defun treesit-forward-list (&optional arg)
   "Move forward across a list.
 What constitutes a list is determined by `sexp-list' in
@@ -2517,17 +2525,12 @@ parentheses-like expressions.
 Unlike `forward-sexp', this command moves only across a list,
 but not across atoms (such as symbols or words) inside the list.
 
-This command is the tree-sitter variant of `forward-list'
-redefined by the variable `forward-list-function'.
+It uses `forward-list-default-function' as long as it doesn't go
+outside of the boundaries of the current list.
 
-ARG is described in the docstring of `forward-list'."
+ARG is described in the docstring of `forward-list-function'."
   (interactive "^p")
-  (let ((arg (or arg 1))
-        (pred 'sexp-list))
-    (or (if (> arg 0)
-            (treesit-end-of-thing pred (abs arg) 'restricted)
-          (treesit-beginning-of-thing pred (abs arg) 'restricted))
-        (treesit--scan-error pred arg))))
+  (treesit--forward-list-with-default arg 'forward-list-default-function))
 
 (defun treesit-down-list (&optional arg)
   "Move forward down one level of parentheses.