[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
feature/tree-sitter 7144e38da8 5/5: Tweak faces in Java and TypeScript
From: |
Yuan Fu |
Subject: |
feature/tree-sitter 7144e38da8 5/5: Tweak faces in Java and TypeScript |
Date: |
Tue, 22 Nov 2022 02:55:12 -0500 (EST) |
branch: feature/tree-sitter
commit 7144e38da828c885b6d11c96bbccbd72f527c162
Author: Theodor Thornhill <theo@thornhill.no>
Commit: Yuan Fu <casouri@gmail.com>
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 ee8ac31f67..e78f1b4c6e 100644
--- a/lisp/progmodes/java-ts-mode.el
+++ b/lisp/progmodes/java-ts-mode.el
@@ -116,18 +116,14 @@
"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
@@ -149,7 +145,8 @@
: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
@@ -191,6 +188,8 @@
(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))
@@ -206,6 +205,12 @@
`((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)
@@ -220,7 +225,15 @@
(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)
@@ -248,33 +261,27 @@ the subtrees."
(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"
@@ -307,9 +314,9 @@ the subtrees."
;; 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 @@
: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 @@
: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 @@
[(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 @@
(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)