Function: treesit-font-lock-fontify-region
treesit-font-lock-fontify-region is a byte-compiled function defined
in treesit.el.gz.
Signature
(treesit-font-lock-fontify-region START END &optional LOUDLY)
Documentation
Fontify the region between START and END.
If LOUDLY is non-nil, display some debugging information.
Source Code
;; Defined in /usr/src/emacs/lisp/treesit.el.gz
;; Some details worth explaining:
;;
;; 1. When we apply face to a node, we clip the face into the
;; currently fontifying region, this way we don't overwrite faces
;; applied by regexp-based font-lock. The clipped part will be
;; fontified fine when Emacs fontifies the region containing it.
;;
;; 2. If you insert an ending quote into a buffer, jit-lock only wants
;; to fontify that single quote, and (treesit-node-on start end) will
;; give you that quote node. We want to capture the string and apply
;; string face to it, but querying on the quote node will not give us
;; the string node. So we don't use treesit-node-on: using the root
;; node with a restricted range is very fast anyway (even in large
;; files of size ~10MB). Plus, querying the result of
;; `treesit-node-on' could still miss patterns even if we use some
;; heuristic to enlarge the node (how much to enlarge? to which
;; extent?), it's much safer to just use the root node.
;;
;; Sometimes the source file has some errors that cause tree-sitter to
;; parse it into a enormously tall tree (10k levels tall). In that
;; case querying the root node is very slow. So we try to get
;; top-level nodes and query them. This ensures that querying is fast
;; everywhere else, except for the problematic region. (Bug#59415).
;;
;; Some other time the source file has a top-level node that contains
;; a huge number of immediate children (say, 10k children), querying
;; that node is also very slow, so instead of getting the top-level
;; node, we recursively go down the tree to find nodes that cover the
;; region but are reasonably small. (Bug#59738).
;;
;; 3. It is possible to capture a node that's completely outside the
;; region between START and END: as long as the whole pattern
;; intersects the region, all the captured nodes in that pattern are
;; returned. If the node is outside of that region, (max node-start
;; start) and friends return bad values, so we filter them out.
;; However, we don't filter these nodes out if a function will process
;; the node, because it could (and often do) fontify the relatives of
;; the captured node, not just the node itself. If we took out those
;; nodes author of those functions would be very confused.
(defun treesit-font-lock-fontify-region (start end &optional loudly)
"Fontify the region between START and END.
If LOUDLY is non-nil, display some debugging information."
(when (or loudly treesit--font-lock-verbose)
(message "Fontifying region: %s-%s" start end))
(treesit-update-ranges start end)
(font-lock-unfontify-region start end)
(let* ((local-parsers (treesit-local-parsers-on start end))
(global-parsers (treesit-parser-list))
(root-nodes
(mapcar #'treesit-parser-root-node
(append local-parsers global-parsers))))
(dolist (setting treesit-font-lock-settings)
(let* ((query (nth 0 setting))
(enable (nth 1 setting))
(override (nth 3 setting))
(language (treesit-query-language query))
(root-nodes (cl-remove-if-not
(lambda (node)
(eq (treesit-node-language node) language))
root-nodes)))
;; Use deterministic way to decide whether to turn on "fast
;; mode". (See bug#60691, bug#60223.)
(when (eq treesit--font-lock-fast-mode 'unspecified)
(pcase-let ((`(,max-depth ,max-width)
(treesit-subtree-stat
(treesit-buffer-root-node language))))
(if (or (> max-depth 100) (> max-width 4000))
(setq treesit--font-lock-fast-mode t)
(setq treesit--font-lock-fast-mode nil))))
;; Only activate if ENABLE flag is t.
(when-let
((activate (eq t enable))
(nodes (if (eq t treesit--font-lock-fast-mode)
(mapcan
(lambda (node)
(treesit--children-covering-range-recurse
node start end (* 4 jit-lock-chunk-size)))
root-nodes)
root-nodes)))
(ignore activate)
;; Query each node.
(dolist (sub-node nodes)
(treesit--font-lock-fontify-region-1
sub-node query start end override loudly))))))
`(jit-lock-bounds ,start . ,end))