Clean up font-lock rules in js-ts-mode

Changes for each feature:

- string: Take out string-interpolation bits.
- string-interpolation: New.
- declaration: Rename to definition.
- identifier: Remove.
- property: Use a pred to filter out methods.
- expression: Rename to assignment.
- function: New.
- pattern: Merge into assignment.

* lisp/progmodes/js.el (js--treesit-font-lock-settings): See above.
(js--treesit-property-not-function-p)
(js--treesit-lhs-identifier-query): New variable.
(js--treesit-fontify-assignment-lhs): New functions.
(js-ts-mode): Update feature list.
This commit is contained in:
Yuan Fu 2022-12-28 16:52:47 -08:00
parent 0a61e4e2b7
commit e78e69b331
No known key found for this signature in database
GPG key ID: 56E19BC57664A442

View file

@ -3498,17 +3498,19 @@ This function is intended for use in `after-change-functions'."
[(this) (super)] @font-lock-keyword-face) [(this) (super)] @font-lock-keyword-face)
:language 'javascript :language 'javascript
:override t
:feature 'string :feature 'string
`((regex pattern: (regex_pattern)) @font-lock-string-face '((regex pattern: (regex_pattern)) @font-lock-string-face
(string) @font-lock-string-face (string) @font-lock-string-face)
(template_string) @js--fontify-template-string
(template_substitution ["${" "}"] @font-lock-builtin-face))
:language 'javascript :language 'javascript
:feature 'string-interpolation
:override t :override t
:feature 'declaration '((template_string) @js--fontify-template-string
`((function (template_substitution ["${" "}"] @font-lock-delimiter-face))
:language 'javascript
:feature 'definition
'((function
name: (identifier) @font-lock-function-name-face) name: (identifier) @font-lock-function-name-face)
(class_declaration (class_declaration
@ -3535,24 +3537,10 @@ This function is intended for use in `after-change-functions'."
value: (array (number) (function)))) value: (array (number) (function))))
:language 'javascript :language 'javascript
:override t
:feature 'identifier
`((new_expression
constructor: (identifier) @font-lock-type-face)
(for_in_statement
left: (identifier) @font-lock-variable-name-face)
(arrow_function
parameter: (identifier) @font-lock-variable-name-face))
:language 'javascript
:override t
:feature 'property :feature 'property
;; This needs to be before function-name feature, because methods '(((property_identifier) @font-lock-property-face
;; can be both property and function-name, and we want them in (:pred js--treesit-property-not-function-p
;; function-name face. @font-lock-property-face))
`((property_identifier) @font-lock-property-face
(pair value: (identifier) @font-lock-variable-name-face) (pair value: (identifier) @font-lock-variable-name-face)
@ -3561,33 +3549,25 @@ This function is intended for use in `after-change-functions'."
((shorthand_property_identifier_pattern) @font-lock-property-face)) ((shorthand_property_identifier_pattern) @font-lock-property-face))
:language 'javascript :language 'javascript
:override t :feature 'assignment
:feature 'expression '((assignment_expression
`((assignment_expression left: (_) @js--treesit-fontify-assignment-lhs))
left: [(identifier) @font-lock-function-name-face
(member_expression property: (property_identifier)
@font-lock-function-name-face)]
right: [(function) (arrow_function)])
(call_expression :language 'javascript
:feature 'function
'((call_expression
function: [(identifier) @font-lock-function-name-face function: [(identifier) @font-lock-function-name-face
(member_expression (member_expression
property: property:
(property_identifier) @font-lock-function-name-face)]) (property_identifier) @font-lock-function-name-face)])
(method_definition
(assignment_expression name: (property_identifier) @font-lock-function-name-face)
left: [(identifier) @font-lock-variable-name-face (function_declaration
(member_expression name: (identifier) @font-lock-function-name-face)
property: (property_identifier) @font-lock-variable-name-face)])) (function
name: (identifier) @font-lock-function-name-face))
:language 'javascript :language 'javascript
:override t
:feature 'pattern
`((pair_pattern key: (property_identifier) @font-lock-variable-name-face)
(array_pattern (identifier) @font-lock-variable-name-face))
:language 'javascript
:override t
:feature 'jsx :feature 'jsx
`( `(
(jsx_opening_element (jsx_opening_element
@ -3657,6 +3637,31 @@ OVERRIDE is the override flag described in
(setq font-beg (treesit-node-end child) (setq font-beg (treesit-node-end child)
child (treesit-node-next-sibling child))))) child (treesit-node-next-sibling child)))))
(defun js--treesit-property-not-function-p (node)
"Check that NODE, a property_identifier, is not used as a function."
(not (equal (treesit-node-type
(treesit-node-parent ; Maybe call_expression.
(treesit-node-parent ; Maybe member_expression.
node)))
"call_expression")))
(defvar js--treesit-lhs-identifier-query
(treesit-query-compile 'javascript '((identifier) @id
(property_identifier) @id))
"Query that captures identifier and query_identifier.")
(defun js--treesit-fontify-assignment-lhs (node override start end &rest _)
"Fontify the lhs NODE of an assignment_expression.
For OVERRIDE, START, END, see `treesit-font-lock-rules'."
(dolist (node (treesit-query-capture
node js--treesit-lhs-identifier-query nil nil t))
(treesit-fontify-with-override
(treesit-node-start node) (treesit-node-end node)
(pcase (treesit-node-type node)
("identifier" 'font-lock-variable-name-face)
("property_identifier" 'font-lock-property-face))
override start end)))
(defun js--treesit-defun-name (node) (defun js--treesit-defun-name (node)
"Return the defun name of NODE. "Return the defun name of NODE.
Return nil if there is no name or if NODE is not a defun node." Return nil if there is no name or if NODE is not a defun node."
@ -3810,11 +3815,12 @@ Currently there are `js-mode' and `js-ts-mode'."
;; Fontification. ;; Fontification.
(setq-local treesit-font-lock-settings js--treesit-font-lock-settings) (setq-local treesit-font-lock-settings js--treesit-font-lock-settings)
(setq-local treesit-font-lock-feature-list (setq-local treesit-font-lock-feature-list
'(( comment declaration) '(( comment definition)
( keyword string) ( keyword string)
( constant escape-sequence expression ( assignment constant escape-sequence jsx number
identifier jsx number pattern property) pattern)
( bracket delimiter operator))) ( bracket delimiter function operator property
string-interpolation)))
;; Imenu ;; Imenu
(setq-local treesit-simple-imenu-settings (setq-local treesit-simple-imenu-settings
`(("Function" "\\`function_declaration\\'" nil nil) `(("Function" "\\`function_declaration\\'" nil nil)