Skip to content

Commit b80ae97

Browse files
authored
Merge pull request #167 from Fuco1/feature/fontify-type-guard
feat(fontlock): fontify the variable and type in typeguard
2 parents f2de3f4 + 333a7c5 commit b80ae97

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

typescript-mode-general-tests.el

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,19 @@ function foo<Z, Y, Z & Y, Z | Y | Z, Y<X<X, Y>>>()\n"
506506
"type Thing = number;"
507507
(should (eq (get-face-at "Thing") 'font-lock-type-face))))
508508

509+
(ert-deftest font-lock/fontify-type-guard ()
510+
"The type guard syntax
511+
512+
var is Type
513+
514+
should be fontified as variable, keyword and type."
515+
(test-with-fontified-buffer
516+
"function test(var: unknown): var is RetType {\n}"
517+
(should (eq (get-face-at 30) 'font-lock-variable-name-face))
518+
(should (eq (get-face-at "is") 'font-lock-keyword-face))
519+
(should (eq (get-face-at "RetType") 'font-lock-type-face))))
520+
521+
509522
(ert-deftest font-lock/type-names-level4 ()
510523
"Typenames should be highlighted in declarations"
511524

typescript-mode.el

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,10 +2013,13 @@ This performs fontification according to `typescript--class-styles'."
20132013
;; - private generic: SomeType<Foo>
20142014
;; - private genericArray: SomeType<Foo>[]
20152015
;; - function testFunc(): SomeType<> {
2016+
;; - function testFunc(a): a is SomeType<> {
20162017
;; TODO: namespaced classes!
20172018
,(list
2018-
(concat ":\\s-\\(" typescript--type-name-re "\\)\\(<" typescript--type-name-re ">\\)?\\(\[\]\\)?\\([,;]\\)?\\s-*{?")
2019-
'(1 'font-lock-type-face))
2019+
(concat ":\\s-\\(?:\\s-*\\(" typescript--name-re "\\)\\s-*\\(is\\)\\s-*\\)?" "\\(" typescript--type-name-re "\\)\\(<" typescript--type-name-re ">\\)?\\(\[\]\\)?\\([,;]\\)?\\s-*{?")
2020+
'(1 'font-lock-variable-name-face nil t)
2021+
'(2 'font-lock-keyword-face nil t)
2022+
'(3 'font-lock-type-face))
20202023

20212024
;; type-casts
20222025
,(list
@@ -2094,7 +2097,7 @@ This performs fontification according to `typescript--class-styles'."
20942097
;; but need care to avoid affecting the // and */ comment markers.
20952098
("\\(?:^\\|[=([{,:;|&!]\\|\\_<return\\_>\\)\\(?:[ \t]\\)*\\(/\\)[^/*]"
20962099
(1 (ignore
2097-
(forward-char -1)
2100+
(forward-char -1)
20982101
(when (or (not (memq (char-after (match-beginning 0)) '(?\s ?\t)))
20992102
;; If the / is at the beginning of line, we have to check
21002103
;; the end of the previous text.
@@ -2330,20 +2333,20 @@ the same column as the current line."
23302333
(save-excursion
23312334
(save-match-data
23322335
(when (looking-at "\\s-*\\_<while\\_>")
2333-
(if (save-excursion
2334-
(skip-chars-backward "[ \t\n]*}")
2335-
(looking-at "[ \t\n]*}"))
2336-
(save-excursion
2337-
(backward-list) (forward-symbol -1) (looking-at "\\_<do\\_>"))
2338-
(typescript--re-search-backward "\\_<do\\_>" (point-at-bol) t)
2339-
(or (looking-at "\\_<do\\_>")
2340-
(let ((saved-indent (current-indentation)))
2341-
(while (and (typescript--re-search-backward "^\\s-*\\_<" nil t)
2342-
(/= (current-indentation) saved-indent)))
2343-
(and (looking-at "\\s-*\\_<do\\_>")
2344-
(not (typescript--re-search-forward
2345-
"\\_<while\\_>" (point-at-eol) t))
2346-
(= (current-indentation) saved-indent)))))))))
2336+
(if (save-excursion
2337+
(skip-chars-backward "[ \t\n]*}")
2338+
(looking-at "[ \t\n]*}"))
2339+
(save-excursion
2340+
(backward-list) (forward-symbol -1) (looking-at "\\_<do\\_>"))
2341+
(typescript--re-search-backward "\\_<do\\_>" (point-at-bol) t)
2342+
(or (looking-at "\\_<do\\_>")
2343+
(let ((saved-indent (current-indentation)))
2344+
(while (and (typescript--re-search-backward "^\\s-*\\_<" nil t)
2345+
(/= (current-indentation) saved-indent)))
2346+
(and (looking-at "\\s-*\\_<do\\_>")
2347+
(not (typescript--re-search-forward
2348+
"\\_<while\\_>" (point-at-eol) t))
2349+
(= (current-indentation) saved-indent)))))))))
23472350

23482351

23492352
(defun typescript--ctrl-statement-indentation ()
@@ -2952,9 +2955,9 @@ Key bindings:
29522955
comment-start-skip "\\(//+\\|/\\*+\\)\\s *")
29532956

29542957
(setq-local electric-indent-chars
2955-
(append "{}():;," electric-indent-chars))
2958+
(append "{}():;," electric-indent-chars))
29562959
(setq-local electric-layout-rules
2957-
'((?\; . after) (?\{ . after) (?\} . before)))
2960+
'((?\; . after) (?\{ . after) (?\} . before)))
29582961

29592962
(let ((c-buffer-is-cc-mode t))
29602963
;; FIXME: These are normally set by `c-basic-common-init'. Should

0 commit comments

Comments
 (0)