branch: elpa/typescript-mode
commit 1767de109515a992b965d275cb5550d336d957df
Merge: bc37bc4b15 e38492f1cd
Author: Jostein Kjønigsen <[email protected]>
Commit: GitHub <[email protected]>
Merge pull request #129 from emacs-typescript/feature/types-in-declarations
Add syntax-highlighting for types in variable-declarations
---
typescript-mode-general-tests.el | 74 ++++++++++++++++++++++++++++++++++++++++
typescript-mode.el | 22 ++++++++++++
2 files changed, 96 insertions(+)
diff --git a/typescript-mode-general-tests.el b/typescript-mode-general-tests.el
index 368a7d5719..6e3f098aa7 100644
--- a/typescript-mode-general-tests.el
+++ b/typescript-mode-general-tests.el
@@ -464,6 +464,80 @@ function foo<Z, Y, Z & Y, Z | Y | Z, Y<X<X, Y>>>()\n"
"type Thing = number;"
(should (eq (get-face-at "Thing") 'font-lock-type-face))))
+(ert-deftest font-lock/type-names-level4 ()
+ "Typenames should be highlighted in declarations"
+
+ (test-with-fontified-buffer
+ "function test(var1: Type1, var2: Type2): RetType {\n}"
+ (should (not (eq (get-face-at "var1") 'font-lock-type-face)))
+ (should (eq (get-face-at "Type1") 'font-lock-type-face))
+ (should (not (eq (get-face-at "var2") 'font-lock-type-face)))
+ (should (eq (get-face-at "Type2") 'font-lock-type-face))
+ (should (eq (get-face-at "RetType") 'font-lock-type-face)))
+
+ (test-with-fontified-buffer
+ "class foo { test(var1: Type1, var2: Type2): RetType {\n} }"
+ (should (not (eq (get-face-at "var1") 'font-lock-type-face)))
+ (should (eq (get-face-at "Type1") 'font-lock-type-face))
+ (should (not (eq (get-face-at "var2") 'font-lock-type-face)))
+ (should (eq (get-face-at "Type2") 'font-lock-type-face))
+ (should (eq (get-face-at "RetType") 'font-lock-type-face)))
+
+ (test-with-fontified-buffer
+ "let a: SomeType;"
+ (should (eq (get-face-at "SomeType") 'font-lock-type-face)))
+ (test-with-fontified-buffer
+ "private b: SomeType;"
+ (should (eq (get-face-at "SomeType") 'font-lock-type-face)))
+ (test-with-fontified-buffer
+ "private someArray: SomeType[];"
+ (should (eq (get-face-at "SomeType") 'font-lock-type-face)))
+ (test-with-fontified-buffer
+ "private generic: SomeType<Foo>;"
+ (should (eq (get-face-at "SomeType") 'font-lock-type-face))
+ (should (eq (get-face-at "Foo") 'font-lock-type-face)))
+
+ (test-with-fontified-buffer
+ "private genericArray: SomeType<Foo>[];"
+ (should (eq (get-face-at "SomeType") 'font-lock-type-face))
+ (should (eq (get-face-at "Foo") 'font-lock-type-face)))
+
+ (test-with-fontified-buffer
+ "private genericArray2: SomeType<Foo[]>;"
+ (should (eq (get-face-at "SomeType") 'font-lock-type-face))
+ (should (eq (get-face-at "Foo") 'font-lock-type-face)))
+
+ (test-with-fontified-buffer
+ "private genericArray3: SomeType<Foo[]>[];"
+ (should (eq (get-face-at "SomeType") 'font-lock-type-face))
+ (should (eq (get-face-at "Foo") 'font-lock-type-face))))
+
+(ert-deftest font-lock/type-names-level4-namespaces ()
+ "Namespaced Typenames should be highlighted in declarations"
+ (test-with-fontified-buffer
+ "private b: Namespaced.ClassName;"
+ (should (eq (get-face-at "Namespaced") 'font-lock-type-face))
+ (should (eq (get-face-at "ClassName") 'font-lock-type-face)))
+ (test-with-fontified-buffer
+ "function test(var1: Namespaced.ClassName): RetType {\n}"
+ (should (eq (get-face-at "Namespaced") 'font-lock-type-face))
+ (should (eq (get-face-at "ClassName") 'font-lock-type-face)))
+
+ (test-with-fontified-buffer
+ "class Foo { test(var1: Namespaced.ClassName): RetType {\n}"
+ (should (eq (get-face-at "Namespaced") 'font-lock-type-face))
+ (should (eq (get-face-at "ClassName") 'font-lock-type-face)))
+
+ (test-with-fontified-buffer
+ "function test(var1: Type): Namespaced.ClassName {\n}"
+ (should (eq (get-face-at "Namespaced") 'font-lock-type-face))
+ (should (eq (get-face-at "ClassName") 'font-lock-type-face)))
+
+ (test-with-fontified-buffer
+ "class Foo { test(var1: Type): Namespaced.ClassName {\n}"
+ (should (eq (get-face-at "Namespaced") 'font-lock-type-face))
+ (should (eq (get-face-at "ClassName") 'font-lock-type-face))))
+
(defun flyspell-predicate-test (search-for)
"This function runs a test on
`typescript--flyspell-mode-predicate'. `SEARCH-FOR' is a string
diff --git a/typescript-mode.el b/typescript-mode.el
index e0d4f841af..82ae817610 100644
--- a/typescript-mode.el
+++ b/typescript-mode.el
@@ -63,6 +63,9 @@
;;; Constants
+(defconst typescript--type-name-re
"\\(?:[A-Z][A-Za-z0-9]+\\.\\)\\{0,\\}\\(?:[A-Z][A-Za-z0-9]+\\)"
+ "Regexp matching a conventional TypeScript type-name. Must start with
upper-case letter!")
+
(defconst typescript--name-start-re "[a-zA-Z_$]"
"Regexp matching the start of a typescript identifier, without grouping.")
@@ -1997,6 +2000,25 @@ This performs fontification according to
`typescript--class-styles'."
'(end-of-line)
'(1 font-lock-type-face)))
+ ;; type-highlighting in variable/parameter declarations
+ ;; supports a small variety of common declarations:
+ ;; - let a: SomeType;
+ ;; - private b: SomeType;
+ ;; - private someFunc(var: SomeType) {
+ ;; - private array: SomeType[]
+ ;; - private generic: SomeType<Foo>
+ ;; - private genericArray: SomeType<Foo>[]
+ ;; - function testFunc(): SomeType<> {
+ ;; TODO: namespaced classes!
+ ,(list
+ (concat ":\\s-\\(" typescript--type-name-re "\\)\\(<"
typescript--type-name-re ">\\)?\\(\[\]\\)?\\([,;]\\)?\\s-*{?")
+ '(1 'font-lock-type-face))
+
+ ;; type-casts
+ ,(list
+ (concat "<\\(" typescript--type-name-re "\\)>")
+ '(1 'font-lock-type-face))
+
;; highlights that append to previous levels
;;
,@typescript--font-lock-keywords-3