From 3f7598806eb923c907dd2d30e8ef1dea12e672ac Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Fri, 1 Sep 2023 04:41:15 +0300 Subject: [PATCH] Add syntax-propertize-function to js-ts-mode * lisp/progmodes/js.el (js-ts--s-p-query): New variable (bug#65470). (js-ts--syntax-propertize): New function. (js-ts-mode): Use it. --- lisp/progmodes/js.el | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 9d2990e7bc9..9b6da4ec2fc 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -3829,6 +3829,7 @@ Currently there are `js-mode' and `js-ts-mode'." (append "{}():;,<>/" electric-indent-chars)) ;FIXME: js2-mode adds "[]*". (setq-local electric-layout-rules '((?\; . after) (?\{ . after) (?\} . before))) + (setq-local syntax-propertize-function #'js-ts--syntax-propertize) ;; Tree-sitter setup. (treesit-parser-create 'javascript) @@ -3864,6 +3865,29 @@ Currently there are `js-mode' and `js-ts-mode'." (add-to-list 'auto-mode-alist '("\\(\\.js[mx]\\|\\.har\\)\\'" . js-ts-mode)))) +(defvar js-ts--s-p-query + (when (treesit-available-p) + (treesit-query-compile 'javascript + '(((regex pattern: (regex_pattern) @regexp)) + ((variable_declarator value: (jsx_element) @jsx)) + ((assignment_expression right: (jsx_element) @jsx)) + ((return_statement (jsx_element) @jsx)))))) + +(defun js-ts--syntax-propertize (beg end) + (let ((captures (treesit-query-capture 'javascript js-ts--s-p-query beg end))) + (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))))) + ;;;###autoload (define-derived-mode js-json-mode js-mode "JSON" (setq-local js-enabled-frameworks nil) -- 2.39.2