Subject: Re: 29.0.60; json-ts-mode fontifies everything as string

From: Theodor Thornhill <[email-masked]>
Date: Mon, 05 Dec 2022 15:12:46 +0100

Randy Taylor <[email-masked]> writes:

> On Monday, December 5th, 2022 at 08:52, Randy Taylor <[email-masked]> wrote:
>> 
>> On Monday, December 5th, 2022 at 06:24, Theodor Thornhill [email-masked] wrote:
>> 
>> > Jostein Kjønigsen [email-masked] writes:
>> > 
>> > > Hey everyone.
>> > > 
>> > > When opening a JSON-file in a build from emacs git emacs-29
>> > > release-branch, Emacs still defaults to json-mode.
>> > > 
>> > > Fontification in json-mode looks correct and proper.
>> > > 
>> > > When trying to insteas use json-ts-mode, literally everything is
>> > > fontified as strings.
>> > > 
>> > > When I change the code to disable the string-fontification rule, this
>> > > yields a buffer where everything is fontified using default-face. That
>> > > is, no other fontification is taking place.
>> > > 
>> > > Have this mode really been tested? Should it be working? Or is there an
>> > > error on my end?
>> > > 
>> > > Could someone else please give json-ts-mode a try?
>> > 
>> > Yeah, you are correct. It seems we've had a regression in this commit,
>> > 0ed313642227afdeccf48057eea458edca9d2962.
>> > 
>> > I believe this is the offending commit - what do you think, Randy?
>> > 
>> > Specifically the lhs should be fontified differently than the rhs, so
>> > not everything is a string.
>> > 
>> > Do you want to do that, Randy, or should I?
>> > 
>> > Theo
>> 
>> 
>> Can someone show me an example JSON file that shows this problem? Everything looks the same for me in json-mode and json-ts-mode, except for the json-ts-mode extras (even with Theo's patch) both with my own Emacs config and emacs -Q. I must be doing something very silly.
>
> Scratch that, I got Theo's patch working, needed to rebuild.

No need to rebuild. Just C-M-x the defvar and revert buffer.

> Looks good to me (although it would be nice to keep treesit-font-lock-feature-list alphabetized Theo ;)).
>

See new patch!

> json-mode still fontifies pretty much everything as a string though (at least for me).

Not for me. I get number-face, bracket-face, constant-face,
warning-face.  Don't you?

Will you commit this, Yuan?

Theo

>From 60b9dfeaec8d194c0b162f3eaa4ae4a4736d68e9 Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <[email-masked]>
Date: Mon, 5 Dec 2022 12:34:08 +0100
Subject: [PATCH] Add back in font-locking for pair in json-ts-mode

* lisp/progmodes/json-ts-mode.el (json-ts-mode--font-lock-settings):
Add back in removed pair font-lock code.  Also alphabetize features.
(json-ts-mode): Use the new feature.

(bug#59833)
---
 lisp/progmodes/json-ts-mode.el | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/lisp/progmodes/json-ts-mode.el b/lisp/progmodes/json-ts-mode.el
index 101e873cf6..0ce820bafb 100644
--- a/lisp/progmodes/json-ts-mode.el
+++ b/lisp/progmodes/json-ts-mode.el
@@ -78,24 +78,30 @@ json-ts-mode--font-lock-settings
    '((["[" "]" "{" "}"]) @font-lock-bracket-face)
    :language 'json
    :feature 'constant
+   :override t
    '([(null) (true) (false)] @font-lock-constant-face)
    :language 'json
    :feature 'delimiter
    '((["," ":"]) @font-lock-delimiter-face)
    :language 'json
-   :feature 'number
-   '((number) @font-lock-number-face)
-   :language 'json
-   :feature 'string
-   '((string) @font-lock-string-face)
+   :feature 'error
+   :override t
+   '((ERROR) @font-lock-warning-face)
    :language 'json
    :feature 'escape-sequence
    :override t
    '((escape_sequence) @font-lock-escape-face)
    :language 'json
-   :feature 'error
+   :feature 'number
    :override t
-   '((ERROR) @font-lock-warning-face))
+   '((number) @font-lock-number-face)
+   :language 'json
+   :feature 'pair
+   :override t
+   `((pair key: (_) @font-lock-variable-name-face))
+   :language 'json
+   :feature 'string
+   '((string) @font-lock-string-face))
   "Font-lock settings for JSON.")
 
 (defun json-ts-mode--imenu-1 (node)
@@ -156,7 +162,7 @@ json-ts-mode
   ;; Font-lock.
   (setq-local treesit-font-lock-settings json-ts-mode--font-lock-settings)
   (setq-local treesit-font-lock-feature-list
-              '((constant number string)
+              '((constant number string pair)
                 (escape-sequence)
                 (bracket delimiter error)))
 
-- 
2.34.1