1
- ; ;; typescript-mode.el --- Major mode for editing typescript
1
+ ; ;; typescript-mode.el --- Major mode for editing typescript -*- lexical-binding : t -*-
2
2
3
3
; ; -----------------------------------------------------------------------------------
4
4
; ; TypeScript support for Emacs
5
5
; ; Unmodified original sourve available at http://www.karllandstrom.se/downloads/emacs/javascript.el
6
- ; ; Copyright (c) 2008 Free Software Foundation
6
+ ; ; Copyright (c) 2008-2025 Free Software Foundation
7
7
; ; Portions Copyright (C) Microsoft Open Technologies, Inc. All rights reserved.
8
8
; ;
9
9
; ; This program is free software: you can redistribute it and/or modify
@@ -312,13 +312,13 @@ Match group 1 is MUMBLE.")
312
312
313
313
(defconst typescript--font-lock-keywords-2
314
314
(append typescript--font-lock-keywords-1
315
- ( list ( cons typescript--constant-re font-lock-constant-face )
316
- ( cons typescript--basic-type-re font-lock-type-face )
317
- ( list typescript--keyword-re 1 font-lock-keyword-face )
318
- ( list " \\ _<for\\ _>"
319
- " \\ s-+\\ (each\\ )\\ _>" nil nil
320
- ( list 1 'font-lock-keyword-face ))
321
- ( cons " \\ _<yield\\ (\\ *\\ |\\ _>\\ )" 'font-lock-keyword-face )))
315
+ `(( , typescript--constant-re ( 0 ' font-lock-constant-face) )
316
+ ( , typescript--basic-type-re ( 0 ' font-lock-type-face) )
317
+ ( , typescript--keyword-re ( 1 ' font-lock-keyword-face) )
318
+ ( " \\ _<for\\ _>"
319
+ ( " \\ s-+\\ (each\\ )\\ _>" nil nil
320
+ ( 1 'font-lock-keyword-face ) ))
321
+ ( " \\ _<yield\\ (\\ *\\ |\\ _>\\ )" ( 0 'font-lock-keyword-face ) )))
322
322
" Level two font lock keywords for `typescript-mode' ." )
323
323
324
324
; ; typescript--pitem is the basic building block of the lexical
@@ -935,15 +935,16 @@ point at BOB."
935
935
This function invokes `re-search-forward' , but treats the buffer
936
936
as if strings and comments have been removed."
937
937
(let ((saved-point (point ))
938
- (search-expr
938
+ (search-fun
939
939
(cond ((null count)
940
- '( typescript--re-search-forward-inner regexp bound 1 ))
940
+ ( lambda () ( typescript--re-search-forward-inner regexp bound 1 ) ))
941
941
((< count 0 )
942
- '( typescript--re-search-backward-inner regexp bound (- count)))
942
+ ( lambda () ( typescript--re-search-backward-inner regexp bound (- count) )))
943
943
((> count 0 )
944
- '(typescript--re-search-forward-inner regexp bound count)))))
944
+ (lambda () (typescript--re-search-forward-inner regexp bound count)))
945
+ (t #'ignore ))))
945
946
(condition-case err
946
- (eval search-expr )
947
+ (funcall search-fun )
947
948
(search-failed
948
949
(goto-char saved-point)
949
950
(unless noerror
@@ -990,15 +991,16 @@ If the point is in the last line, searching back for \"\\n\" will
990
991
skip over the line with \" let b\" . The newline found will be the
991
992
one at the end of the line with \" let a\" ."
992
993
(let ((saved-point (point ))
993
- (search-expr
994
+ (search-fun
994
995
(cond ((null count)
995
- `( typescript--re-search-backward-inner , regexp , bound 1 ))
996
+ ( lambda () ( typescript--re-search-backward-inner regexp bound 1 ) ))
996
997
((< count 0 )
997
- `( typescript--re-search-forward-inner , regexp , bound (- , count )))
998
+ ( lambda () ( typescript--re-search-forward-inner regexp bound (- count) )))
998
999
((> count 0 )
999
- `(typescript--re-search-backward-inner , regexp , bound , count )))))
1000
+ (lambda () (typescript--re-search-backward-inner regexp bound count)))
1001
+ (t #'ignore ))))
1000
1002
(condition-case err
1001
- (eval search-expr )
1003
+ (funcall search-fun )
1002
1004
(search-failed
1003
1005
(goto-char saved-point)
1004
1006
(unless noerror
@@ -1839,35 +1841,35 @@ and searches for the next token to be highlighted."
1839
1841
(0 'typescript-jsdoc-value t ))
1840
1842
1841
1843
(typescript--tslint-flag-matcher
1842
- (1 font-lock-preprocessor-face t ))
1844
+ (1 ' font-lock-preprocessor-face t ))
1843
1845
1844
1846
(" \\ .\\ (prototype\\ )\\ _>"
1845
- (1 font-lock-constant-face ))
1847
+ (1 ' font-lock-constant-face ))
1846
1848
1847
1849
(,(rx symbol-start " class" (+ space) (group (+ (or (syntax word) (syntax symbol)))))
1848
- (1 font-lock-type-face ))
1850
+ (1 ' font-lock-type-face ))
1849
1851
1850
1852
(,(rx symbol-start " extends" (+ space) (group (+ (or (syntax word) (syntax symbol)))))
1851
- (1 font-lock-type-face ))
1853
+ (1 ' font-lock-type-face ))
1852
1854
1853
1855
(,(rx symbol-start " implements" (+ space))
1854
- (,(rx symbol-start (+ (syntax word))) nil nil (0 font-lock-type-face )))
1856
+ (,(rx symbol-start (+ (syntax word))) nil nil (0 ' font-lock-type-face )))
1855
1857
1856
1858
(,(rx symbol-start " interface" (+ space) (group (+ (or (syntax word) (syntax symbol)))))
1857
- (1 font-lock-type-face ))
1859
+ (1 ' font-lock-type-face ))
1858
1860
1859
1861
(,(rx symbol-start " type" (+ space) (group (+ (or (syntax word) (syntax symbol)))))
1860
- (1 font-lock-type-face ))
1862
+ (1 ' font-lock-type-face ))
1861
1863
1862
1864
(,(rx symbol-start " enum" (+ space) (group (+ (or (syntax word) (syntax symbol)))))
1863
- (1 font-lock-type-face ))
1865
+ (1 ' font-lock-type-face ))
1864
1866
1865
1867
; ; Highlights class being declared, in parts
1866
1868
(typescript--class-decl-matcher
1867
1869
,(concat " \\ (" typescript--name-re " \\ )\\ (?:\\ .\\ |.*$\\ )" )
1868
1870
(goto-char (match-beginning 1 ))
1869
1871
nil
1870
- (1 font-lock-type-face ))
1872
+ (1 ' font-lock-type-face ))
1871
1873
1872
1874
; ; Highlights parent class, in parts, if available
1873
1875
(typescript--class-decl-matcher
@@ -1884,20 +1886,20 @@ and searches for the next token to be highlighted."
1884
1886
(save-excursion
1885
1887
(goto-char typescript--tmp-location)
1886
1888
(delete-char 1 )))
1887
- (1 font-lock-type-face ))
1889
+ (1 ' font-lock-type-face ))
1888
1890
1889
1891
; ; Highlights parent class
1890
1892
(typescript--class-decl-matcher
1891
- (2 font-lock-type-face nil t ))
1893
+ (2 ' font-lock-type-face nil t ))
1892
1894
1893
1895
; ; Dojo needs its own matcher to override the string highlighting
1894
1896
(,(typescript--make-framework-matcher
1895
1897
'dojo
1896
1898
" ^\\ s-*dojo\\ .declare\\ s-*(\" "
1897
1899
" \\ (" typescript--dotted-name-re " \\ )"
1898
1900
" \\ (?:\" \\ s-*,\\ s-*\\ (" typescript--dotted-name-re " \\ )\\ )?" )
1899
- (1 font-lock-type-face t )
1900
- (2 font-lock-type-face nil t ))
1901
+ (1 ' font-lock-type-face t )
1902
+ (2 ' font-lock-type-face nil t ))
1901
1903
1902
1904
; ; Match Dojo base classes. Of course Mojo has to be different
1903
1905
; ; from everything else under the sun...
@@ -1909,7 +1911,7 @@ and searches for the next token to be highlighted."
1909
1911
" \\ (?:\\ ].*$\\ )?" )
1910
1912
(backward-char )
1911
1913
(end-of-line )
1912
- (1 font-lock-type-face ))
1914
+ (1 ' font-lock-type-face ))
1913
1915
1914
1916
; ; continued Dojo base-class list
1915
1917
(,(typescript--make-framework-matcher
@@ -1922,22 +1924,20 @@ and searches for the next token to be highlighted."
1922
1924
(forward-symbol -1 )
1923
1925
(end-of-line ))
1924
1926
(end-of-line )
1925
- (1 font-lock-type-face ))
1927
+ (1 ' font-lock-type-face ))
1926
1928
1927
1929
; ; variable declarations
1928
1930
,(list
1929
1931
(concat " \\ _<\\ (const\\ |var\\ |let\\ )\\ _>\\ |" typescript--basic-type-re)
1930
1932
(list #'typescript--variable-decl-matcher nil nil nil ))
1931
1933
1932
1934
; ; class instantiation
1933
- ,(list
1934
- (concat " \\ _<new\\ _>\\ s-+\\ (" typescript--dotted-name-re " \\ )" )
1935
- (list 1 'font-lock-type-face ))
1935
+ (,(concat " \\ _<new\\ _>\\ s-+\\ (" typescript--dotted-name-re " \\ )" )
1936
+ (1 'font-lock-type-face ))
1936
1937
1937
1938
; ; instanceof
1938
- ,(list
1939
- (concat " \\ _<instanceof\\ _>\\ s-+\\ (" typescript--dotted-name-re " \\ )" )
1940
- (list 1 'font-lock-type-face ))
1939
+ (,(concat " \\ _<instanceof\\ _>\\ s-+\\ (" typescript--dotted-name-re " \\ )" )
1940
+ (1 'font-lock-type-face ))
1941
1941
1942
1942
; ; formal parameters in "function" function call
1943
1943
; ; function helloWorld(a: number, b: Promise<number>): void { }
@@ -2162,7 +2162,7 @@ This performs fontification according to `typescript--class-styles'."
2162
2162
; ; - () => SomeType
2163
2163
; ; TODO: namespaced classes!
2164
2164
,(list
2165
- (concat " \\ (?::\\ |=>\\ )\\ s-\\ (?:\\ s-*\\ (" typescript--name-re " \\ )\\ s-*\\ (is\\ )\\ s-*\\ )?" " \\ (" typescript--type-name-re " \\ )\\ (<" typescript--type-name-re " >\\ )?\\ (\[ \] \\ )?\\ ([,;]\\ )?\\ s-*{?" )
2165
+ (concat " \\ (?::\\ |=>\\ )\\ s-\\ (?:\\ s-*\\ (" typescript--name-re " \\ )\\ s-*\\ (is\\ )\\ s-*\\ )?" " \\ (" typescript--type-name-re " \\ )\\ (<" typescript--type-name-re " >\\ )?\\ (\\ [ \ \ ]\\ )?\\ ([,;]\\ )?\\ s-*{?" )
2166
2166
'(1 'font-lock-variable-name-face nil t )
2167
2167
'(2 'font-lock-keyword-face nil t )
2168
2168
'(3 'font-lock-type-face ))
@@ -2176,22 +2176,38 @@ This performs fontification according to `typescript--class-styles'."
2176
2176
; ;
2177
2177
,@typescript--font-lock-keywords-3
2178
2178
2179
- (, typescript--decorator-re (1 font-lock-function-name -face ))
2180
- (, typescript--function-call-re (1 font-lock -function-name- face ))
2179
+ (, typescript--decorator-re (1 ' font-lock-function-call -face ))
2180
+ (, typescript--function-call-re (1 (typescript- -function-face) ))
2181
2181
(,(concat " \\ (?:\\ .\\ s-*\\ )" typescript--function-call-re)
2182
- (1 font-lock-function-name -face t ))
2183
- (, typescript--builtin-re (1 font-lock-type-face ))
2182
+ (1 ' font-lock-function-call -face t ))
2183
+ (, typescript--builtin-re (1 ' font-lock-type-face ))
2184
2184
2185
2185
; ; arrow function
2186
2186
(" \\ (=>\\ )"
2187
- (1 font-lock-keyword-face ))
2187
+ (1 ' font-lock-keyword-face ))
2188
2188
2189
2189
(typescript--match-subst-in-quotes
2190
2190
(1 'font-lock-keyword-face t )
2191
2191
(2 'default t )
2192
2192
(3 'font-lock-keyword-face t )))
2193
2193
" Level four font lock for `typescript-mode' ." )
2194
2194
2195
+ (defun typescript--function-face ()
2196
+ " Return the face to use depending if it's a definition or a call.
2197
+ Point is assumed to be right after the open paren."
2198
+ (save-excursion
2199
+ (forward-char -1 )
2200
+ (if (condition-case nil
2201
+ (progn
2202
+ (forward-sexp 1 )
2203
+ (forward-comment (point-max ))
2204
+ (memq (char-after ) '(?: ?\{ )))
2205
+ (scan-error nil ))
2206
+ ; ; Looks like a declaration/definition.
2207
+ 'font-lock-function-name-face
2208
+ ; ; Probably just a call.
2209
+ 'font-lock-function-call-face )))
2210
+
2195
2211
(defconst typescript--font-lock-keywords
2196
2212
'(typescript--font-lock-keywords-4 typescript--font-lock-keywords-1
2197
2213
typescript--font-lock-keywords-2
0 commit comments