aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuan Fu2022-12-05 18:37:47 -0800
committerYuan Fu2022-12-05 19:56:47 -0800
commitc26fe45cb8046eecaf3a74e3e7d4bc62ab511a8c (patch)
tree3cdc961f440fa79f0d9c48901c9f961d525787a8
parent318bf42b410d4a8ecf0e8ff64280cfd655884877 (diff)
downloademacs-c26fe45cb8046eecaf3a74e3e7d4bc62ab511a8c.tar.gz
emacs-c26fe45cb8046eecaf3a74e3e7d4bc62ab511a8c.zip
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).
-rw-r--r--lisp/textmodes/css-mode.el5
-rw-r--r--lisp/treesit.el7
-rw-r--r--src/treesit.c11
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.
1839 '((selector comment query keyword) 1839 '((selector comment query keyword)
1840 (property constant string) 1840 (property constant string)
1841 (error variable function operator bracket))) 1841 (error variable function operator bracket)))
1842 ;; Tree-sitter-css, for whatever reason, cannot reliably return
1843 ;; the captured nodes in a given range (it instead returns the
1844 ;; nodes preceding range). Before this is fixed in
1845 ;; tree-sitter-css, use this heuristic as a temporary fix.
1846 (setq-local treesit--font-lock-query-expand-range (cons 80 80))
1847 (setq-local imenu-create-index-function #'css--treesit-imenu) 1842 (setq-local imenu-create-index-function #'css--treesit-imenu)
1848 (setq-local which-func-functions nil) 1843 (setq-local which-func-functions nil)
1849 (treesit-major-mode-setup))) 1844 (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
545buffer, Emacs will move the start of the query range backward by 545buffer, Emacs will move the start of the query range backward by
546START amount, and the end of the query range by END amount. Both 546START amount, and the end of the query range by END amount. Both
547START and END should be positive integers or 0. This doesn't 547START and END should be positive integers or 0. This doesn't
548affect the fontified range. 548affect the fontified range.")
549
550Sometimes, querying on some parser with a restricted range
551returns nodes not in that range but before it, which breaks
552fontification. Major modes can adjust this variable as a
553temporarily fix.")
554 549
555(defvar-local treesit-font-lock-feature-list nil 550(defvar-local treesit-font-lock-feature-list nil
556 "A list of lists of feature symbols. 551 "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. */)
2507 /* Set query range. */ 2507 /* Set query range. */
2508 if (!NILP (beg) && !NILP (end)) 2508 if (!NILP (beg) && !NILP (end))
2509 { 2509 {
2510 EMACS_INT beg_byte = XFIXNUM (beg); 2510 EMACS_INT beg_byte = buf_charpos_to_bytepos (current_buffer,
2511 EMACS_INT end_byte = XFIXNUM (end); 2511 XFIXNUM (beg));
2512 EMACS_INT end_byte = buf_charpos_to_bytepos (current_buffer,
2513 XFIXNUM (end));
2512 /* We never let tree-sitter run on buffers too large, so these 2514 /* We never let tree-sitter run on buffers too large, so these
2513 assertion should never hit. */ 2515 assertion should never hit. */
2514 eassert (beg_byte - visible_beg <= UINT32_MAX); 2516 eassert (beg_byte - visible_beg <= UINT32_MAX);
2515 eassert (end_byte - visible_beg <= UINT32_MAX); 2517 eassert (end_byte - visible_beg <= UINT32_MAX);
2516 ts_query_cursor_set_byte_range (cursor, (uint32_t) beg_byte - visible_beg, 2518 ts_query_cursor_set_byte_range (cursor,
2517 (uint32_t) end_byte - visible_beg); 2519 (uint32_t) (beg_byte - visible_beg),
2520 (uint32_t) (end_byte - visible_beg));
2518 } 2521 }
2519 2522
2520 /* Execute query. */ 2523 /* Execute query. */