emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

feature/tree-sitter a3651d3237 4/8: Query on the root node in tree-sitte


From: Yuan Fu
Subject: feature/tree-sitter a3651d3237 4/8: Query on the root node in tree-sitter font-lock
Date: Tue, 1 Nov 2022 16:28:16 -0400 (EDT)

branch: feature/tree-sitter
commit a3651d3237e91f788211e507d61c2dcf886c185f
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    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 72c8186044..733d721f10 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)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]