]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix treesit-query-capture
authorYuan Fu <casouri@gmail.com>
Tue, 6 Dec 2022 02:37:47 +0000 (18:37 -0800)
committerYuan Fu <casouri@gmail.com>
Tue, 6 Dec 2022 03:56:47 +0000 (19:56 -0800)
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
lisp/treesit.el
src/treesit.c

index b82886e39740b4d74392cb7fe3b3285768a8eafa..8a66986dc6f9a39151936d37021d6af32d6fe8c8 100644 (file)
@@ -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)))
index eee6eee0c7f1e493fac33d3e4d6c1f1520a97f59..dbbf7ec18c3b874575eca75b55e70f2314736b76 100644 (file)
@@ -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.
index 4b150059fac3a46f1c2d08d0aea7f53f52a421f2..343054ed53e447b71d9b4828076e6b3a38a425a3 100644 (file)
@@ -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.  */