Subject: Re: bug#61913: java-ts-mode query error for string highlight, due to recent commit in tree-sitter-java.

From: Dmitry Gutov <[email-masked]>
Date: Thu, 9 Mar 2023 19:08:14 +0200

On 09/03/2023 18:49, Vincenzo Pupillo wrote:
> treesit-query-validate calls treesit-query-capture which is defined in treesit.c.
> It's a wrapper for interactive use, I guess it's not faster.

Fair point, but there is a certain difference: treesit-query-validate 
creates a temporary buffer to run treesit-query-capture in. Whereas, in 
your patch, treesit-query-capture is called in the current buffer, 
whichever buffer is current when the code is loaded, I suppose.

If the buffer is large, that might take a non-negligible amount of time.

So okay, reusing treesit-query-validate is not a good idea, but we could 
use the same pattern here, or try treesit-query-string which also uses a 
temp buffer:

diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el
index fca00cdce4f..0db6c9822ed 100644
--- a/lisp/progmodes/java-ts-mode.el
+++ b/lisp/progmodes/java-ts-mode.el
@@ -148,13 +148,13 @@ java-ts-mode--operators
    "Java operators for tree-sitter font-locking.")

  (defun java-ts-mode--string-highlight-helper ()
-"Returns, for strings, a query based on what is supported by
+  "Returns, for strings, a query based on what is supported by
  the available version of Tree-sitter for java."
    (condition-case nil
-      (progn (treesit-query-capture 'java '((text_block) 
@font-lock-string-face))
+      (progn (treesit-query-string "" '((text_block) 
@font-lock-string-face) 'java)
  	     `((string_literal) @font-lock-string-face
  	       (text_block) @font-lock-string-face))
-    (error
+    (treesit-query-error
       `((string_literal) @font-lock-string-face))))

  (defvar java-ts-mode--font-lock-settings