Function: treesit--thing-sibling

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

Signature

(treesit--thing-sibling POS THING PREV &optional PARSER)

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.

If PARSER is non-nil, only use that parser's parse tree. Otherwise each parser covering point is tried from most specific (deepest embedded) to least specific. If there are multiple parsers with the same embed level at POS, which parser is tried first is undefined. If PARSER is a language symbol, the parsers tried are limited to ones for that language.

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 &optional parser)
  "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.

If PARSER is non-nil, only use that parser's parse tree.  Otherwise each
parser covering point is tried from most specific (deepest embedded) to
least specific.  If there are multiple parsers with the same embed level
at POS, which parser is tried first is undefined.  If PARSER is a
language symbol, the parsers tried are limited to ones for that
language.

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'."
  (treesit--some (parser (cond ((null parser)
                                (treesit-parsers-at pos))
                               ((symbolp parser)
                                (treesit-parsers-at pos parser))
                               (t (list parser))))
    (let* ((cursor (treesit-node-at pos parser))
           (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))))