]> git.eshelyaron.com Git - emacs.git/commitdiff
Update JSX support due to upstream changes (bug#64647)
authorVincenzo Pupillo <v.pupillo@gmail.com>
Sat, 22 Jul 2023 11:38:48 +0000 (13:38 +0200)
committerTheodor Thornhill <theo@thornhill.no>
Sat, 22 Jul 2023 21:18:23 +0000 (23:18 +0200)
A recent change in tree-sitter-javascript grammar support for
JSX (commit bb1f97b), changed two things:
1. renamed nested_identifier to member_expression
2. removed jsx_fragment, jsx_text is used instead

* lisp/progmodes/js.el (js-jsx--treesit-indent-compatibility-bb1f97b):
Indent helper function to handle different tree-sitter-javascript
version.
* lisp/progmodes/js.el (js--treesit-indent-rules): Use the new
function to handle both jsx_fragment and jsx_text.
* lisp/progmodes/js.el (js-jsx--treesit-font-lock-compatibility-bb1f97b):
Font lock helper function for handle different tree-sitter-javascript
version.
* lisp/progmodes/js.el (js--treesit-font-lock-settings): Use the new
function to handle both nested_identifier and member_expression.

lisp/progmodes/js.el

index a05bd758dbc51461f26e06440c95c8ea6ffc1302..70048e5d26c33f8e20faf8a5c04608faf37769e3 100644 (file)
@@ -3427,6 +3427,18 @@ This function is intended for use in `after-change-functions'."
 
 ;;; Tree sitter integration
 
+(defun js-jsx--treesit-indent-compatibility-bb1f97b ()
+  "Indent rules helper, to handle different releases of tree-sitter-javascript.
+Check if a node type is available, then return the right indent rules."
+  ;; handle commit bb1f97b
+  (condition-case nil
+      (progn (treesit-query-capture 'javascript '((jsx_fragment) @capture))
+             `(((match "<" "jsx_fragment") parent 0)
+               ((parent-is "jsx_fragment") parent js-indent-level)))
+    (error
+     `(((match "<" "jsx_text") parent 0)
+       ((parent-is "jsx_text") parent js-indent-level)))))
+
 (defvar js--treesit-indent-rules
   (let ((switch-case (rx "switch_" (or "case" "default"))))
     `((javascript
@@ -3462,8 +3474,7 @@ This function is intended for use in `after-change-functions'."
        ((parent-is "statement_block") parent-bol js-indent-level)
 
        ;; JSX
-       ((match "<" "jsx_fragment") parent 0)
-       ((parent-is "jsx_fragment") parent js-indent-level)
+       (js-jsx--treesit-indent-compatibility-bb1f97b)
        ((node-is "jsx_closing_element") parent 0)
        ((match "jsx_element" "statement") parent js-indent-level)
        ((parent-is "jsx_element") parent js-indent-level)
@@ -3490,6 +3501,35 @@ This function is intended for use in `after-change-functions'."
     "&&" "||" "!")
   "JavaScript operators for tree-sitter font-locking.")
 
+(defun js-jsx--treesit-font-lock-compatibility-bb1f97b ()
+  "Font lock rules helper, to handle different releases of tree-sitter-javascript.
+Check if a node type is available, then return the right font lock rules."
+  ;; handle commit bb1f97b
+  (condition-case nil
+      (progn (treesit-query-capture 'javascript '((member_expression) @capture))
+            '((jsx_opening_element
+               [(member_expression (identifier)) (identifier)]
+               @font-lock-function-call-face)
+
+              (jsx_closing_element
+               [(member_expression (identifier)) (identifier)]
+               @font-lock-function-call-face)
+
+              (jsx_self_closing_element
+               [(member_expression (identifier)) (identifier)]
+               @font-lock-function-call-face)))
+    (error '((jsx_opening_element
+             [(nested_identifier (identifier)) (identifier)]
+             @font-lock-function-call-face)
+
+            (jsx_closing_element
+             [(nested_identifier (identifier)) (identifier)]
+             @font-lock-function-call-face)
+
+            (jsx_self_closing_element
+             [(nested_identifier (identifier)) (identifier)]
+             @font-lock-function-call-face)))))
+
 (defvar js--treesit-font-lock-settings
   (treesit-font-lock-rules
 
@@ -3599,21 +3639,8 @@ This function is intended for use in `after-change-functions'."
 
    :language 'javascript
    :feature 'jsx
-   '((jsx_opening_element
-      [(nested_identifier (identifier)) (identifier)]
-      @font-lock-function-call-face)
-
-     (jsx_closing_element
-      [(nested_identifier (identifier)) (identifier)]
-      @font-lock-function-call-face)
-
-     (jsx_self_closing_element
-      [(nested_identifier (identifier)) (identifier)]
-      @font-lock-function-call-face)
-
-     (jsx_attribute
-      (property_identifier)
-      @font-lock-constant-face))
+   (append (js-jsx--treesit-font-lock-compatibility-bb1f97b)
+          '((jsx_attribute (property_identifier) @font-lock-constant-face)))
 
    :language 'javascript
    :feature 'number