Skip to content

Commit

Permalink
Use font-lock-function-call-face for calls
Browse files Browse the repository at this point in the history
* typescript-mode.el (typescript--function-face): New function.
(typescript--font-lock-keywords-4): Use `font-lock-function-name-face`
only for definitions, not calls and not decorators.

* typescript-mode-general-tests.el (font-lock/level-four)
(font-lock/method-call-with-keyword-name): Adjust to new faces.
(font-lock/funargs--method--single-line--with-type): Also test
the face of the method definition.
  • Loading branch information
monnier committed Jan 18, 2025
1 parent 8818e76 commit 769c1f9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
15 changes: 8 additions & 7 deletions typescript-mode-general-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,15 @@ private async innerExecuteAsync<TResponse extends Response, TValue>(endpoint: st
innerExecuteAsync(x: string, y: boolean, z: number, j?: any): Promise<FResponse> {\n
console.log(this.methodCall());\n
snake_cased_function(1, 2, 3)"
'(("@decorator" . font-lock-function-name-face)
'(("@decorator" . font-lock-function-call-face)
("Foo" . font-lock-type-face)
("private" . typescript-access-modifier-face)
("innerExecuteAsync" . font-lock-function-name-face)
(("TResponse" "FResponse" "Response" "TValue") . font-lock-type-face)
("console" . font-lock-type-face)
("this" . typescript-this-face)
("methodCall" . font-lock-function-name-face)
("snake_cased_function" . font-lock-function-name-face)
("methodCall" . font-lock-function-call-face)
("snake_cased_function" . font-lock-function-call-face)
(("string" "boolean" "number" "any") . typescript-primitive-face)
(("endpoint" "data") . font-lock-variable-name-face)
(("<" ">" ",") . nil))))
Expand All @@ -413,9 +413,9 @@ app.post()
app.delete()
if (true) {}
// for (abc) {}"
(should (eq (get-face-at "get") 'font-lock-function-name-face))
(should (eq (get-face-at "post") 'font-lock-function-name-face))
(should (eq (get-face-at "delete") 'font-lock-function-name-face))
(should (eq (get-face-at "get") 'font-lock-function-call-face))
(should (eq (get-face-at "post") 'font-lock-function-call-face))
(should (eq (get-face-at "delete") 'font-lock-function-call-face))
(should (eq (get-face-at "if") 'font-lock-keyword-face))
(should (eq (get-face-at "for") 'font-lock-comment-face))))

Expand Down Expand Up @@ -861,7 +861,8 @@ bbb: Bar,

(ert-deftest font-lock/funargs--method--single-line--with-type ()
(test-with-fontified-buffer
"class Foo { foo(aaa: Foo,bbb: Bar,): void {}"
"class Foo { foom(aaa: Foo,bbb: Bar,): void {}"
(should (eq (get-face-at "foom") 'font-lock-function-name-face))
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
(should (eq (get-face-at "Foo") 'font-lock-type-face))
Expand Down
22 changes: 19 additions & 3 deletions typescript-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2176,10 +2176,10 @@ This performs fontification according to `typescript--class-styles'."
;;
,@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--decorator-re (1 'font-lock-function-call-face))
(,typescript--function-call-re (1 (typescript--function-face)))
(,(concat "\\(?:\\.\\s-*\\)" typescript--function-call-re)
(1 font-lock-function-name-face t))
(1 font-lock-function-call-face t))
(,typescript--builtin-re (1 font-lock-type-face))

;; arrow function
Expand All @@ -2192,6 +2192,22 @@ This performs fontification according to `typescript--class-styles'."
(3 'font-lock-keyword-face t)))
"Level four font lock for `typescript-mode'.")

(defun typescript--function-face ()
"Return the face to use depending if it's a definition or a call.
Point is assumed to be right after the open paren."
(save-excursion
(forward-char -1)
(if (condition-case nil
(progn
(forward-sexp 1)
(forward-comment (point-max))
(memq (char-after) '(?: ?\{)))
(scan-error nil))
;; Looks like a declaration/definition.
'font-lock-function-name-face
;; Probably just a call.
'font-lock-function-call-face)))

(defconst typescript--font-lock-keywords
'(typescript--font-lock-keywords-4 typescript--font-lock-keywords-1
typescript--font-lock-keywords-2
Expand Down

0 comments on commit 769c1f9

Please sign in to comment.