Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(fontlock): fontify ${} expressions inside backticks #177

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions typescript-mode-general-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
21 changes: 20 additions & 1 deletion typescript-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading