From: Juri Linkov Date: Thu, 27 Feb 2025 17:21:45 +0000 (+0200) Subject: Improve 'treesit-outline-search' X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=cebb3dd905b71d3745f2a532e477fb2455c102ae;p=emacs.git Improve 'treesit-outline-search' * 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) --- diff --git a/lisp/textmodes/markdown-ts-mode.el b/lisp/textmodes/markdown-ts-mode.el index 78402e3c0cb..bced0e4b86a 100644 --- a/lisp/textmodes/markdown-ts-mode.el +++ b/lisp/textmodes/markdown-ts-mode.el @@ -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) diff --git a/lisp/treesit.el b/lisp/treesit.el index 386278ee3f0..759bd9fe8bf 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -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)))