]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/treesit.el: Fix treesit-outline.
authorJuri Linkov <juri@linkov.net>
Mon, 10 Feb 2025 07:19:46 +0000 (09:19 +0200)
committerEshel Yaron <me@eshelyaron.com>
Mon, 10 Feb 2025 20:56:40 +0000 (21:56 +0100)
(treesit-outline--at-point): New internal function.
(treesit-outline-search, treesit-outline-level):
Use 'treesit-outline--at-point'.

(cherry picked from commit 302274b18625b04d1f52fe4c1e52dbcd2cd5124b)

lisp/treesit.el

index e29f757d36a52103cc461b82fbd6035b5e413e01..7a449770576786d4b4404f4bd07c56ae45fe253b 100644 (file)
@@ -3500,15 +3500,26 @@ when a major mode sets it.")
               (funcall (nth 2 setting) node))))
    treesit-simple-imenu-settings))
 
+(defun treesit-outline--at-point ()
+  "Return the outline heading at the current line."
+  (let* ((pred treesit-outline-predicate)
+         (bol (pos-bol))
+         (eol (pos-eol))
+         (current (treesit-thing-at (point) pred))
+         (current-valid (when current
+                          (<= bol (treesit-node-start current) eol)))
+         (next (unless current-valid
+                 (treesit-navigate-thing (point) 1 'beg pred)))
+         (next-valid (when next (<= bol next eol))))
+    (or (and current-valid current)
+        (and next-valid (treesit-thing-at next pred)))))
+
 (defun treesit-outline-search (&optional bound move backward looking-at)
   "Search for the next outline heading in the syntax tree.
 For BOUND, MOVE, BACKWARD, LOOKING-AT, see the descriptions in
 `outline-search-function'."
   (if looking-at
-      (when-let* ((node (or (treesit-thing-at (1- (pos-eol)) treesit-outline-predicate)
-                            (treesit-thing-at (pos-bol) treesit-outline-predicate)))
-                  (start (treesit-node-start node)))
-        (eq (pos-bol) (save-excursion (goto-char start) (pos-bol))))
+      (when (treesit-outline--at-point) (pos-bol))
 
     (let* ((bob-pos
             ;; `treesit-navigate-thing' can't find a thing at bobp,
@@ -3539,7 +3550,7 @@ For BOUND, MOVE, BACKWARD, LOOKING-AT, see the descriptions in
 
 (defun treesit-outline-level ()
   "Return the depth of the current outline heading."
-  (let* ((node (treesit-node-at (point) nil t))
+  (let* ((node (treesit-outline--at-point))
          (level (if (treesit-node-match-p node treesit-outline-predicate)
                     1 0)))
     (while (setq node (treesit-parent-until node treesit-outline-predicate))