From 1fb2fb501f34d7c08462209a798e65904ea7b8da Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jostein=20Kj=C3=B8nigsen?= Date: Tue, 5 Sep 2023 21:29:27 +0200 Subject: [PATCH] typescript-ts-mode, tsx-ts-mode: Fix syntax properties for regexp and jsx Propertize regexps as strings and JSX elements as generic strings. * lisp/progmodes/typescript-ts-mode.el (ts-ts--s-p-query) (tsx-ts--s-p-query): New variables. (ts-ts--syntax-propertize, tsx-ts--syntax-propertize) (ts-ts--syntax-propertize-captures): New functions. (typescript-ts-mode, tsx-ts-mode): Use them (bug#65470). --- lisp/progmodes/typescript-ts-mode.el | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/lisp/progmodes/typescript-ts-mode.el b/lisp/progmodes/typescript-ts-mode.el index 96ea18523e3..57382c9cb31 100644 --- a/lisp/progmodes/typescript-ts-mode.el +++ b/lisp/progmodes/typescript-ts-mode.el @@ -418,6 +418,7 @@ Argument LANGUAGE is either `typescript' or `tsx'." (keyword string escape-sequence) (constant expression identifier number pattern property) (function bracket delimiter))) + (setq-local syntax-propertize-function #'ts-ts--syntax-propertize) (treesit-major-mode-setup))) @@ -464,9 +465,47 @@ at least 3 (which is the default value)." (keyword string escape-sequence) (constant expression identifier jsx number pattern property) (function bracket delimiter))) + (setq-local syntax-propertize-function #'tsx-ts--syntax-propertize) (treesit-major-mode-setup))) +(defvar ts-ts--s-p-query + (when (treesit-available-p) + (treesit-query-compile 'typescript + '(((regex pattern: (regex_pattern) @regexp)))))) + +(defvar tsx-ts--s-p-query + (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)))))) + +(defun ts-ts--syntax-propertize (beg end) + (let ((captures (treesit-query-capture 'typescript ts-ts--s-p-query beg end))) + (ts-ts--syntax-propertize-captures captures))) + +(defun tsx-ts--syntax-propertize (beg end) + (let ((captures (treesit-query-capture 'tsx tsx-ts--s-p-query beg end))) + (ts-ts--syntax-propertize-captures captures))) + +(defun ts-ts--syntax-propertize-captures (captures) + (pcase-dolist (`(,name . ,node) captures) + (let* ((ns (treesit-node-start node)) + (ne (treesit-node-end node)) + (syntax (pcase-exhaustive name + ('regexp + (cl-decf ns) + (cl-incf ne) + (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)))) + (if (treesit-ready-p 'tsx) (add-to-list 'auto-mode-alist '("\\.tsx\\'" . tsx-ts-mode))) -- 2.39.2