unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#59445: 29.0.50; Tweak tree sitter faces in Java and TypeScript
@ 2022-11-21 12:13 Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-11-22  2:39 ` Randy Taylor
  2022-11-22 10:08 ` Yuan Fu via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 2 replies; 4+ messages in thread
From: Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-11-21 12:13 UTC (permalink / raw)
  To: 59445

[-- Attachment #1: Type: text/plain, Size: 104 bytes --]


Hi Yuan!

I tweaked Java and TypeScript a little.

Feel free to apply when you have the time :)

Theo


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Tweak-faces-in-Java-and-TypeScript.patch --]
[-- Type: text/x-diff, Size: 8764 bytes --]

From 5fa011d763f6c799766361104e5372e10a31b54c Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
Date: Mon, 21 Nov 2022 13:12:03 +0100
Subject: [PATCH] Tweak faces in Java and TypeScript

* lisp/progmodes/java-ts-mode.el (java-ts-mode--operators): Remove @
as an operator.

(java-ts-mode--font-lock-settings): Use constant-face for @ to match
rest of the annotation.  Add bracket, delimiter and use some of the
new faces.

(java-ts-mode--imenu): Clean up the implementation a little.

(java-ts-mode): Refer to the new features.

* lisp/progmodes/ts-mode.el (ts-mode--font-lock-settings, ts-mode):
Add in bracket and delimiter'.
---
 lisp/progmodes/java-ts-mode.el | 85 ++++++++++++++++++----------------
 lisp/progmodes/ts-mode.el      | 21 ++++++---
 2 files changed, 60 insertions(+), 46 deletions(-)

diff --git a/lisp/progmodes/java-ts-mode.el b/lisp/progmodes/java-ts-mode.el
index 62962b7293..d6e8a9d638 100644
--- a/lisp/progmodes/java-ts-mode.el
+++ b/lisp/progmodes/java-ts-mode.el
@@ -111,18 +111,14 @@ java-ts-mode--keywords
   "C keywords for tree-sitter font-locking.")
 
 (defvar java-ts-mode--operators
-  '("@" "+" ":" "++" "-" "--" "&" "&&" "|" "||"
-    "!=" "==" "*" "/" "%" "<" "<=" ">" ">=" "="
-    "-=" "+=" "*=" "/=" "%=" "->" "^" "^=" "&="
-    "|=" "~" ">>" ">>>" "<<" "::" "?")
+  '("+" ":" "++" "-" "--" "&" "&&" "|" "||" "="
+    "!=" "==" "*" "/" "%" "<" "<=" ">" ">="
+    "-=" "+=" "*=" "/=" "%=" "->" "^" "^="
+    "|=" "~" ">>" ">>>" "<<" "::" "?" "&=")
   "C operators for tree-sitter font-locking.")
 
 (defvar java-ts-mode--font-lock-settings
   (treesit-font-lock-rules
-   :language 'java
-   :override t
-   :feature 'basic
-   '((identifier) @font-lock-variable-name-face)
    :language 'java
    :override t
    :feature 'comment
@@ -144,7 +140,8 @@ java-ts-mode--font-lock-settings
    :language 'java
    :override t
    :feature 'operator
-   `([,@java-ts-mode--operators] @font-lock-builtin-face)
+   `([,@java-ts-mode--operators] @font-lock-operator-face
+     "@" @font-lock-constant-face)
    :language 'java
    :override t
    :feature 'annotation
@@ -186,6 +183,8 @@ java-ts-mode--font-lock-settings
 
      (method_reference (identifier) @font-lock-type-face)
 
+     (scoped_identifier (identifier) @font-lock-variable-name-face)
+
      ((scoped_identifier name: (identifier) @font-lock-type-face)
       (:match "^[A-Z]" @font-lock-type-face))
 
@@ -201,6 +200,12 @@ java-ts-mode--font-lock-settings
    `((method_declaration
       name: (identifier) @font-lock-function-name-face)
 
+     (variable_declarator
+      name: (identifier) @font-lock-variable-name-face)
+
+     (element_value_pair
+      key: (identifier) @font-lock-property-face)
+
      (formal_parameter
       name: (identifier) @font-lock-variable-name-face)
 
@@ -215,7 +220,15 @@ java-ts-mode--font-lock-settings
      (method_invocation
       name: (identifier) @font-lock-function-name-face)
 
-     (argument_list (identifier) @font-lock-variable-name-face)))
+     (argument_list (identifier) @font-lock-variable-name-face))
+
+   :language 'java
+   :feature 'bracket
+   '((["(" ")" "[" "]" "{" "}"]) @font-lock-bracket-face)
+
+   :language 'java
+   :feature 'delimiter
+   '((["," ":" ";"]) @font-lock-delimiter-face))
   "Tree-sitter font-lock settings.")
 
 (defun java-ts-mode--imenu-1 (node)
@@ -243,33 +256,27 @@ java-ts-mode--imenu-1
 (defun java-ts-mode--imenu ()
   "Return Imenu alist for the current buffer."
   (let* ((node (treesit-buffer-root-node))
-         (class-tree
-          `("Class" . ,(java-ts-mode--imenu-1
-                        (treesit-induce-sparse-tree
-                         node "^class_declaration$" nil 1000))))
-         (interface-tree
-          `("Interface" . ,(java-ts-mode--imenu-1
-                            (treesit-induce-sparse-tree
-                             node "^interface_declaration$"  nil 1000))))
-         (enum-tree
-          `("Enum" . ,(java-ts-mode--imenu-1
-                       (treesit-induce-sparse-tree
-                        node "^enum_declaration$"  nil 1000))))
-         (record-tree
-          `("Record" . ,(java-ts-mode--imenu-1
-                         (treesit-induce-sparse-tree
-                          node "^record_declaration$"  nil 1000))))
-         (method-tree
-          `("Method" . ,(java-ts-mode--imenu-1
-                         (treesit-induce-sparse-tree
-                          node "^method_declaration$"  nil 1000)))))
-    (cl-remove-if
-     #'null
-     `(,(when (cdr class-tree) class-tree)
-       ,(when (cdr interface-tree) interface-tree)
-       ,(when (cdr enum-tree) enum-tree)
-       ,(when (cdr record-tree) record-tree)
-       ,(when (cdr method-tree) method-tree)))))
+         (class-tree (treesit-induce-sparse-tree
+                      node "^class_declaration$" nil 1000))
+         (interface-tree (treesit-induce-sparse-tree
+                          node "^interface_declaration$" nil 1000))
+         (enum-tree (treesit-induce-sparse-tree
+                     node "^enum_declaration$" nil 1000))
+         (record-tree (treesit-induce-sparse-tree
+                       node "^record_declaration$"  nil 1000))
+         (method-tree (treesit-induce-sparse-tree
+                       node "^method_declaration$" nil 1000))
+         (class-index (java-ts-mode--imenu-1 class-tree))
+         (interface-index (java-ts-mode--imenu-1 interface-tree))
+         (enum-index (java-ts-mode--imenu-1 enum-tree))
+         (record-index (java-ts-mode--imenu-1 record-tree))
+         (method-index (java-ts-mode--imenu-1 method-tree)))
+    (append
+     (when class-index `(("Class" . ,class-index)))
+     (when interface-index `(("Interface" . ,interface-index)))
+     (when enum-index `(("Enum" . ,enum-index)))
+     (when record-index `(("Record" . ,record-index)))
+     (when method-index `(("Method" . ,method-index))))))
 
 ;;;###autoload
 (define-derived-mode java-ts-mode prog-mode "Java"
@@ -302,9 +309,9 @@ java-ts-mode
   ;; Font-lock.
   (setq-local treesit-font-lock-settings java-ts-mode--font-lock-settings)
   (setq-local treesit-font-lock-feature-list
-              '((basic comment keyword constant string operator)
+              '((comment keyword constant string)
                 (type definition expression literal annotation)
-                ()))
+                (bracket delimiter operator)))
 
   ;; Imenu.
   (setq-local imenu-create-index-function #'java-ts-mode--imenu)
diff --git a/lisp/progmodes/ts-mode.el b/lisp/progmodes/ts-mode.el
index 01719a89ee..a91eba6501 100644
--- a/lisp/progmodes/ts-mode.el
+++ b/lisp/progmodes/ts-mode.el
@@ -210,18 +210,18 @@ ts-mode--font-lock-settings
    :language 'tsx
    :override t
    :feature 'property
-   `((pair key: (property_identifier) @font-lock-variable-name-face)
+   `((pair key: (property_identifier) @font-lock-property-face)
 
      (pair value: (identifier) @font-lock-variable-name-face)
 
      (pair
-      key: (property_identifier) @font-lock-function-name-face
+      key: (property_identifier) @font-lock-property-face
       value: [(function) (arrow_function)])
 
      (property_signature
-      name: (property_identifier) @font-lock-variable-name-face)
+      name: (property_identifier) @font-lock-property-face)
 
-     ((shorthand_property_identifier) @font-lock-variable-name-face)
+     ((shorthand_property_identifier) @font-lock-property-face)
 
      ((shorthand_property_identifier_pattern)
       @font-lock-variable-name-face))
@@ -230,7 +230,7 @@ ts-mode--font-lock-settings
    :override t
    :feature 'pattern
    `((pair_pattern
-      key: (property_identifier) @font-lock-variable-name-face)
+      key: (property_identifier) @font-lock-property-face)
 
      (array_pattern (identifier) @font-lock-variable-name-face))
 
@@ -249,7 +249,14 @@ ts-mode--font-lock-settings
       [(nested_identifier (identifier)) (identifier)]
       @font-lock-function-name-face)
 
-     (jsx_attribute (property_identifier) @font-lock-constant-face)))
+     (jsx_attribute (property_identifier) @font-lock-constant-face))
+   :language 'tsx
+   :feature 'bracket
+   '((["(" ")" "[" "]" "{" "}"]) @font-lock-bracket-face)
+
+   :language 'tsx
+   :feature 'delimiter
+   '((["," ":" ";"]) @font-lock-delimiter-face))
   "Tree-sitter font-lock settings.")
 
 ;;;###autoload
@@ -297,7 +304,7 @@ ts-mode
     (setq-local treesit-font-lock-feature-list
                 '((comment declaration)
                   (string keyword identifier expression constant)
-                  (property pattern jsx)))
+                  (property pattern jsx bracket delimiter)))
     ;; Imenu.
     (setq-local imenu-create-index-function #'js--treesit-imenu)
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* bug#59445: 29.0.50; Tweak tree sitter faces in Java and TypeScript
  2022-11-21 12:13 bug#59445: 29.0.50; Tweak tree sitter faces in Java and TypeScript Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2022-11-22  2:39 ` Randy Taylor
  2022-11-22 10:08 ` Yuan Fu via Bug reports for GNU Emacs, the Swiss army knife of text editors
  1 sibling, 0 replies; 4+ messages in thread
From: Randy Taylor @ 2022-11-22  2:39 UTC (permalink / raw)
  To: Theodor Thornhill; +Cc: 59445

On Monday, November 21st, 2022 at 07:13, Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org> wrote:

> 
> Hi Yuan!
> 
> I tweaked Java and TypeScript a little.
> 
> Feel free to apply when you have the time :)
> 
> Theo

Looks good.

For java-ts-mode.el can you show examples of what bracket and delimiter highlight with your changes? I couldn't get them to highlight anything when I tested and I had the exact same queries as you, hence why I left them out of my patch. Similar thing occurred with sh-script.el.





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#59445: 29.0.50; Tweak tree sitter faces in Java and TypeScript
  2022-11-21 12:13 bug#59445: 29.0.50; Tweak tree sitter faces in Java and TypeScript Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-11-22  2:39 ` Randy Taylor
@ 2022-11-22 10:08 ` Yuan Fu via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2022-11-22 22:31   ` Randy Taylor
  1 sibling, 1 reply; 4+ messages in thread
From: Yuan Fu via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2022-11-22 10:08 UTC (permalink / raw)
  To: Randy Taylor; +Cc: 59445, Theodor Thornhill


Randy Taylor <dev@rjt.dev> writes:

> On Monday, November 21st, 2022 at 07:13, Theodor Thornhill via "Bug
> reports for GNU Emacs, the Swiss army knife of text editors"
> <bug-gnu-emacs@gnu.org> wrote:
>
>> 
>> Hi Yuan!
>> 
>> I tweaked Java and TypeScript a little.
>> 
>> Feel free to apply when you have the time :)
>> 
>> Theo

Thanks, I just applied this patch.

>
> Looks good.
>
> For java-ts-mode.el can you show examples of what bracket and
> delimiter highlight with your changes? I couldn't get them to
> highlight anything when I tested and I had the exact same queries as
> you, hence why I left them out of my patch. Similar thing occurred
> with sh-script.el.

Have you tried using treesit-query-capture to "manually" capture these
nodes? I always use

(treesit-query-capture (treesit-buffer-root-node) <query> (region-beginning) (region-end))

Yuan





^ permalink raw reply	[flat|nested] 4+ messages in thread

* bug#59445: 29.0.50; Tweak tree sitter faces in Java and TypeScript
  2022-11-22 10:08 ` Yuan Fu via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2022-11-22 22:31   ` Randy Taylor
  0 siblings, 0 replies; 4+ messages in thread
From: Randy Taylor @ 2022-11-22 22:31 UTC (permalink / raw)
  To: Yuan Fu; +Cc: 59445, Theodor Thornhill

On Tuesday, November 22nd, 2022 at 05:08, Yuan Fu via "Bug reports for GNU Emacs, the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org> wrote:
> 
> Randy Taylor dev@rjt.dev writes:
> 
> > On Monday, November 21st, 2022 at 07:13, Theodor Thornhill via "Bug
> > reports for GNU Emacs, the Swiss army knife of text editors"
> > bug-gnu-emacs@gnu.org wrote:
> > 
> > > Hi Yuan!
> > > 
> > > I tweaked Java and TypeScript a little.
> > > 
> > > Feel free to apply when you have the time :)
> > > 
> > > Theo
> 
> 
> Thanks, I just applied this patch.
> 
> > Looks good.
> > 
> > For java-ts-mode.el can you show examples of what bracket and
> > delimiter highlight with your changes? I couldn't get them to
> > highlight anything when I tested and I had the exact same queries as
> > you, hence why I left them out of my patch. Similar thing occurred
> > with sh-script.el.
> 
> 
> Have you tried using treesit-query-capture to "manually" capture these
> nodes? I always use
> 
> (treesit-query-capture (treesit-buffer-root-node) <query> (region-beginning) (region-end))
> 
> 
> Yuan
> 

No, I've never tried that before. But I found out what the problem was:

   '(["(" ")" "[" "]" "{" "}"] @font-lock-bracket-face)

The above query works fine for javascript and typescript, but not for java. Java needs it to be (note the parens surrounding the brackets):

   '((["(" ")" "[" "]" "{" "}"]) @font-lock-bracket-face)





^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-11-22 22:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-21 12:13 bug#59445: 29.0.50; Tweak tree sitter faces in Java and TypeScript Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-22  2:39 ` Randy Taylor
2022-11-22 10:08 ` Yuan Fu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2022-11-22 22:31   ` Randy Taylor

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).