]> git.eshelyaron.com Git - emacs.git/commitdiff
Improve 'treesit-outline-search'
authorJuri Linkov <juri@linkov.net>
Thu, 27 Feb 2025 17:21:45 +0000 (19:21 +0200)
committerEshel Yaron <me@eshelyaron.com>
Fri, 28 Feb 2025 11:19:22 +0000 (12:19 +0100)
* lisp/treesit.el (treesit-outline-search): Add optional arg
'recursive' to avoid infinite recursion when it gets stuck.

* lisp/textmodes/markdown-ts-mode.el (markdown-ts-mode):
Set buffer-local 'treesit-outline-predicate' to "section".

(cherry picked from commit dc1637fef7c658b6d865f3e71f3b95686e56542d)

lisp/textmodes/markdown-ts-mode.el
lisp/treesit.el

index 78402e3c0cb0ae86b42181b62599171654038ae3..bced0e4b86a583ca284a4e36a05f1cb2c8d3cb27 100644 (file)
@@ -95,8 +95,8 @@
                                                (paragraph-inline)))
 
   (setq-local treesit-simple-imenu-settings
-              `(("Headings"
-                 markdown-ts-imenu-node-p nil markdown-ts-imenu-name-function)))
+              `(("Headings" markdown-ts-imenu-node-p nil markdown-ts-imenu-name-function)))
+  (setq-local treesit-outline-predicate "section")
 
   (when (treesit-ready-p 'markdown)
     (treesit-parser-create 'markdown-inline)
index 386278ee3f0aaf18327c339346ec7a80c1ad312e..759bd9fe8bfc9c71f607a8934003ddb6af27831e 100644 (file)
@@ -3657,7 +3657,7 @@ this variable takes priority.")
                          (if backward (seq-max bounds) (seq-min bounds)))))
     closest))
 
-(defun treesit-outline-search (&optional bound move backward looking-at)
+(defun treesit-outline-search (&optional bound move backward looking-at recursive)
   "Search for the next outline heading in the syntax tree.
 For BOUND, MOVE, BACKWARD, LOOKING-AT, see the descriptions in
 `outline-search-function'."
@@ -3677,7 +3677,7 @@ For BOUND, MOVE, BACKWARD, LOOKING-AT, see the descriptions in
                   (if (bobp) (point) (1- (point)))
                 (pos-eol))))
            (pred (if treesit-aggregated-outline-predicate
-                     (alist-get (treesit-language-at pos)
+                     (alist-get (treesit-language-at (or bob-pos pos))
                                 treesit-aggregated-outline-predicate)
                    treesit-outline-predicate))
            (found (or bob-pos
@@ -3685,7 +3685,7 @@ For BOUND, MOVE, BACKWARD, LOOKING-AT, see the descriptions in
            (closest (treesit-closest-parser-boundary pos backward)))
 
       ;; Handle multi-language modes
-      (if (and closest
+      (if (and closest (not recursive)
                (or
                 ;; Possibly was inside the local parser, and when can't find
                 ;; more matches inside it then need to go over the closest
@@ -3700,7 +3700,7 @@ For BOUND, MOVE, BACKWARD, LOOKING-AT, see the descriptions in
             (goto-char (if backward
                            (max (point-min) (1- closest))
                          (min (point-max) (1+ closest))))
-            (treesit-outline-search bound move backward))
+            (treesit-outline-search bound move backward nil 'recursive))
 
         (if found
             (if (or (not bound) (if backward (>= found bound) (<= found bound)))