]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/treesit.el (treesit--parser-overlay-offset): New variable.
authorJuri Linkov <juri@linkov.net>
Mon, 12 May 2025 18:23:03 +0000 (21:23 +0300)
committerEshel Yaron <me@eshelyaron.com>
Mon, 12 May 2025 19:56:30 +0000 (21:56 +0200)
(treesit-parsers-at): Use it (bug#77906).
(treesit-forward-sexp, treesit--forward-list-with-default)
(treesit-up-list): Let-bind 'treesit--parser-overlay-offset'.

(cherry picked from commit 824e4868db312f0cd9e735cf13d54466c4af5934)

lisp/treesit.el

index 9cbf063be1d8ea33cf54df307d2ca6df1fb2795c..c2dc9ab460202fe05f78186c646a994514795bf4 100644 (file)
@@ -838,6 +838,11 @@ those inside are kept."
            if (<= start (car range) (cdr range) end)
            collect range))
 
+(defvar treesit--parser-overlay-offset 0
+  "Defines at which position to get the parser overlay.
+The commands that move backward need to set it to -1 to be
+able to use the range that ends immediately before point.")
+
 (defun treesit-parsers-at (&optional pos language with-host only)
   "Return all parsers at POS.
 
@@ -868,7 +873,8 @@ That is, the deepest embedded parser comes first."
   (let ((res nil))
     ;; Refer to (ref:local-parser-overlay) for more explanation of local
     ;; parser overlays.
-    (dolist (ov (overlays-at (or pos (point))))
+    (dolist (ov (overlays-at (+ (or pos (point))
+                                treesit--parser-overlay-offset)))
       (when-let* ((parser (overlay-get ov 'treesit-parser))
                   (host-parser (or (null with-host)
                                    (overlay-get ov 'treesit-host-parser)))
@@ -3011,7 +3017,8 @@ across atoms (such as symbols or words) inside the list."
           t)
         (if (> arg 0)
             (treesit-end-of-thing pred (abs arg) 'restricted)
-          (treesit-beginning-of-thing pred (abs arg) 'restricted))
+          (let ((treesit--parser-overlay-offset -1))
+            (treesit-beginning-of-thing pred (abs arg) 'restricted)))
         ;; If we couldn't move, we should signal an error and report
         ;; the obstacle, like `forward-sexp' does.  If we couldn't
         ;; find a parent, we simply return nil without moving point,
@@ -3026,6 +3033,7 @@ the boundaries of the list.
 ARG is described in the docstring of `forward-list'."
   (let* ((pred (or treesit-sexp-type-regexp 'list))
          (arg (or arg 1))
+         (treesit--parser-overlay-offset (if (> arg 0) 0 -1))
          (cnt arg)
          (inc (if (> arg 0) 1 -1)))
     (while (/= cnt 0)
@@ -3153,6 +3161,7 @@ ARG is described in the docstring of `up-list'."
   (interactive "^p")
   (let* ((pred (or treesit-sexp-type-regexp 'list))
          (arg (or arg 1))
+         (treesit--parser-overlay-offset -1)
          (cnt arg)
          (inc (if (> arg 0) 1 -1)))
     (while (/= cnt 0)