Skip to content

Commit

Permalink
Enable more colors (#110)
Browse files Browse the repository at this point in the history
* primitive function call highlighting
* arrow function parameters
* better type hint
* generics and decorator
* extend function call regexp for generics

This closes #79.
  • Loading branch information
tam5 authored and josteink committed Sep 18, 2019
1 parent 3214651 commit 1bafd27
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions typescript-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -270,20 +270,43 @@ Match group 1 is MUMBLE.")
"private" "protected" "public" "readonly" "return" "set" "static" "string"
"super" "switch" "this" "throw" "true"
"try" "type" "typeof" "unknown" "var" "void"
"while")) ; yield is handled separately
"while")) ; yield is handled separately
"Regexp matching any typescript keyword.")

(defconst typescript--basic-type-re
(typescript--regexp-opt-symbol
'("any" "bool" "boolean" "bigint" "never" "number" "string" "unknown" "void"))
"Regular expression matching any predefined type in typescript.")

(defconst typescript--access-modifier-re
(typescript--regexp-opt-symbol
'("private" "protected" "public" "readonly" "static" "extends" "implements"))
"Regular expression matching access modifiers.")

(defconst typescript--generic-type-re
"<\\(.*\\)>"
"Regular expression matching generic types.")

(defconst typescript--generic-type-extended-re
(concat "<\\(.*\\)extends\\(.*\\)>")
"Regular expression matching generic types with extension.")

(defconst typescript--decorator-re
(concat "\\(@" typescript--name-re "\\)"))

(defconst typescript--constant-re
(typescript--regexp-opt-symbol '("false" "null" "undefined"
"Infinity" "NaN"
"true" "arguments" "this"))
"Regular expression matching any future reserved words in typescript.")

(defconst typescript--builtin-re
(typescript--regexp-opt-symbol
'("console"))
"Regular expression matching builtins.")

(defconst typescript--function-call-re "\\(\\w+\\)\\(<.+>\\)?\s*("
"Regular expression matching function calls.")

(defconst typescript--font-lock-keywords-1
(list
Expand Down Expand Up @@ -592,6 +615,21 @@ Match group 1 is MUMBLE.")
"Face used to highlight tag values in jsdoc comments."
:group 'typescript)

(defface typescript-access-modifier-face
'((t (:inherit font-lock-keyword-face)))
"Face used to highlight access modifiers."
:group 'typescript)

(defface typescript-this-face
'((t (:inherit font-lock-keyword-face)))
"Face used to highlight 'this' keyword."
:group 'typescript)

(defface typescript-primitive-face
'((t (:inherit font-lock-keyword-face)))
"Face used to highlight builtin types."
:group 'typescript)

;;; User Customization

(defgroup typescript nil
Expand Down Expand Up @@ -1966,10 +2004,40 @@ This performs fontification according to `typescript--class-styles'."
return t
else do (goto-char orig-end)))

(defconst typescript--font-lock-keywords-4
`(
;; highlights that override previous levels
;;

;; special highlight for `this' keyword
("\\(this\\)\\."
(1 'typescript-this-face))

(,typescript--access-modifier-re (1 'typescript-access-modifier-face))
(,typescript--basic-type-re (1 'typescript-primitive-face))

;; highlights that append to previous levels
;;
,@typescript--font-lock-keywords-3

(,typescript--decorator-re (1 font-lock-function-name-face))
(,typescript--function-call-re (1 font-lock-function-name-face))
(,typescript--builtin-re (1 font-lock-type-face))

(,typescript--generic-type-re (1 font-lock-type-face))
(,typescript--generic-type-extended-re (1 font-lock-type-face))

;; arrow function
("\\(=>\\)"
(1 font-lock-keyword-face))
)
"Level four font lock for `typescript-mode'.")

(defconst typescript--font-lock-keywords
'(typescript--font-lock-keywords-3 typescript--font-lock-keywords-1
'(typescript--font-lock-keywords-4 typescript--font-lock-keywords-1
typescript--font-lock-keywords-2
typescript--font-lock-keywords-3)
typescript--font-lock-keywords-3
typescript--font-lock-keywords-4)
"Font lock keywords for `typescript-mode'. See `font-lock-keywords'.")

;;; Propertize
Expand Down

0 comments on commit 1bafd27

Please sign in to comment.