From f3224c1f9bfdcc4cec184ece959ec6c49e0afba7 Mon Sep 17 00:00:00 2001 From: Matus Goljer Date: Mon, 8 Aug 2022 14:41:46 +0200 Subject: [PATCH] feat(fontlock): fontify ${} expressions inside backticks --- typescript-mode-general-tests.el | 25 +++++++++++++++++++++++++ typescript-mode.el | 21 ++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/typescript-mode-general-tests.el b/typescript-mode-general-tests.el index 65aa074..eac9ed1 100644 --- a/typescript-mode-general-tests.el +++ b/typescript-mode-general-tests.el @@ -823,6 +823,31 @@ bbb: Bar, (should (eq (get-face-at "Foo") 'font-lock-type-face)) (should (eq (get-face-at "Bar") 'font-lock-type-face)))) +(ert-deftest font-lock/backticks--expr-fontification--with-variable () + (test-with-fontified-buffer + "const x = `hello ${world}`" + (should (eq (get-face-at "${") 'font-lock-keyword-face)) + (should (eq (get-face-at "world") 'default)) + (should (eq (get-face-at "}") 'font-lock-keyword-face)))) + +(ert-deftest font-lock/backticks--expr-fontification--not-in-regular-string () + (test-with-fontified-buffer + "const x = 'hello ${world}'" + (should (eq (get-face-at "${") 'font-lock-string-face)) + (should (eq (get-face-at "world") 'font-lock-string-face)) + (should (eq (get-face-at "}") 'font-lock-string-face)))) + +(ert-deftest font-lock/backticks--expr-fontification--with-funcall () + "For now function calls or any other expressions are fontified as +if a simple variable token in its entirety. When/if this is +implemented better, this test should be adjusted to capture the +new functionality." + (test-with-fontified-buffer + "const x = `hello ${parseInt(foobar)}`" + (should (eq (get-face-at "${") 'font-lock-keyword-face)) + (should (eq (get-face-at "parseInt(foobar)") 'default)) + (should (eq (get-face-at "}") 'font-lock-keyword-face)))) + (ert-deftest font-lock/funargs--method--multiline--with-type () (test-with-fontified-buffer "class Foo { foo( diff --git a/typescript-mode.el b/typescript-mode.el index 8381294..1c07070 100644 --- a/typescript-mode.el +++ b/typescript-mode.el @@ -2115,6 +2115,20 @@ This performs fontification according to `typescript--class-styles'." return t else do (goto-char orig-end))) +(defun typescript--match-subst-in-quotes (limit) + "Match dollar substitutions inside backticks." + (catch 'done + (while (re-search-forward + ;; `rx' is cool, mkay. + (rx (or line-start (not (any "\\"))) + (group "${") + (group (+? nonl)) + (group "}")) + limit t) + (let ((string-delim (nth 3 (syntax-ppss)))) + (when (and string-delim (= string-delim 96)) + (throw 'done (point))))))) + (defconst typescript--font-lock-keywords-4 `( ;; highlights that override previous levels @@ -2170,7 +2184,12 @@ This performs fontification according to `typescript--class-styles'." ;; arrow function ("\\(=>\\)" - (1 font-lock-keyword-face))) + (1 font-lock-keyword-face)) + + (typescript--match-subst-in-quotes + (1 'font-lock-keyword-face t) + (2 'default t) + (3 'font-lock-keyword-face t))) "Level four font lock for `typescript-mode'.") (defconst typescript--font-lock-keywords