]> git.eshelyaron.com Git - emacs.git/commitdiff
Add in new font lock features in js/ts-mode
authorTheodor Thornhill <theo@thornhill.no>
Fri, 28 Oct 2022 19:23:19 +0000 (21:23 +0200)
committerYuan Fu <casouri@gmail.com>
Fri, 28 Oct 2022 23:46:10 +0000 (16:46 -0700)
We want to support font lock features on the syntactic level, not only
the decoration-level level.

New supported features are:
  - comment
  - constant
  - keyword
  - string
  - declaration
  - identifier
  - expression
  - property
  - pattern
  - jsx

* lisp/progmodes/js.el (js--treesit-font-lock-settings, js-mode): Add
in the new features.

* lisp/progmodes/ts-mode.el (ts-mode--indent-rules): Remove faulty '.'
anchor.

(ts-mode--font-lock-settings, ts-mode): Add in the new features.

lisp/progmodes/js.el
lisp/progmodes/ts-mode.el

index dae6de1052a90162ecb5e5ed126f355450638652..d786fa7415a298ff7a20cb3e0dbedb51b5945001 100644 (file)
@@ -3463,28 +3463,32 @@ This function is intended for use in `after-change-functions'."
   (treesit-font-lock-rules
    :language 'javascript
    :override t
-   :feature 'minimal
-   `(
-     ((identifier) @font-lock-constant-face
+   :feature 'comment
+   `((comment) @font-lock-comment-face)
+   :language 'javascript
+   :override t
+   :feature 'constant
+   `(((identifier) @font-lock-constant-face
       (:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face))
 
-     [(this) (super)] @font-lock-keyword-face
-
      [(true) (false) (null)] @font-lock-constant-face
-     (regex pattern: (regex_pattern)) @font-lock-string-face
-     (number) @font-lock-constant-face
-
+     (number) @font-lock-constant-face)
+   :language 'javascript
+   :override t
+   :feature 'keyword
+   `([,@js--treesit-keywords] @font-lock-keyword-face
+     [(this) (super)] @font-lock-keyword-face)
+   :language 'javascript
+   :override t
+   :feature 'string
+   `((regex pattern: (regex_pattern)) @font-lock-string-face
      (string) @font-lock-string-face
-     (comment) @font-lock-comment-face
-     [,@js--treesit-keywords] @font-lock-keyword-face
-
      (template_string) @js--fontify-template-string
-     (template_substitution ["${" "}"] @font-lock-constant-face))
+     (template_substitution ["${" "}"] @font-lock-builtin-face))
    :language 'javascript
    :override t
-   :feature 'moderate
-   `(
-     (function
+   :feature 'declaration
+   `((function
       name: (identifier) @font-lock-function-name-face)
 
      (class_declaration
@@ -3499,18 +3503,6 @@ This function is intended for use in `after-change-functions'."
      (variable_declarator
       name: (identifier) @font-lock-variable-name-face)
 
-     (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 'full
-   `(
      (variable_declarator
       name: (identifier) @font-lock-function-name-face
       value: [(function) (arrow_function)])
@@ -3520,9 +3512,22 @@ This function is intended for use in `after-change-functions'."
              (identifier)
              (identifier)
              @font-lock-function-name-face)
-      value: (array (number) (function)))
+      value: (array (number) (function))))
+   :language 'javascript
+   :override t
+   :feature 'identifier
+   `((new_expression
+      constructor: (identifier) @font-lock-type-face)
 
-     (assignment_expression
+     (for_in_statement
+      left: (identifier) @font-lock-variable-name-face)
+
+     (arrow_function
+      parameter: (identifier) @font-lock-variable-name-face))
+   :language 'javascript
+   :override t
+   :feature 'expression
+   `((assignment_expression
       left: [(identifier) @font-lock-function-name-face
              (member_expression property: (property_identifier)
                                 @font-lock-function-name-face)]
@@ -3537,9 +3542,11 @@ This function is intended for use in `after-change-functions'."
      (assignment_expression
       left: [(identifier) @font-lock-variable-name-face
              (member_expression
-              property: (property_identifier) @font-lock-variable-name-face)])
-
-     (pair key: (property_identifier) @font-lock-variable-name-face)
+              property: (property_identifier) @font-lock-variable-name-face)]))
+   :language 'javascript
+   :override t
+   :feature 'property
+   `((pair key: (property_identifier) @font-lock-variable-name-face)
 
      (pair value: (identifier) @font-lock-variable-name-face)
 
@@ -3549,12 +3556,16 @@ This function is intended for use in `after-change-functions'."
 
      ((shorthand_property_identifier) @font-lock-variable-name-face)
 
-     (pair_pattern key: (property_identifier) @font-lock-variable-name-face)
-
-     ((shorthand_property_identifier_pattern) @font-lock-variable-name-face)
-
-     (array_pattern (identifier) @font-lock-variable-name-face)
-
+     ((shorthand_property_identifier_pattern) @font-lock-variable-name-face))
+   :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
+   `(
      (jsx_opening_element
       [(nested_identifier (identifier)) (identifier)]
       @font-lock-function-name-face)
@@ -3780,7 +3791,10 @@ definition*\"."
                         "lexical_declaration")))
     ;; Fontification.
     (setq-local treesit-font-lock-settings js--treesit-font-lock-settings)
-    (setq-local treesit-font-lock-feature-list '((minimal) (moderate) (full)))
+    (setq-local treesit-font-lock-feature-list
+                '((comment declaration)
+                  (string keyword identifier expression constant)
+                  (property pattern jsx )))
     ;; Imenu
     (setq-local imenu-create-index-function
                 #'js--treesit-imenu)
@@ -3802,8 +3816,7 @@ definition*\"."
     (add-hook 'syntax-propertize-extend-region-functions
               #'syntax-propertize-multiline 'append 'local)
     (add-hook 'syntax-propertize-extend-region-functions
-              #'js--syntax-propertize-extend-region 'append 'local)
-    )))
+              #'js--syntax-propertize-extend-region 'append 'local))))
 
 (defvar js-json--treesit-font-lock-settings
   (treesit-font-lock-rules
index 15b8cbf71101e93db5b7b4d21a577f24fd336de5..1f0a7fd5c4027d626fd913e74d8e5a40e38a354d 100644 (file)
@@ -60,8 +60,6 @@
      ((node-is ")") parent-bol 0)
      ((node-is "]") parent-bol 0)
      ((node-is ">") parent-bol 0)
-     ((node-is ".")
-      parent-bol ,ts-mode-indent-offset)
      ((parent-is "ternary_expression")
       parent-bol ,ts-mode-indent-offset)
      ((parent-is "member_expression")
   (treesit-font-lock-rules
    :language 'tsx
    :override t
-   :feature 'minimal
-   `(
-     ((identifier) @font-lock-constant-face
+   :feature 'comment
+   `((comment) @font-lock-comment-face)
+   :language 'tsx
+   :override t
+   :feature 'constant
+   `(((identifier) @font-lock-constant-face
       (:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face))
 
-     [,@ts-mode--keywords] @font-lock-keyword-face
-     [(this) (super)] @font-lock-keyword-face
-
      [(true) (false) (null)] @font-lock-constant-face
-     (regex pattern: (regex_pattern)) @font-lock-string-face
-     (number) @font-lock-constant-face
-
+     (number) @font-lock-constant-face)
+   :language 'tsx
+   :override t
+   :feature 'keyword
+   `([,@ts-mode--keywords] @font-lock-keyword-face
+     [(this) (super)] @font-lock-keyword-face)
+   :language 'tsx
+   :override t
+   :feature 'string
+   `((regex pattern: (regex_pattern)) @font-lock-string-face
      (string) @font-lock-string-face
-
      (template_string) @js--fontify-template-string
-     (template_substitution ["${" "}"] @font-lock-builtin-face)
-
-     (comment) @font-lock-comment-face)
+     (template_substitution ["${" "}"] @font-lock-builtin-face))
    :language 'tsx
    :override t
-   :feature 'moderate
-   '(
-     (nested_type_identifier
-      module: (identifier) @font-lock-type-face)
-
-     (type_identifier) @font-lock-type-face
-
-     (predefined_type) @font-lock-type-face
-
-     (new_expression
-      constructor: (identifier) @font-lock-type-face)
-
-     (function
+   :feature 'declaration
+   `((function
       name: (identifier) @font-lock-function-name-face)
 
      (function_declaration
 
      (enum_declaration (identifier) @font-lock-type-face)
 
+     (arrow_function
+      parameter: (identifier) @font-lock-variable-name-face)
+
+     (variable_declarator
+      name: (identifier) @font-lock-function-name-face
+      value: [(function) (arrow_function)])
+
+     (variable_declarator
+      name: (array_pattern
+             (identifier)
+             (identifier) @font-lock-function-name-face)
+      value: (array (number) (function))))
+   :language 'tsx
+   :override t
+   :feature 'identifier
+   `((nested_type_identifier
+      module: (identifier) @font-lock-type-face)
+
+     (type_identifier) @font-lock-type-face
+
+     (predefined_type) @font-lock-type-face
+
+     (new_expression
+      constructor: (identifier) @font-lock-type-face)
+
      (enum_body (property_identifier) @font-lock-type-face)
 
      (enum_assignment name: (property_identifier) @font-lock-type-face)
       left: (identifier) @font-lock-variable-name-face)
 
      (arrow_function
-      parameter: (identifier) @font-lock-variable-name-face))
+      parameters:
+      [(_ (identifier) @font-lock-variable-name-face)
+       (_ (_ (identifier) @font-lock-variable-name-face))
+       (_ (_ (_ (identifier) @font-lock-variable-name-face)))]))
    :language 'tsx
    :override t
-   :feature 'full
-   '(
-     (variable_declarator
-      name: (identifier) @font-lock-function-name-face
-      value: [(function) (arrow_function)])
-
-     (variable_declarator
-      name: (array_pattern
-             (identifier)
-             (identifier) @font-lock-function-name-face)
-      value: (array (number) (function)))
-
-     (assignment_expression
+   :feature 'expression
+   '((assignment_expression
       left: [(identifier) @font-lock-function-name-face
              (member_expression
               property: (property_identifier) @font-lock-function-name-face)]
       function:
       [(identifier) @font-lock-function-name-face
        (member_expression
-        property: (property_identifier) @font-lock-function-name-face)])
-
-     (arrow_function
-      parameters:
-      [(_ (identifier) @font-lock-variable-name-face)
-       (_ (_ (identifier) @font-lock-variable-name-face))
-       (_ (_ (_ (identifier) @font-lock-variable-name-face)))])
-
-     (pair key: (property_identifier) @font-lock-variable-name-face)
+        property: (property_identifier) @font-lock-function-name-face)]))
+   :language 'tsx
+   :override t
+   :feature 'property
+   `((pair key: (property_identifier) @font-lock-variable-name-face)
 
      (pair value: (identifier) @font-lock-variable-name-face)
 
 
      ((shorthand_property_identifier) @font-lock-variable-name-face)
 
-     (pair_pattern
-      key: (property_identifier) @font-lock-variable-name-face)
-
      ((shorthand_property_identifier_pattern)
-      @font-lock-variable-name-face)
-
-     (array_pattern (identifier) @font-lock-variable-name-face)
+      @font-lock-variable-name-face))
+   :language 'tsx
+   :override t
+   :feature 'pattern
+   `((pair_pattern
+      key: (property_identifier) @font-lock-variable-name-face)
 
-     (jsx_opening_element
+     (array_pattern (identifier) @font-lock-variable-name-face))
+   :language 'tsx
+   :override t
+   :feature 'jsx
+   `((jsx_opening_element
       [(nested_identifier (identifier)) (identifier)]
       @font-lock-function-name-face)
 
                         "lexical_declaration")))
     ;; Font-lock.
     (setq-local treesit-font-lock-settings ts-mode--font-lock-settings)
-    (setq-local treesit-font-lock-feature-list '((minimal) (moderate) (full)))
+    (setq-local treesit-font-lock-feature-list
+                '((comment declaration)
+                  (string keyword identifier expression constant)
+                  (property pattern jsx)))
     ;; Imenu.
     (setq-local imenu-create-index-function #'js--treesit-imenu)
     ;; Which-func (use imenu).