Skip to content

Commit 481df3a

Browse files
authored
Merge pull request #193 from emacs-typescript/feature/various-fixes
Misc minor issues (courtesy of Stefan Monnier)
2 parents 8818e76 + 6f3b8b4 commit 481df3a

File tree

3 files changed

+74
-53
lines changed

3 files changed

+74
-53
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,7 @@ $RECYCLE.BIN/
8484
*.lnk
8585

8686
# End of https://www.gitignore.io/api/emacs,windows,linux
87+
88+
# ELPA-generated files.
89+
/typescript-mode-autoloads.el
90+
/typescript-mode-pkg.el

typescript-mode-general-tests.el

+8-7
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,15 @@ private async innerExecuteAsync<TResponse extends Response, TValue>(endpoint: st
391391
innerExecuteAsync(x: string, y: boolean, z: number, j?: any): Promise<FResponse> {\n
392392
console.log(this.methodCall());\n
393393
snake_cased_function(1, 2, 3)"
394-
'(("@decorator" . font-lock-function-name-face)
394+
'(("@decorator" . font-lock-function-call-face)
395395
("Foo" . font-lock-type-face)
396396
("private" . typescript-access-modifier-face)
397397
("innerExecuteAsync" . font-lock-function-name-face)
398398
(("TResponse" "FResponse" "Response" "TValue") . font-lock-type-face)
399399
("console" . font-lock-type-face)
400400
("this" . typescript-this-face)
401-
("methodCall" . font-lock-function-name-face)
402-
("snake_cased_function" . font-lock-function-name-face)
401+
("methodCall" . font-lock-function-call-face)
402+
("snake_cased_function" . font-lock-function-call-face)
403403
(("string" "boolean" "number" "any") . typescript-primitive-face)
404404
(("endpoint" "data") . font-lock-variable-name-face)
405405
(("<" ">" ",") . nil))))
@@ -413,9 +413,9 @@ app.post()
413413
app.delete()
414414
if (true) {}
415415
// for (abc) {}"
416-
(should (eq (get-face-at "get") 'font-lock-function-name-face))
417-
(should (eq (get-face-at "post") 'font-lock-function-name-face))
418-
(should (eq (get-face-at "delete") 'font-lock-function-name-face))
416+
(should (eq (get-face-at "get") 'font-lock-function-call-face))
417+
(should (eq (get-face-at "post") 'font-lock-function-call-face))
418+
(should (eq (get-face-at "delete") 'font-lock-function-call-face))
419419
(should (eq (get-face-at "if") 'font-lock-keyword-face))
420420
(should (eq (get-face-at "for") 'font-lock-comment-face))))
421421

@@ -861,7 +861,8 @@ bbb: Bar,
861861

862862
(ert-deftest font-lock/funargs--method--single-line--with-type ()
863863
(test-with-fontified-buffer
864-
"class Foo { foo(aaa: Foo,bbb: Bar,): void {}"
864+
"class Foo { foom(aaa: Foo,bbb: Bar,): void {}"
865+
(should (eq (get-face-at "foom") 'font-lock-function-name-face))
865866
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
866867
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
867868
(should (eq (get-face-at "Foo") 'font-lock-type-face))

typescript-mode.el

+62-46
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
;;; typescript-mode.el --- Major mode for editing typescript
1+
;;; typescript-mode.el --- Major mode for editing typescript -*- lexical-binding: t -*-
22

33
;; -----------------------------------------------------------------------------------
44
;; TypeScript support for Emacs
55
;; 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
77
;; Portions Copyright (C) Microsoft Open Technologies, Inc. All rights reserved.
88
;;
99
;; This program is free software: you can redistribute it and/or modify
@@ -312,13 +312,13 @@ Match group 1 is MUMBLE.")
312312

313313
(defconst typescript--font-lock-keywords-2
314314
(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))))
322322
"Level two font lock keywords for `typescript-mode'.")
323323

324324
;; typescript--pitem is the basic building block of the lexical
@@ -935,15 +935,16 @@ point at BOB."
935935
This function invokes `re-search-forward', but treats the buffer
936936
as if strings and comments have been removed."
937937
(let ((saved-point (point))
938-
(search-expr
938+
(search-fun
939939
(cond ((null count)
940-
'(typescript--re-search-forward-inner regexp bound 1))
940+
(lambda () (typescript--re-search-forward-inner regexp bound 1)))
941941
((< count 0)
942-
'(typescript--re-search-backward-inner regexp bound (- count)))
942+
(lambda () (typescript--re-search-backward-inner regexp bound (- count))))
943943
((> count 0)
944-
'(typescript--re-search-forward-inner regexp bound count)))))
944+
(lambda () (typescript--re-search-forward-inner regexp bound count)))
945+
(t #'ignore))))
945946
(condition-case err
946-
(eval search-expr)
947+
(funcall search-fun)
947948
(search-failed
948949
(goto-char saved-point)
949950
(unless noerror
@@ -990,15 +991,16 @@ If the point is in the last line, searching back for \"\\n\" will
990991
skip over the line with \"let b\". The newline found will be the
991992
one at the end of the line with \"let a\"."
992993
(let ((saved-point (point))
993-
(search-expr
994+
(search-fun
994995
(cond ((null count)
995-
`(typescript--re-search-backward-inner ,regexp ,bound 1))
996+
(lambda () (typescript--re-search-backward-inner regexp bound 1)))
996997
((< count 0)
997-
`(typescript--re-search-forward-inner ,regexp ,bound (- ,count)))
998+
(lambda () (typescript--re-search-forward-inner regexp bound (- count))))
998999
((> count 0)
999-
`(typescript--re-search-backward-inner ,regexp ,bound ,count)))))
1000+
(lambda () (typescript--re-search-backward-inner regexp bound count)))
1001+
(t #'ignore))))
10001002
(condition-case err
1001-
(eval search-expr)
1003+
(funcall search-fun)
10021004
(search-failed
10031005
(goto-char saved-point)
10041006
(unless noerror
@@ -1839,35 +1841,35 @@ and searches for the next token to be highlighted."
18391841
(0 'typescript-jsdoc-value t))
18401842

18411843
(typescript--tslint-flag-matcher
1842-
(1 font-lock-preprocessor-face t))
1844+
(1 'font-lock-preprocessor-face t))
18431845

18441846
("\\.\\(prototype\\)\\_>"
1845-
(1 font-lock-constant-face))
1847+
(1 'font-lock-constant-face))
18461848

18471849
(,(rx symbol-start "class" (+ space) (group (+ (or (syntax word) (syntax symbol)))))
1848-
(1 font-lock-type-face))
1850+
(1 'font-lock-type-face))
18491851

18501852
(,(rx symbol-start "extends" (+ space) (group (+ (or (syntax word) (syntax symbol)))))
1851-
(1 font-lock-type-face))
1853+
(1 'font-lock-type-face))
18521854

18531855
(,(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)))
18551857

18561858
(,(rx symbol-start "interface" (+ space) (group (+ (or (syntax word) (syntax symbol)))))
1857-
(1 font-lock-type-face))
1859+
(1 'font-lock-type-face))
18581860

18591861
(,(rx symbol-start "type" (+ space) (group (+ (or (syntax word) (syntax symbol)))))
1860-
(1 font-lock-type-face))
1862+
(1 'font-lock-type-face))
18611863

18621864
(,(rx symbol-start "enum" (+ space) (group (+ (or (syntax word) (syntax symbol)))))
1863-
(1 font-lock-type-face))
1865+
(1 'font-lock-type-face))
18641866

18651867
;; Highlights class being declared, in parts
18661868
(typescript--class-decl-matcher
18671869
,(concat "\\(" typescript--name-re "\\)\\(?:\\.\\|.*$\\)")
18681870
(goto-char (match-beginning 1))
18691871
nil
1870-
(1 font-lock-type-face))
1872+
(1 'font-lock-type-face))
18711873

18721874
;; Highlights parent class, in parts, if available
18731875
(typescript--class-decl-matcher
@@ -1884,20 +1886,20 @@ and searches for the next token to be highlighted."
18841886
(save-excursion
18851887
(goto-char typescript--tmp-location)
18861888
(delete-char 1)))
1887-
(1 font-lock-type-face))
1889+
(1 'font-lock-type-face))
18881890

18891891
;; Highlights parent class
18901892
(typescript--class-decl-matcher
1891-
(2 font-lock-type-face nil t))
1893+
(2 'font-lock-type-face nil t))
18921894

18931895
;; Dojo needs its own matcher to override the string highlighting
18941896
(,(typescript--make-framework-matcher
18951897
'dojo
18961898
"^\\s-*dojo\\.declare\\s-*(\""
18971899
"\\(" typescript--dotted-name-re "\\)"
18981900
"\\(?:\"\\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))
19011903

19021904
;; Match Dojo base classes. Of course Mojo has to be different
19031905
;; from everything else under the sun...
@@ -1909,7 +1911,7 @@ and searches for the next token to be highlighted."
19091911
"\\(?:\\].*$\\)?")
19101912
(backward-char)
19111913
(end-of-line)
1912-
(1 font-lock-type-face))
1914+
(1 'font-lock-type-face))
19131915

19141916
;; continued Dojo base-class list
19151917
(,(typescript--make-framework-matcher
@@ -1922,22 +1924,20 @@ and searches for the next token to be highlighted."
19221924
(forward-symbol -1)
19231925
(end-of-line))
19241926
(end-of-line)
1925-
(1 font-lock-type-face))
1927+
(1 'font-lock-type-face))
19261928

19271929
;; variable declarations
19281930
,(list
19291931
(concat "\\_<\\(const\\|var\\|let\\)\\_>\\|" typescript--basic-type-re)
19301932
(list #'typescript--variable-decl-matcher nil nil nil))
19311933

19321934
;; 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))
19361937

19371938
;; 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))
19411941

19421942
;; formal parameters in "function" function call
19431943
;; function helloWorld(a: number, b: Promise<number>): void { }
@@ -2162,7 +2162,7 @@ This performs fontification according to `typescript--class-styles'."
21622162
;; - () => SomeType
21632163
;; TODO: namespaced classes!
21642164
,(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-*{?")
21662166
'(1 'font-lock-variable-name-face nil t)
21672167
'(2 'font-lock-keyword-face nil t)
21682168
'(3 'font-lock-type-face))
@@ -2176,22 +2176,38 @@ This performs fontification according to `typescript--class-styles'."
21762176
;;
21772177
,@typescript--font-lock-keywords-3
21782178

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)))
21812181
(,(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))
21842184

21852185
;; arrow function
21862186
("\\(=>\\)"
2187-
(1 font-lock-keyword-face))
2187+
(1 'font-lock-keyword-face))
21882188

21892189
(typescript--match-subst-in-quotes
21902190
(1 'font-lock-keyword-face t)
21912191
(2 'default t)
21922192
(3 'font-lock-keyword-face t)))
21932193
"Level four font lock for `typescript-mode'.")
21942194

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+
21952211
(defconst typescript--font-lock-keywords
21962212
'(typescript--font-lock-keywords-4 typescript--font-lock-keywords-1
21972213
typescript--font-lock-keywords-2

0 commit comments

Comments
 (0)