From a3651d3237e91f788211e507d61c2dcf886c185f Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Mon, 31 Oct 2022 15:27:40 -0700 Subject: [PATCH] Query on the root node in tree-sitter font-lock Rather than querying on the smallest node that spans START to END, we query on the root node between START to END. * lisp/progmodes/python.el (python--treesit-fontify-string): Accept the string rather than the quote node. (python--treesit-settings): Capture the string rather than the quote node. * lisp/treesit.el (treesit-font-lock-fontify-region): Query the root node rather than the smallest node. --- lisp/treesit.el | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lisp/treesit.el b/lisp/treesit.el index 72c8186044f..733d721f109 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -635,17 +635,18 @@ If LOUDLY is non-nil, display some debugging information." (enable (nth 1 setting)) (override (nth 3 setting)) (language (treesit-query-language query))) - (when-let ((node (treesit-node-on start end language)) + ;; Why root node rather than (treesit-node-on start end)? 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. + (when-let ((root (treesit-buffer-root-node language)) ;; Only activate if ENABLE flag is t. (activate (eq t enable))) (ignore activate) (let ((captures (treesit-query-capture - node query - ;; Specifying the range is important. More - ;; often than not, NODE will be the root - ;; node, and if we don't specify the range, - ;; we are basically querying the whole file. - start end)) + root query start end)) (inhibit-point-motion-hooks t)) (with-silent-modifications (dolist (capture captures) -- 2.39.5