Function: treesit--update-ranges-local

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

Signature

(treesit--update-ranges-local QUERY EMBEDDED-LANG MODIFIED-TICK &optional BEG END OFFSET)

Documentation

Update range for local parsers between BEG and END.

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

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.

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

Source Code

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

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.

OFFSET is a cons of start and end offsets that are applied to the range
for the local parser."
  ;; Update range.
  (let* ((host-lang (treesit-query-language query))
         (host-parser (treesit-parser-create host-lang))
         (ranges (treesit-query-range host-parser query beg end))
         (offset-left (or (car offset) 0))
         (offset-right (or (cdr offset) 0)))
    (pcase-dolist (`(,beg . ,end) ranges)
      (let ((has-parser nil))
        (setq
         has-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)))
               (when (eq parser-lang embedded-lang)
                 (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 t))))))
        ;; Create overlay and local parser.
        (when (not has-parser)
          (let ((embedded-parser (treesit-parser-create
                                  embedded-lang nil t 'embedded))
                (ov (make-overlay beg end nil nil t)))
            (overlay-put ov 'treesit-parser embedded-parser)
            (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 offset-left)
                                . ,(+ end offset-right))))))))))