emacs-diffs
[Top][All Lists]
Advanced

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

emacs-29 c26fe45cb80: Fix treesit-query-capture


From: Yuan Fu
Subject: emacs-29 c26fe45cb80: Fix treesit-query-capture
Date: Mon, 5 Dec 2022 22:58:18 -0500 (EST)

branch: emacs-29
commit c26fe45cb8046eecaf3a74e3e7d4bc62ab511a8c
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>

    Fix treesit-query-capture
    
    Before this change Ftreesit_query_capture doesn't convert character
    position to byte position for BEG and END parameters.  I observed
    fontification issue in css files but couldn't figure out why, now I
    know :-)
    
    I decide to keep treesit--font-lock-query-expand-range, since it might
    provide a escape hatch for problems we discover in the future, and it
    should be very cheap so no downside of keeping it.
    
    * lisp/textmodes/css-mode.el (css-ts-mode): Stop setting
    treesit--font-lock-query-expand-range.
    * lisp/treesit.el (treesit--font-lock-query-expand-range): Update
    docstring.
    * src/treesit.c (Ftreesit_query_capture): Convert BEG and END to byte
    position.  Also added parentheses wround "beg_byte - visible_beg" in
    the call to ts_query_cursor_set_byte_range (i.e., style change).
---
 lisp/textmodes/css-mode.el |  5 -----
 lisp/treesit.el            |  7 +------
 src/treesit.c              | 11 +++++++----
 3 files changed, 8 insertions(+), 15 deletions(-)

diff --git a/lisp/textmodes/css-mode.el b/lisp/textmodes/css-mode.el
index b82886e3974..8a66986dc6f 100644
--- a/lisp/textmodes/css-mode.el
+++ b/lisp/textmodes/css-mode.el
@@ -1839,11 +1839,6 @@ can also be used to fill comments.
                 '((selector comment query keyword)
                   (property constant string)
                   (error variable function operator bracket)))
-    ;; Tree-sitter-css, for whatever reason, cannot reliably return
-    ;; the captured nodes in a given range (it instead returns the
-    ;; nodes preceding range).  Before this is fixed in
-    ;; tree-sitter-css, use this heuristic as a temporary fix.
-    (setq-local treesit--font-lock-query-expand-range (cons 80 80))
     (setq-local imenu-create-index-function #'css--treesit-imenu)
     (setq-local which-func-functions nil)
     (treesit-major-mode-setup)))
diff --git a/lisp/treesit.el b/lisp/treesit.el
index eee6eee0c7f..dbbf7ec18c3 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -545,12 +545,7 @@ This should be a cons cell (START . END).  When fontifying 
a
 buffer, Emacs will move the start of the query range backward by
 START amount, and the end of the query range by END amount.  Both
 START and END should be positive integers or 0.  This doesn't
-affect the fontified range.
-
-Sometimes, querying on some parser with a restricted range
-returns nodes not in that range but before it, which breaks
-fontification.  Major modes can adjust this variable as a
-temporarily fix.")
+affect the fontified range.")
 
 (defvar-local treesit-font-lock-feature-list nil
   "A list of lists of feature symbols.
diff --git a/src/treesit.c b/src/treesit.c
index 4b150059fac..343054ed53e 100644
--- a/src/treesit.c
+++ b/src/treesit.c
@@ -2507,14 +2507,17 @@ the query.  */)
   /* Set query range.  */
   if (!NILP (beg) && !NILP (end))
     {
-      EMACS_INT beg_byte = XFIXNUM (beg);
-      EMACS_INT end_byte = XFIXNUM (end);
+      EMACS_INT beg_byte = buf_charpos_to_bytepos (current_buffer,
+                                                  XFIXNUM (beg));
+      EMACS_INT end_byte = buf_charpos_to_bytepos (current_buffer,
+                                                  XFIXNUM (end));
       /* We never let tree-sitter run on buffers too large, so these
         assertion should never hit.  */
       eassert (beg_byte - visible_beg <= UINT32_MAX);
       eassert (end_byte - visible_beg <= UINT32_MAX);
-      ts_query_cursor_set_byte_range (cursor, (uint32_t) beg_byte - 
visible_beg,
-                                     (uint32_t) end_byte - visible_beg);
+      ts_query_cursor_set_byte_range (cursor,
+                                     (uint32_t) (beg_byte - visible_beg),
+                                     (uint32_t) (end_byte - visible_beg));
     }
 
   /* Execute query.  */



reply via email to

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