From ce83336940f8d5a96e6139a5513057b8d8389ace Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Thu, 12 Dec 2024 21:42:44 -0800 Subject: [PATCH] Apply string syntax only to string in jsx (bug#73978) The current code applies string syntax to too many things. This patch makes tsx-ts-mode (and friends) only apply string syntax to actual text in tsx/jsx. * lisp/progmodes/typescript-ts-mode.el: (tsx-ts--s-p-query): Only capture jsx_text. (tsx-ts--syntax-propertize-captures): handle the case when the text is one character long. (cherry picked from commit 989cdb2c35889476702e4d2bd82d8195fa2e7ec0) --- lisp/progmodes/typescript-ts-mode.el | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index 5e5012ade59..e308b5b4005 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -594,11 +594,7 @@ at least 3 (which is the default value)." (when (treesit-available-p) (treesit-query-compile 'tsx '(((regex pattern: (regex_pattern) @regexp)) - ((variable_declarator value: (jsx_element) @jsx)) - ((assignment_expression right: (jsx_element) @jsx)) - ((arguments (jsx_element) @jsx)) - ((parenthesized_expression (jsx_element) @jsx)) - ((return_statement (jsx_element) @jsx)))))) + ((jsx_text) @jsx))))) (defun typescript-ts--syntax-propertize (beg end) (let ((captures (treesit-query-capture 'typescript typescript-ts--s-p-query beg end))) @@ -619,8 +615,15 @@ at least 3 (which is the default value)." (string-to-syntax "\"/")) ('jsx (string-to-syntax "|"))))) - (put-text-property ns (1+ ns) 'syntax-table syntax) - (put-text-property (1- ne) ne 'syntax-table syntax)))) + ;; The string syntax require at least two characters (one for + ;; opening fence and one for closing fence). So if the string has + ;; only one character, we apply the whitespace syntax. The string + ;; has to be in a non-code syntax, lest the string could contain + ;; parent or brackets and messes up syntax-ppss. + (if (eq ne (1+ ns)) + (put-text-property ns ne 'syntax-table "-") + (put-text-property ns (1+ ns) 'syntax-table syntax) + (put-text-property (1- ne) ne 'syntax-table syntax))))) (if (treesit-ready-p 'tsx) (add-to-list 'auto-mode-alist '("\\.tsx\\'" . tsx-ts-mode))) -- 2.39.2