Function: treesit--thing-sibling

treesit--thing-sibling is a byte-compiled function defined in treesit.el.gz.

Signature

(treesit--thing-sibling POS THING PREV)

Documentation

Return the next or previous THING at POS.

If PREV is non-nil, return the previous THING. It's guaranteed that returned previous sibling's end <= POS, and returned next sibling's beginning >= POS.

Return nil if no THING can be found. THING should be a thing defined in treesit-thing-settings, or a predicate as described in treesit-thing-settings.

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit--thing-sibling (pos thing prev)
  "Return the next or previous THING at POS.

If PREV is non-nil, return the previous THING.  It's guaranteed
that returned previous sibling's end <= POS, and returned next
sibling's beginning >= POS.

Return nil if no THING can be found.  THING should be a thing
defined in `treesit-thing-settings', or a predicate as described
in `treesit-thing-settings'."
  (let* ((cursor (treesit-node-at pos))
         (pos-pred (if prev
                       (lambda (n) (<= (treesit-node-end n) pos))
                     (lambda (n) (>= (treesit-node-start n) pos))))
         (iter-pred (lambda (node)
                      (and (treesit-node-match-p node thing t)
                           (funcall pos-pred node))))
         (sibling nil))
    (when cursor
      ;; Find the node just before/after POS to start searching.
      (save-excursion
        (while (and cursor (not (funcall pos-pred cursor)))
          (setq cursor (treesit-search-forward-goto
                        cursor "" prev prev t))))
      ;; Keep searching until we run out of candidates or found a
      ;; return value.
      (while (and cursor
                  (funcall pos-pred cursor)
                  (null sibling))
        (setq sibling (treesit-node-top-level cursor iter-pred t))
        (setq cursor (treesit-search-forward cursor thing prev prev)))
      sibling)))