aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincenzo Pupillo2023-07-22 13:37:54 +0200
committerTheodor Thornhill2023-07-22 23:18:23 +0200
commit235561a2ccc03f50652cd942ab7906fe6178ef83 (patch)
tree08a66dc82ae27ee8792835415de5296620c3c105
parent12ab82d3b358fa72dc4705cb6ff7ce0523968200 (diff)
downloademacs-235561a2ccc03f50652cd942ab7906fe6178ef83.tar.gz
emacs-235561a2ccc03f50652cd942ab7906fe6178ef83.zip
Update TSX support due to upstream changes (bug#64647)
A recent change in tree-sitter-typescript grammar support for TSX (commit b893426), changed two things: 1. renamed nested_identifier to member_expression 2. removed jsx_fragment, jsx_text is used instead * lisp/progmodes/typescript-ts-mode.el (tsx-ts-mode--indent-compatibility-b893426): Indent helper function to handle different tree-sitter-typescript version. * lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode--indent-rules): use the new function to handle both jsx_fragment and jsx_text. * lisp/progmodes/typescript-ts-mode.el (tsx-ts-mode--font-lock-compatibility-bb1f97b): Font lock helper function for handle different tree-sitter-typescript version. * lisp/progmodes/typescript-ts-mode.el (typescript-ts-mode--font-lock-settings): Use the new function to handle both nested_identifier and member_expression.
-rw-r--r--lisp/progmodes/typescript-ts-mode.el78
1 files changed, 55 insertions, 23 deletions
diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el
index 5df34de0472..173ec52f209 100644
--- a/lisp/progmodes/typescript-ts-mode.el
+++ b/lisp/progmodes/typescript-ts-mode.el
@@ -75,6 +75,18 @@
75 table) 75 table)
76 "Syntax table for `typescript-ts-mode'.") 76 "Syntax table for `typescript-ts-mode'.")
77 77
78(defun tsx-ts-mode--indent-compatibility-b893426 ()
79 "Indent rules helper, to handle different releases of tree-sitter-tsx.
80Check if a node type is available, then return the right indent rules."
81 ;; handle commit b893426
82 (condition-case nil
83 (progn (treesit-query-capture 'tsx '((jsx_fragment) @capture))
84 `(((match "<" "jsx_fragment") parent 0)
85 ((parent-is "jsx_fragment") parent typescript-ts-mode-indent-offset)))
86 (error
87 `(((match "<" "jsx_text") parent 0)
88 ((parent-is "jsx_text") parent typescript-ts-mode-indent-offset)))))
89
78(defun typescript-ts-mode--indent-rules (language) 90(defun typescript-ts-mode--indent-rules (language)
79 "Rules used for indentation. 91 "Rules used for indentation.
80Argument LANGUAGE is either `typescript' or `tsx'." 92Argument LANGUAGE is either `typescript' or `tsx'."
@@ -110,16 +122,15 @@ Argument LANGUAGE is either `typescript' or `tsx'."
110 ((parent-is "binary_expression") parent-bol typescript-ts-mode-indent-offset) 122 ((parent-is "binary_expression") parent-bol typescript-ts-mode-indent-offset)
111 123
112 ,@(when (eq language 'tsx) 124 ,@(when (eq language 'tsx)
113 `(((match "<" "jsx_fragment") parent 0) 125 (append (tsx-ts-mode--indent-compatibility-b893426)
114 ((parent-is "jsx_fragment") parent typescript-ts-mode-indent-offset) 126 `(((node-is "jsx_closing_element") parent 0)
115 ((node-is "jsx_closing_element") parent 0) 127 ((match "jsx_element" "statement") parent typescript-ts-mode-indent-offset)
116 ((match "jsx_element" "statement") parent typescript-ts-mode-indent-offset) 128 ((parent-is "jsx_element") parent typescript-ts-mode-indent-offset)
117 ((parent-is "jsx_element") parent typescript-ts-mode-indent-offset) 129 ((parent-is "jsx_text") parent-bol typescript-ts-mode-indent-offset)
118 ((parent-is "jsx_text") parent-bol typescript-ts-mode-indent-offset) 130 ((parent-is "jsx_opening_element") parent typescript-ts-mode-indent-offset)
119 ((parent-is "jsx_opening_element") parent typescript-ts-mode-indent-offset) 131 ((parent-is "jsx_expression") parent-bol typescript-ts-mode-indent-offset)
120 ((parent-is "jsx_expression") parent-bol typescript-ts-mode-indent-offset) 132 ((match "/" "jsx_self_closing_element") parent 0)
121 ((match "/" "jsx_self_closing_element") parent 0) 133 ((parent-is "jsx_self_closing_element") parent typescript-ts-mode-indent-offset))))
122 ((parent-is "jsx_self_closing_element") parent typescript-ts-mode-indent-offset)))
123 ;; FIXME(Theo): This no-node catch-all should be removed. When is it needed? 134 ;; FIXME(Theo): This no-node catch-all should be removed. When is it needed?
124 (no-node parent-bol 0)))) 135 (no-node parent-bol 0))))
125 136
@@ -142,6 +153,38 @@ Argument LANGUAGE is either `typescript' or `tsx'."
142 "&&" "||" "!" "?.") 153 "&&" "||" "!" "?.")
143 "TypeScript operators for tree-sitter font-locking.") 154 "TypeScript operators for tree-sitter font-locking.")
144 155
156(defun tsx-ts-mode--font-lock-compatibility-bb1f97b ()
157 "Font lock rules helper, to handle different releases of tree-sitter-tsx.
158Check if a node type is available, then return the right font lock rules."
159 ;; handle commit bb1f97b
160 ;; Warning: treesitter-query-capture says both node types are valid,
161 ;; but then raises an error if the wrong node type is used. So it is
162 ;; important to check with the new node type (member_expression)
163 (condition-case nil
164 (progn (treesit-query-capture 'tsx '((member_expression) @capture))
165 '((jsx_opening_element
166 [(member_expression (identifier)) (identifier)]
167 @typescript-ts-jsx-tag-face)
168
169 (jsx_closing_element
170 [(member_expression (identifier)) (identifier)]
171 @typescript-ts-jsx-tag-face)
172
173 (jsx_self_closing_element
174 [(member_expression (identifier)) (identifier)]
175 @typescript-ts-jsx-tag-face)))
176 (error '((jsx_opening_element
177 [(nested_identifier (identifier)) (identifier)]
178 @typescript-ts-jsx-tag-face)
179
180 (jsx_closing_element
181 [(nested_identifier (identifier)) (identifier)]
182 @typescript-ts-jsx-tag-face)
183
184 (jsx_self_closing_element
185 [(nested_identifier (identifier)) (identifier)]
186 @typescript-ts-jsx-tag-face)))))
187
145(defun typescript-ts-mode--font-lock-settings (language) 188(defun typescript-ts-mode--font-lock-settings (language)
146 "Tree-sitter font-lock settings. 189 "Tree-sitter font-lock settings.
147Argument LANGUAGE is either `typescript' or `tsx'." 190Argument LANGUAGE is either `typescript' or `tsx'."
@@ -293,19 +336,8 @@ Argument LANGUAGE is either `typescript' or `tsx'."
293 336
294 :language language 337 :language language
295 :feature 'jsx 338 :feature 'jsx
296 `((jsx_opening_element 339 (append (tsx-ts-mode--font-lock-compatibility-bb1f97b)
297 [(nested_identifier (identifier)) (identifier)] 340 `((jsx_attribute (property_identifier) @typescript-ts-jsx-attribute-face)))
298 @typescript-ts-jsx-tag-face)
299
300 (jsx_closing_element
301 [(nested_identifier (identifier)) (identifier)]
302 @typescript-ts-jsx-tag-face)
303
304 (jsx_self_closing_element
305 [(nested_identifier (identifier)) (identifier)]
306 @typescript-ts-jsx-tag-face)
307
308 (jsx_attribute (property_identifier) @typescript-ts-jsx-attribute-face))
309 341
310 :language language 342 :language language
311 :feature 'number 343 :feature 'number