]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix incorrect tree-sitter fontification
authorYuan Fu <casouri@gmail.com>
Sun, 6 Nov 2022 01:58:39 +0000 (18:58 -0700)
committerYuan Fu <casouri@gmail.com>
Sun, 6 Nov 2022 02:00:17 +0000 (19:00 -0700)
* lisp/treesit.el (treesit-font-lock-fontify-region): If the captured
node is outside of the region between START and END, don't fontify it.
Wrap fontification code in a when form.

lisp/treesit.el

index 7233ce7f18bc7d154e9c45604501f89c6d1c32a4..84be69d8b72174055ee3de95297f7384933d5f17 100644 (file)
@@ -846,23 +846,30 @@ If LOUDLY is non-nil, display some debugging information."
                      (node (cdr capture))
                      (node-start (treesit-node-start node))
                      (node-end (treesit-node-end node)))
-                (cond
-                 ((eq face 'contextual)
-                  (treesit-font-lock-contextual-post-process
-                   node start end
-                   (or loudly treesit--font-lock-verbose)))
-                 ((facep face)
-                  (treesit-fontify-with-override
-                   (max node-start start) (min node-end end)
-                   face override))
-                 ((functionp face)
-                  (funcall face node override start end)))
-                ;; Don't raise an error if FACE is neither a face nor
-                ;; a function.  This is to allow intermediate capture
-                ;; names used for #match and #eq.
-                (when (or loudly treesit--font-lock-verbose)
-                  (message "Fontifying text from %d to %d, Face: %s, Node: %s"
-                           start end face (treesit-node-type node))))))))))
+                ;; Turns out it is possible to capture a node that's
+                ;; completely outside the region between START and
+                ;; END.  If the node is outside of that region, (max
+                ;; node-start start) and friends return bad values.
+                (when (and (< start node-end)
+                           (< node-start end))
+                  (cond
+                   ((eq face 'contextual)
+                    (treesit-font-lock-contextual-post-process
+                     node start end
+                     (or loudly treesit--font-lock-verbose)))
+                   ((facep face)
+                    (treesit-fontify-with-override
+                     (max node-start start) (min node-end end)
+                     face override))
+                   ((functionp face)
+                    (funcall face node override start end)))
+                  ;; Don't raise an error if FACE is neither a face nor
+                  ;; a function.  This is to allow intermediate capture
+                  ;; names used for #match and #eq.
+                  (when (or loudly treesit--font-lock-verbose)
+                    (message "Fontifying text from %d to %d, Face: %s, Node: %s"
+                             (max node-start start) (min node-end end)
+                             face (treesit-node-type node)))))))))))
   `(jit-lock-bounds ,start . ,end))
 
 ;;; Indent