Function: treesit-hs-find-next-block

treesit-hs-find-next-block is a byte-compiled function defined in treesit.el.gz.

Signature

(treesit-hs-find-next-block REGEXP MAXP COMMENTS)

Documentation

Tree-sitter implementation of hs-find-next-block-function.

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit-hs-find-next-block (_regexp maxp comments)
  "Tree-sitter implementation of `hs-find-next-block-function'."
  (when (not comments)
    (forward-comment (point-max)))
  (let* ((comment-pred
          (when comments
            (if (treesit-thing-defined-p 'comment (treesit-language-at (point)))
                'comment "\\`comment\\'")))
         (hs-things (bound-and-true-p hs-treesit-things))
         (pred (append `(or ,hs-things) (when comment-pred (list comment-pred))))
         ;; `treesit-navigate-thing' can't find a thing at bobp,
         ;; so use `treesit-thing-at' to match at bobp.
         (current (treesit-thing-at (point) pred))
         (beg (or (and current (eq (point) (treesit-node-start current)) (point))
                  (treesit-navigate-thing (point) 1 'beg pred)))
         ;; Check if we found a block or a comment
         (block-thing (when beg (treesit-thing-at beg hs-things)))
         (comment-thing (when beg (treesit-thing-at beg comment-pred)))
         (comment-p (and comment-thing (eq beg (treesit-node-start comment-thing))))
         (thing (if comment-p comment-thing block-thing))
         (end (if thing (min (1+ (treesit-node-start thing)) (point-max)))))
    (when (and end (<= end maxp))
      (goto-char end)
      (set-match-data
       (if (and comments comment-p)
           (list beg end nil nil beg end)
         (list beg end beg end)))
      t)))