From 170924e945f4cceda12b5e1681391c3b828a3dec Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Mon, 17 Oct 2022 01:47:01 -0700 Subject: [PATCH] Fix js/ts tree-sitter template_string font-lock * lisp/progmodes/js.el (js--treesit-settings): Fontify template_strings with js--fontify-template-string. (js--fontify-template-string): New function. (js--json-treesit-settings): Add missing :feature flag. * lisp/progmodes/ts-mode.el (ts-mode--settings): Fontify template_strings with js--fontify-template-string. --- lisp/progmodes/js.el | 25 +++++++++++++++++++++---- lisp/progmodes/ts-mode.el | 7 ++----- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lisp/progmodes/js.el b/lisp/progmodes/js.el index 18499a466af..667416852ef 100644 --- a/lisp/progmodes/js.el +++ b/lisp/progmodes/js.el @@ -3461,10 +3461,7 @@ indentation, which-function and movement functions." :language 'javascript :feature 'basic :override t - `(;; Everything overrides template string. - (template_string) @font-lock-string-face - - ((identifier) @font-lock-constant-face + `(((identifier) @font-lock-constant-face (:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face)) (new_expression @@ -3561,8 +3558,27 @@ indentation, which-function and movement functions." (comment) @font-lock-comment-face [,@js--treesit-keywords] @font-lock-keyword-face + (template_string) @js--fontify-template-string (template_substitution ["${" "}"] @font-lock-constant-face)))) +(defun js--fontify-template-string (beg end node) + "Fontify template string but not substitution inside it. +BEG, END, NODE refers to the template_string node." + (ignore end) + ;; You would have thought that the children of the string node spans + ;; the whole string. No, the children of the template_string only + ;; includes the starting "`", any template_substitution, and the + ;; closing "`". That's why we have to track BEG instead of just + ;; fontifying each child. + (let ((child (treesit-node-child node 0))) + (while child + (if (equal (treesit-node-type child) "template_substitution") + (put-text-property beg (treesit-node-start child) + 'face 'font-lock-string-face) + (put-text-property beg (treesit-node-end child) + 'face 'font-lock-string-face)) + (setq beg (treesit-node-end child) + child (treesit-node-next-sibling child))))) (defun js-treesit-current-defun () "Return name of surrounding function. @@ -3748,6 +3764,7 @@ indentation." (defvar js--json-treesit-settings (treesit-font-lock-rules :language 'json + :feature 'basic :override t `( (pair diff --git a/lisp/progmodes/ts-mode.el b/lisp/progmodes/ts-mode.el index 10d4b7bd18b..6e4aeebde87 100644 --- a/lisp/progmodes/ts-mode.el +++ b/lisp/progmodes/ts-mode.el @@ -109,10 +109,7 @@ :language 'tsx :override t :feature 'basic - '( - (template_string) @font-lock-string-face - - ((identifier) @font-lock-constant-face + '(((identifier) @font-lock-constant-face (:match "^[A-Z_][A-Z_\\d]*$" @font-lock-constant-face)) (nested_type_identifier @@ -223,8 +220,8 @@ (number) @font-lock-constant-face (string) @font-lock-string-face - (template_string) @font-lock-string-face + (template_string) @js--fontify-template-string (template_substitution ["${" "}"] @font-lock-constant-face) -- 2.39.2