Skip to content

Commit 7a4a15f

Browse files
committed
feat(fontlock): fontify ${} expressions inside backticks
1 parent d1123e0 commit 7a4a15f

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

typescript-mode-general-tests.el

+25
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,31 @@ bbb: Bar,
846846
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
847847
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))))
848848

849+
(ert-deftest font-lock/backticks--expr-fontification--with-variable ()
850+
(test-with-fontified-buffer
851+
"const x = `hello ${world}`"
852+
(should (eq (get-face-at "${") 'font-lock-keyword-face))
853+
(should (eq (get-face-at "world") 'default))
854+
(should (eq (get-face-at "}") 'font-lock-keyword-face))))
855+
856+
(ert-deftest font-lock/backticks--expr-fontification--not-in-regular-string ()
857+
(test-with-fontified-buffer
858+
"const x = 'hello ${world}'"
859+
(should (eq (get-face-at "${") 'font-lock-string-face))
860+
(should (eq (get-face-at "world") 'font-lock-string-face))
861+
(should (eq (get-face-at "}") 'font-lock-string-face))))
862+
863+
(ert-deftest font-lock/backticks--expr-fontification--with-funcall ()
864+
"For now function calls or any other expressions are fontified as
865+
if a simple variable token in its entirety. When/if this is
866+
implemented better, this test should be adjusted to capture the
867+
new functionality."
868+
(test-with-fontified-buffer
869+
"const x = `hello ${parseInt(foobar)}`"
870+
(should (eq (get-face-at "${") 'font-lock-keyword-face))
871+
(should (eq (get-face-at "parseInt(foobar)") 'default))
872+
(should (eq (get-face-at "}") 'font-lock-keyword-face))))
873+
849874
(defun flyspell-predicate-test (search-for)
850875
"This function runs a test on
851876
`typescript--flyspell-mode-predicate'. `SEARCH-FOR' is a string

typescript-mode.el

+20-1
Original file line numberDiff line numberDiff line change
@@ -2089,6 +2089,20 @@ This performs fontification according to `typescript--class-styles'."
20892089
return t
20902090
else do (goto-char orig-end)))
20912091

2092+
(defun typescript--match-subst-in-quotes (limit)
2093+
"Match dollar substitutions inside backticks."
2094+
(catch 'done
2095+
(while (re-search-forward
2096+
;; `rx' is cool, mkay.
2097+
(rx (or line-start (not (any "\\")))
2098+
(group "${")
2099+
(group (+? nonl))
2100+
(group "}"))
2101+
limit t)
2102+
(let ((string-delim (nth 3 (syntax-ppss))))
2103+
(when (and string-delim (= string-delim 96))
2104+
(throw 'done (point)))))))
2105+
20922106
(defconst typescript--font-lock-keywords-4
20932107
`(
20942108
;; highlights that override previous levels
@@ -2144,7 +2158,12 @@ This performs fontification according to `typescript--class-styles'."
21442158

21452159
;; arrow function
21462160
("\\(=>\\)"
2147-
(1 font-lock-keyword-face)))
2161+
(1 font-lock-keyword-face))
2162+
2163+
(typescript--match-subst-in-quotes
2164+
(1 'font-lock-keyword-face t)
2165+
(2 'default t)
2166+
(3 'font-lock-keyword-face t)))
21482167
"Level four font lock for `typescript-mode'.")
21492168

21502169
(defconst typescript--font-lock-keywords

0 commit comments

Comments
 (0)