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

From: Vincenzo Pupillo <[email-masked]>
Date: Mon, 06 Mar 2023 15:03:46 +0100

Is this patch okay? Please feel free to make any changes you see fit.
Thank you.

Vincenzo

In data lunedì 6 marzo 2023 07:12:49 CET, Theodor Thornhill ha scritto:
> Eli Zaretskii <[email-masked]> writes:
> 
> >> From: Vincenzo Pupillo <[email-masked]>
> >> Date: Sun, 5 Mar 2023 14:50:39 +0100
> >> Cc: Eli Zaretskii <[email-masked]>, [email-masked]
> >> 
> >> nvim-treesiter seems to use a "lockfile" in which they put the version of the language parser to be installed
> >> (https://github.com/nvim-treesitter/nvim-treesitter/blob/master/lockfile.json) to allow some stability I suppose.
> >
> > Those seem to be Git SHA hashes, but how can Emacs verify that the
> > particular grammar shared library is of that revision or later?  We
> > can only tell users to verify that, which is not very reliable, and
> > requires users to clone the repository.
> 
> We could keep a header in the *-ts-mode.el file maybe?  Something like:
> 
> 
> ;;; Grammar version: 95e84b5493e8d21060f1a905d2e5e3c5590bae45
> 
> 
> Anyway, I think until we have such a system in place, we should strive
> to adhere to the most recent version of the grammar.  The diff is ok,
> Vicenzo.  Would you like to add a proper patch or should I just do it?
> I mean with a commit message and format like in CONTRIBUTE?
> 
> Theo
> 
>From 72f32b71d82b925e5b4aa5d6bebc707aaca992af Mon Sep 17 00:00:00 2001
From: Vincenzo Pupillo <[email-masked]>
Date: Mon, 6 Mar 2023 12:24:34 +0100
Subject: [PATCH] java-ts-mode: detect if text_block is supported.

A recent commit in tree-sitter-java (issue #141) removed support for
the text_block node type from the grammar. To support the old released
version and the new one, a helper function was added on which the
string syntax highlighting now depends. (bug#61913)

* lisp/progmodes/java-ts-mode.el
(java-ts-mode--string-highlight-helper): New helper function that
return the right query string
(java-ts-mode--font-lock-settings): Use it
---
 lisp/progmodes/java-ts-mode.el | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el
index 1115eadde7..66fec3b0f1 100644
--- a/lisp/progmodes/java-ts-mode.el
+++ b/lisp/progmodes/java-ts-mode.el
@@ -145,6 +145,16 @@ 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
+the available version of Tree-sitter for java."
+  (condition-case nil
+      (progn (treesit-query-capture 'java '((text_block) @font-lock-string-face))
+	     `((string_literal) @font-lock-string-face
+	       (text_block) @font-lock-string-face))
+    (error
+     `((string_literal) @font-lock-string-face))))
+
 (defvar java-ts-mode--font-lock-settings
   (treesit-font-lock-rules
    :language 'java
@@ -182,8 +192,7 @@ java-ts-mode--font-lock-settings
    :language 'java
    :override t
    :feature 'string
-   `((string_literal) @font-lock-string-face
-     (text_block) @font-lock-string-face)
+   (java-ts-mode--string-highlight-helper)
    :language 'java
    :override t
    :feature 'literal
-- 
2.39.2