Adapt treesit-outline-search to the recent improvements (bug#76398)

* lisp/treesit.el (treesit-closest-parser-boundary):
Remove temporary function.
(treesit-outline-search): Use 'previous/next-single-char-property-change'
with 'treesit-parser' property to find the closest parser boundary.
(treesit-outline-level): Get the 'treesit-host-parser' overlay
and find the parent parser node with 'treesit-node-at'.
This commit is contained in:
Juri Linkov 2025-03-11 20:18:24 +02:00
parent ef79c8cf3d
commit b741023070

View file

@ -3945,18 +3945,6 @@ this variable takes priority.")
(or (and current-valid current)
(and next-valid (treesit-thing-at next pred)))))
(defun treesit-closest-parser-boundary (pos backward)
"Get the closest boundary of a local parser."
(when-let* ((ranges (mapcar #'treesit-parser-included-ranges
(treesit-parser-list)))
(ranges (delq nil (delete '((1 . 1)) ranges)))
(bounds (seq-filter
(lambda (p) (if backward (< p pos) (> p pos)))
(flatten-list ranges)))
(closest (when bounds
(if backward (seq-max bounds) (seq-min bounds)))))
closest))
(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
@ -3982,10 +3970,15 @@ For BOUND, MOVE, BACKWARD, LOOKING-AT, see the descriptions in
treesit-outline-predicate))
(found (or bob-pos
(treesit-navigate-thing pos (if backward -1 1) 'beg pred)))
(closest (treesit-closest-parser-boundary pos backward)))
(closest (unless bob-pos
(if backward
(previous-single-char-property-change pos 'treesit-parser)
(next-single-char-property-change pos 'treesit-parser)))))
;; Handle multi-language modes
(if (and closest (not recursive)
(if (and closest
(not (eq closest (if backward (point-min) (point-max))))
(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
@ -4018,21 +4011,21 @@ For BOUND, MOVE, BACKWARD, LOOKING-AT, see the descriptions in
"Return the depth of the current outline heading."
(let* ((node (treesit-outline--at-point))
(level 1)
(parser (when (and treesit-aggregated-outline-predicate node)
(treesit-node-parser node)))
(pred (if treesit-aggregated-outline-predicate
(alist-get (treesit-language-at (point))
treesit-aggregated-outline-predicate)
treesit-outline-predicate)))
(while (setq node (treesit-parent-until node pred))
(setq level (1+ level)))
(when-let* ((_ (and parser (not (eq parser treesit-primary-parser))))
(guest-root-node (treesit-parser-root-node parser))
(host-lang (treesit-parser-language treesit-primary-parser))
(host-pred (alist-get host-lang treesit-aggregated-outline-predicate)))
;; Continue from the host node that contains the guest parser.
(setq node (treesit-node-at (- (treesit-node-start guest-root-node) 2)))
(while (setq node (treesit-parent-until node host-pred))
;; Continue counting from the host node.
(when-let* ((parser
(when treesit-aggregated-outline-predicate
(seq-some (lambda (o) (overlay-get o 'treesit-host-parser))
(overlays-at (point)))))
(node (treesit-node-at (point) parser))
(lang (treesit-parser-language parser))
(pred (alist-get lang treesit-aggregated-outline-predicate)))
(while (setq node (treesit-parent-until node pred))
(setq level (1+ level))))
level))