summaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authorYuan Fu <casouri@gmail.com>2022-11-20 20:29:53 -0800
committerYuan Fu <casouri@gmail.com>2022-11-21 01:29:31 -0800
commit938e68bb280c96eaf163bd932ab2582fbe27e780 (patch)
tree796388ce80fd7e71cd0ecbad041c8ac346c03295 /lisp
parent3069849bd4c1048a8f8c0467d26560fad939e791 (diff)
downloademacs-938e68bb280c96eaf163bd932ab2582fbe27e780.tar.gz
emacs-938e68bb280c96eaf163bd932ab2582fbe27e780.tar.bz2
emacs-938e68bb280c96eaf163bd932ab2582fbe27e780.zip
Fix tree-sitter fontification heuristic
Previously applied heuristic 2 sometimes invalidates heuristic 1, add a guard so it doesn't. The new function is just for clearity of the code and has nothing to do with the change itself. * lisp/treesit.el (treesit--node-length): New function (treesit-font-lock-fontify-region): Use the new function. Only do heuristic 2 when the node is large.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/treesit.el26
1 files changed, 16 insertions, 10 deletions
diff --git a/lisp/treesit.el b/lisp/treesit.el
index f9f807947c2..7af64b4392c 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -779,6 +779,10 @@ range is between START and END."
;; If NODE has no child, keep NODE.
(or result node)))
+(defsubst treesit--node-length (node)
+ "Return the length of the text of NODE."
+ (- (treesit-node-end node) (treesit-node-start node)))
+
;; Some details worth explaining:
;;
;; 1. When we apply face to a node, we clip the face into the
@@ -817,19 +821,21 @@ If LOUDLY is non-nil, display some debugging information."
(activate (eq t enable)))
(ignore activate)
- ;; The node seems small, enlarge it.
- (while (and (< (- (treesit-node-end node-on)
- (treesit-node-start node-on))
- 40)
+ ;; Heuristic 1: The node seems small, enlarge it.
+ (while (and (< (treesit--node-length node-on) 40)
(treesit-node-parent node-on))
(setq node-on (treesit-node-parent node-on)))
- ;; Maybe the node returned by `treesit-node-on' is the root
- ;; node, because the region between START and END contains
- ;; several top-level constructs (e.g., variable declarations
- ;; in C).
- (setq node-on (treesit--children-covering-range
- node-on start end))
+ ;; Heuristic 2: Maybe the node returned by `treesit-node-on'
+ ;; is the root node or some excessively large node, because
+ ;; the region between START and END contains several top-level
+ ;; constructs (e.g., variable declarations in C), try find a
+ ;; list of children that spans the fontification range.
+ (if (> (treesit--node-length node-on)
+ (* 4 (max jit-lock-chunk-size (- end start))))
+ (setq node-on (treesit--children-covering-range
+ node-on start end))
+ (setq node-on (list node-on)))
(dolist (sub-node node-on)
(let ((captures (treesit-query-capture