Function: treesit-parsers-at

treesit-parsers-at is a byte-compiled function defined in treesit.el.gz.

Signature

(treesit-parsers-at &optional POS LANGUAGE WITH-HOST ONLY)

Documentation

Return all parsers at POS.

POS defaults to point. The returned parsers are sorted by the decreasing embed level.

If LANGUAGE is non-nil, only return parsers for LANGUAGE.

If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER) instead. HOST-PARSER is the host parser which created the PARSER.

If ONLY is nil, return all parsers including the primary parser.

The argument ONLY can be a list of symbols that specify what parsers to include in the return value.

If ONLY contains the symbol local, include local parsers. Local parsers are those which only parse a limited region marked by an overlay with non-nil treesit-parser-local-p property.

If ONLY contains the symbol global, include non-local parsers excluding the primary parser.

If ONLY contains the symbol primary, include the primary parser.

The returned parsers are sorted in descending order of embed level. That is, the deepest embedded parser comes first.

View in manual

Probably introduced at or before Emacs version 31.1.

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit-parsers-at (&optional pos language with-host only)
  "Return all parsers at POS.

POS defaults to point.  The returned parsers are sorted by
the decreasing embed level.

If LANGUAGE is non-nil, only return parsers for LANGUAGE.

If WITH-HOST is non-nil, return a list of (PARSER . HOST-PARSER)
instead.  HOST-PARSER is the host parser which created the PARSER.

If ONLY is nil, return all parsers including the primary parser.

The argument ONLY can be a list of symbols that specify what
parsers to include in the return value.

If ONLY contains the symbol `local', include local parsers.
Local parsers are those which only parse a limited region marked
by an overlay with non-nil `treesit-parser-local-p' property.

If ONLY contains the symbol `global', include non-local parsers
excluding the primary parser.

If ONLY contains the symbol `primary', include the primary parser.

The returned parsers are sorted in descending order of embed level.
That is, the deepest embedded parser comes first."
  (let ((res nil))
    ;; Refer to (ref:local-parser-overlay) for more explanation of local
    ;; parser overlays.
    (dolist (ov (overlays-at (+ (or pos (point))
                                treesit--parser-overlay-offset)))
      (when-let* ((parser (overlay-get ov 'treesit-parser))
                  (host-parser (or (null with-host)
                                   (overlay-get ov 'treesit-host-parser)))
                  (_ (or (null language)
                         (eq (treesit-parser-language parser)
                             language)))
                  (_ (or (null only)
                         (and (memq 'local only) (memq 'global only))
                         (and (memq 'local only)
                              (overlay-get ov 'treesit-parser-local-p))
                         (and (memq 'global only)
                              (not (overlay-get ov 'treesit-parser-local-p))))))
        (push (if with-host (cons parser host-parser) parser) res)))
    (when (and (not with-host) (or (null only) (memq 'primary only)))
      (push (or treesit-primary-parser
                (car (treesit-parser-list)))
            res))
    (seq-sort-by (lambda (p)
                   (treesit-parser-embed-level
                    (or (car-safe p) p)))
                 (lambda (a b)
                   (> (or a 0) (or b 0)))
                 res)))