Function: treesit--update-ranges-local

treesit--update-ranges-local is a byte-compiled function defined in treesit.el.gz.

Signature

(treesit--update-ranges-local HOST-PARSER QUERY EMBEDDED-LANG MODIFIED-TICK EMBED-LEVEL &optional BEG END OFFSET RANGE-FN)

Documentation

Update range for local parsers between BEG and END under HOST-PARSER.

Use QUERY to get the ranges, and make sure each range has a local parser for EMBEDDED-LANG. HOST-PARSER and QUERY must match.

The local parser is stored in an overlay, in the treesit-parser property, the host parser is stored in the treesit-host-parser property.

When this function touches an overlay, it sets the treesit-parser-ov-timestamp property of the overlay to MODIFIED-TICK. This will help Emacs garbage-collect overlays that aren't in use anymore.

EMBED-LEVEL is the embed level for the local parsers being created or updated. When looking for existing local parsers, only look for parsers of this level; when creating new local parsers, set their level to this level.

OFFSET is a cons of start and end offsets that are applied to the range for the local parser.

RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and returns the ranges to use for that node.

Return the created local parsers as a list.

Source Code

;; Defined in /usr/src/emacs/lisp/treesit.el.gz
(defun treesit--update-ranges-local
    ( host-parser query embedded-lang modified-tick embed-level
      &optional beg end offset range-fn)
  "Update range for local parsers between BEG and END under HOST-PARSER.
Use QUERY to get the ranges, and make sure each range has a local
parser for EMBEDDED-LANG.  HOST-PARSER and QUERY must match.

The local parser is stored in an overlay, in the `treesit-parser'
property, the host parser is stored in the `treesit-host-parser'
property.

When this function touches an overlay, it sets the
`treesit-parser-ov-timestamp' property of the overlay to MODIFIED-TICK.
This will help Emacs garbage-collect overlays that aren't in use
anymore.

EMBED-LEVEL is the embed level for the local parsers being created or
updated.  When looking for existing local parsers, only look for parsers
of this level; when creating new local parsers, set their level to this
level.

OFFSET is a cons of start and end offsets that are applied to the range
for the local parser.

RANGE-FN, if non-nil, is a function that takes a node and OFFSET, and
returns the ranges to use for that node.

Return the created local parsers as a list."
  ;; Update range.
  (let ((ranges-by-lang
         (if (functionp embedded-lang)
             (treesit-query-range-by-language
              host-parser query embedded-lang beg end offset range-fn)
           (list (cons embedded-lang
                       (treesit-query-range
                        host-parser query beg end offset range-fn)))))
        (touched-parsers nil))
    (dolist (lang-and-range ranges-by-lang)
      (let ((embedded-lang (car lang-and-range))
            (ranges (cdr lang-and-range)))
        (pcase-dolist (`(,beg . ,end) ranges)
          (let ((existing-local-parser
                 (catch 'done
                   (dolist (ov (overlays-in beg end) nil)
                     ;; Update range of local parser.
                     (when-let* ((embedded-parser
                                  (overlay-get ov 'treesit-parser))
                                 (parser-lang (treesit-parser-language
                                               embedded-parser))
                                 (parser-level (treesit-parser-embed-level
                                                embedded-parser)))
                       (when (and (overlay-get ov 'treesit-parser-local-p)
                                  (eq parser-lang embedded-lang)
                                  (eq embed-level parser-level))
                         (treesit-parser-set-included-ranges
                          embedded-parser `((,beg . ,end)))
                         (move-overlay ov beg end)
                         (overlay-put ov 'treesit-parser-ov-timestamp
                                      modified-tick)
                         (throw 'done embedded-parser)))))))
            (if existing-local-parser
                (push existing-local-parser touched-parsers)
              ;; Create overlay and local parser.  Refer to
              ;; (ref:local-parser-overlay) for more explanation of
              ;; local parser overlays.
              (let ((embedded-parser (treesit-parser-create
                                      embedded-lang nil t 'embedded))
                    (ov (make-overlay beg end nil nil t)))
                (treesit-parser-set-embed-level embedded-parser embed-level)
                (overlay-put ov 'treesit-parser embedded-parser)
                (overlay-put ov 'treesit-parser-local-p t)
                (overlay-put ov 'treesit-host-parser host-parser)
                (overlay-put ov 'treesit-parser-ov-timestamp
                             modified-tick)
                (treesit-parser-set-included-ranges
                 embedded-parser `((,beg . ,end)))
                (push embedded-parser touched-parsers)))))))
    touched-parsers))