Skip to content

Commit f3224c1

Browse files
committed
feat(fontlock): fontify ${} expressions inside backticks
1 parent 1cf78d7 commit f3224c1

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
@@ -823,6 +823,31 @@ bbb: Bar,
823823
(should (eq (get-face-at "Foo") 'font-lock-type-face))
824824
(should (eq (get-face-at "Bar") 'font-lock-type-face))))
825825

826+
(ert-deftest font-lock/backticks--expr-fontification--with-variable ()
827+
(test-with-fontified-buffer
828+
"const x = `hello ${world}`"
829+
(should (eq (get-face-at "${") 'font-lock-keyword-face))
830+
(should (eq (get-face-at "world") 'default))
831+
(should (eq (get-face-at "}") 'font-lock-keyword-face))))
832+
833+
(ert-deftest font-lock/backticks--expr-fontification--not-in-regular-string ()
834+
(test-with-fontified-buffer
835+
"const x = 'hello ${world}'"
836+
(should (eq (get-face-at "${") 'font-lock-string-face))
837+
(should (eq (get-face-at "world") 'font-lock-string-face))
838+
(should (eq (get-face-at "}") 'font-lock-string-face))))
839+
840+
(ert-deftest font-lock/backticks--expr-fontification--with-funcall ()
841+
"For now function calls or any other expressions are fontified as
842+
if a simple variable token in its entirety. When/if this is
843+
implemented better, this test should be adjusted to capture the
844+
new functionality."
845+
(test-with-fontified-buffer
846+
"const x = `hello ${parseInt(foobar)}`"
847+
(should (eq (get-face-at "${") 'font-lock-keyword-face))
848+
(should (eq (get-face-at "parseInt(foobar)") 'default))
849+
(should (eq (get-face-at "}") 'font-lock-keyword-face))))
850+
826851
(ert-deftest font-lock/funargs--method--multiline--with-type ()
827852
(test-with-fontified-buffer
828853
"class Foo { foo(

typescript-mode.el

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

2118+
(defun typescript--match-subst-in-quotes (limit)
2119+
"Match dollar substitutions inside backticks."
2120+
(catch 'done
2121+
(while (re-search-forward
2122+
;; `rx' is cool, mkay.
2123+
(rx (or line-start (not (any "\\")))
2124+
(group "${")
2125+
(group (+? nonl))
2126+
(group "}"))
2127+
limit t)
2128+
(let ((string-delim (nth 3 (syntax-ppss))))
2129+
(when (and string-delim (= string-delim 96))
2130+
(throw 'done (point)))))))
2131+
21182132
(defconst typescript--font-lock-keywords-4
21192133
`(
21202134
;; highlights that override previous levels
@@ -2170,7 +2184,12 @@ This performs fontification according to `typescript--class-styles'."
21702184

21712185
;; arrow function
21722186
("\\(=>\\)"
2173-
(1 font-lock-keyword-face)))
2187+
(1 font-lock-keyword-face))
2188+
2189+
(typescript--match-subst-in-quotes
2190+
(1 'font-lock-keyword-face t)
2191+
(2 'default t)
2192+
(3 'font-lock-keyword-face t)))
21742193
"Level four font lock for `typescript-mode'.")
21752194

21762195
(defconst typescript--font-lock-keywords

0 commit comments

Comments
 (0)