Skip to content

Commit acd8d79

Browse files
authored
Merge pull request #170 from Fuco1/feature/fontify-arrow-fn-args
feat(fontlock): fontify arrow fn arguments
2 parents e5704af + 84bab53 commit acd8d79

File tree

3 files changed

+298
-33
lines changed

3 files changed

+298
-33
lines changed

typescript-mode-general-tests.el

+174-17
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ declare function declareFunctionDefn(x3: xty3, y3: yty3): ret3;"
371371
("exportedDefaultDefn" . font-lock-function-name-face)
372372
("declareFunctionDefn" . font-lock-function-name-face)
373373
(("x0" "x1" "x2" "x3") . font-lock-variable-name-face)
374-
(("y0" "y1" "y2" "y3") . font-lock-variable-name-face)
374+
(("\\by0" "\\by1" "\\by2" "\\by3") . font-lock-variable-name-face)
375375
(("ret0" "ret1" "ret2" "ret3") . nil))))
376376

377377
(ert-deftest font-lock/level-four ()
@@ -393,7 +393,7 @@ snake_cased_function(1, 2, 3)"
393393
("methodCall" . font-lock-function-name-face)
394394
("snake_cased_function" . font-lock-function-name-face)
395395
(("string" "boolean" "number" "any") . typescript-primitive-face)
396-
(("endpoint" "data") . nil)
396+
(("endpoint" "data") . font-lock-variable-name-face)
397397
(("<" ">" ",") . nil))))
398398

399399
(ert-deftest font-lock/method-call-with-keyword-name ()
@@ -621,19 +621,24 @@ should be fontified as variable, keyword and type."
621621
(should (eq (get-face-at "Namespaced") 'font-lock-type-face))
622622
(should (eq (get-face-at "ClassName") 'font-lock-type-face))))
623623

624-
(ert-deftest font-lock/variables-in-declaration-multiline-with-types ()
624+
(ert-deftest font-lock/funargs--function--multiline-with-types ()
625625
"Variables should be highlighted in multiline declarations with types."
626626
(test-with-fontified-buffer
627627
"function test(
628-
var1: Type1,
629-
var2: Type2,
628+
var1: Promise<U1, V1>,
629+
var2: (xxx: Foo) => Bar,
630+
var3: Type3,
630631
): RetType {\n}"
631632
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
632633
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))
633-
(should (eq (get-face-at "Type1") 'font-lock-type-face))
634-
(should (eq (get-face-at "Type2") 'font-lock-type-face))))
634+
(should (eq (get-face-at "var3") 'font-lock-variable-name-face))
635+
(should (eq (get-face-at "xxx") 'font-lock-variable-name-face))
636+
(should (eq (get-face-at "Promise") 'font-lock-type-face))
637+
(should (eq (get-face-at "U1") 'font-lock-type-face))
638+
(should (eq (get-face-at "Foo") 'font-lock-type-face))
639+
(should (eq (get-face-at "Type3") 'font-lock-type-face))))
635640

636-
(ert-deftest font-lock/variables-in-declaration-multiline-without-types ()
641+
(ert-deftest font-lock/funargs--function--multiline-without-types ()
637642
"Variables should be highlighted in multiline declarations without types."
638643
(test-with-fontified-buffer
639644
"function test(
@@ -643,33 +648,33 @@ var2,
643648
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
644649
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))))
645650

646-
(ert-deftest font-lock/variables-in-declaration-multiline-no-hanging-paren ()
647-
"Variables should be highlighted in multiline declarations with no hanging paren."
651+
(ert-deftest font-lock/funargs--function--multiline-hanging-paren ()
652+
"Variables should be highlighted in multiline declarations with hanging paren."
648653
(test-with-fontified-buffer
649654
"function test(
650655
var1,
651656
var2): RetType {\n}"
652657
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
653658
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))))
654659

655-
(ert-deftest font-lock/variables-in-declaration-multiline-ending-comma-no-hanging-paren ()
656-
"Variables should be highlighted in multiline declarations with no hanging paren and trailing comma."
660+
(ert-deftest font-lock/funargs--function--multiline-ending-comma-hanging-paren ()
661+
"Variables should be highlighted in multiline declarations with hanging paren and trailing comma."
657662
(test-with-fontified-buffer
658663
"function test(
659664
var1,
660665
var2,): RetType {\n}"
661666
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
662667
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))))
663668

664-
(ert-deftest font-lock/variables-in-declaration-singleline-ending-comma-hanging-paren ()
665-
"Variables should be highlighted in singleline declarations with hanging paren and trailing comma."
669+
(ert-deftest font-lock/funargs--function--singleline-ending-comma-no-hanging-paren ()
670+
"Variables should be highlighted in singleline declarations with no hanging paren and trailing comma."
666671
(test-with-fontified-buffer
667672
"function test(var1,var2,
668673
): RetType {\n}"
669674
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
670675
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))))
671676

672-
(ert-deftest font-lock/variables-in-declaration-singleline-with-types ()
677+
(ert-deftest font-lock/funargs--function--singleline-with-types ()
673678
"Variables should be highlighted in singleline declarations with types."
674679
(test-with-fontified-buffer
675680
"function test(var1: Foo, var2: Bar,): RetType {\n}"
@@ -678,13 +683,165 @@ var2,): RetType {\n}"
678683
(should (eq (get-face-at "Foo") 'font-lock-type-face))
679684
(should (eq (get-face-at "Bar") 'font-lock-type-face))))
680685

681-
(ert-deftest font-lock/variables-in-declaration-singleline-ending-comma-no-hanging-paren ()
682-
"Variables should be highlighted in singleline declarations with no hanging paren and trailing comma."
686+
(ert-deftest font-lock/funargs--function--singleline-ending-comma-hanging-paren ()
687+
"Variables should be highlighted in singleline declarations with hanging paren and trailing comma."
683688
(test-with-fontified-buffer
684689
"function test(var1,var2,): RetType {\n}"
685690
(should (eq (get-face-at "var1") 'font-lock-variable-name-face))
686691
(should (eq (get-face-at "var2") 'font-lock-variable-name-face))))
687692

693+
(ert-deftest font-lock/funargs--function--keywords-as-variables ()
694+
"Keywords when used as variables should have variable face"
695+
(test-with-fontified-buffer
696+
"function test(type, unknown): void {}"
697+
(should (eq (get-face-at "type") 'font-lock-variable-name-face))
698+
(should (eq (get-face-at "unknown") 'font-lock-variable-name-face))))
699+
700+
(ert-deftest font-lock/funargs--arrow--keywords-as-variables ()
701+
"Keywords when used as variables should have variable face"
702+
(test-with-fontified-buffer
703+
"const test = (type, unknown): void => {}"
704+
(should (eq (get-face-at "type") 'font-lock-variable-name-face))
705+
(should (eq (get-face-at "unknown") 'font-lock-variable-name-face))))
706+
707+
(ert-deftest font-lock/funargs--arrow--single-line--no-type ()
708+
(test-with-fontified-buffer
709+
"const test = (aaa, bbb, ccc): void => {}"
710+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
711+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
712+
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))))
713+
714+
(ert-deftest font-lock/funargs--arrow--single-line--no-type--no-return-type ()
715+
(test-with-fontified-buffer
716+
"const test = (aaa, bbb, ccc) => {}"
717+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
718+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
719+
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))))
720+
721+
(ert-deftest font-lock/funargs--arrow--single-line--no-type--trailing-comma ()
722+
(test-with-fontified-buffer
723+
"const test = (aaa, bbb, ccc,): void => {}"
724+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
725+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
726+
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))))
727+
728+
(ert-deftest font-lock/funargs--arrow--single-line--no-type--optional ()
729+
(test-with-fontified-buffer
730+
"const test = (aaa, bbb?): void => {}"
731+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
732+
(should (eq (get-face-at "bbb?") 'font-lock-variable-name-face))))
733+
734+
(ert-deftest font-lock/funargs--arrow--multiline--no-type ()
735+
(test-with-fontified-buffer
736+
"const test = (aaa, bbb,
737+
ccc, ddd): void => {}"
738+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
739+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
740+
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))
741+
(should (eq (get-face-at "ddd") 'font-lock-variable-name-face))))
742+
743+
(ert-deftest font-lock/funargs--arrow--multiline--no-type--newline-after-last ()
744+
(test-with-fontified-buffer
745+
"const test = (aaa, bbb,
746+
ccc, ddd
747+
): void => {}"
748+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
749+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
750+
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))
751+
(should (eq (get-face-at "ddd") 'font-lock-variable-name-face))))
752+
753+
(ert-deftest font-lock/funargs--arrow--multiline--no-type--newline-before-first ()
754+
(test-with-fontified-buffer
755+
"const test = (
756+
aaa, bbb,
757+
ccc, ddd
758+
): void => {}"
759+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
760+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
761+
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))
762+
(should (eq (get-face-at "ddd") 'font-lock-variable-name-face))))
763+
764+
(ert-deftest font-lock/funargs--arrow--multiline--no-type--with-comment ()
765+
(test-with-fontified-buffer
766+
"const test = (
767+
aaa, bbb, // comment
768+
ccc, ddd // comment
769+
): void => {}"
770+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
771+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
772+
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))
773+
(should (eq (get-face-at "ddd") 'font-lock-variable-name-face))))
774+
775+
(ert-deftest font-lock/funargs--arrow--single--mixed-type--newline-before-first ()
776+
(test-with-fontified-buffer
777+
"const test = (aaa, bbb: Promise, ccc: number, ddd): void => {}"
778+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
779+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
780+
(should (eq (get-face-at "ccc") 'font-lock-variable-name-face))
781+
(should (eq (get-face-at "ddd") 'font-lock-variable-name-face))
782+
(should (eq (get-face-at "Promise") 'font-lock-type-face))
783+
(should (eq (get-face-at "number") 'typescript-primitive-face))))
784+
785+
(ert-deftest font-lock/funargs--arrow--single--with-type--complex-type ()
786+
(test-with-fontified-buffer
787+
"const test = (aaa: Promise<U, V, (xxx: A) => Foo>, bbb): void => {}"
788+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
789+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
790+
(should (eq (get-face-at "xxx") 'font-lock-variable-name-face))))
791+
792+
(ert-deftest font-lock/funargs--arrow--multiline--with-type--newline-before-first-after-last ()
793+
(test-with-fontified-buffer
794+
"const test = (
795+
aaa: Foo,
796+
bbb: Bar
797+
): void => {}"
798+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
799+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
800+
(should (eq (get-face-at "Foo") 'font-lock-type-face))
801+
(should (eq (get-face-at "Bar") 'font-lock-type-face))))
802+
803+
(ert-deftest font-lock/funargs--arrow--multiline--with-type--newline-before-first-after-last--hanging-comma ()
804+
(test-with-fontified-buffer
805+
"const test = (
806+
aaa: Foo,
807+
bbb: Bar,
808+
): void => {}"
809+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
810+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
811+
(should (eq (get-face-at "Foo") 'font-lock-type-face))
812+
(should (eq (get-face-at "Bar") 'font-lock-type-face))))
813+
814+
(ert-deftest font-lock/funargs--method--multiline--with-type ()
815+
(test-with-fontified-buffer
816+
"class Foo { foo(
817+
aaa: Foo,
818+
bbb: Bar,
819+
): void {}"
820+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
821+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
822+
(should (eq (get-face-at "Foo") 'font-lock-type-face))
823+
(should (eq (get-face-at "Bar") 'font-lock-type-face))))
824+
825+
(ert-deftest font-lock/funargs--method--single-line--with-type ()
826+
(test-with-fontified-buffer
827+
"class Foo { foo(aaa: Foo,bbb: Bar,): void {}"
828+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
829+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))
830+
(should (eq (get-face-at "Foo") 'font-lock-type-face))
831+
(should (eq (get-face-at "Bar") 'font-lock-type-face))))
832+
833+
(ert-deftest font-lock/funargs--method--single-line--no-type ()
834+
(test-with-fontified-buffer
835+
"class Foo { foo(aaa, bbb): void {}"
836+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
837+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))))
838+
839+
(ert-deftest font-lock/funargs--method--single-line--no-return-type ()
840+
(test-with-fontified-buffer
841+
"class Foo { foo(aaa, bbb) {}"
842+
(should (eq (get-face-at "aaa") 'font-lock-variable-name-face))
843+
(should (eq (get-face-at "bbb") 'font-lock-variable-name-face))))
844+
688845
(defun flyspell-predicate-test (search-for)
689846
"This function runs a test on
690847
`typescript--flyspell-mode-predicate'. `SEARCH-FOR' is a string

typescript-mode-test-utilities.el

+11-11
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
(declare (debug t)
2727
(indent 1))
2828
`(test-with-temp-buffer
29-
,content
30-
(font-lock-fontify-buffer)
29+
,content
30+
(font-lock-ensure (point-min) (point-max))
3131
,@body))
3232

3333
(defun get-face-at (loc)
@@ -48,15 +48,15 @@ It should be a list of (LOCATION . FACE) pairs, where
4848
LOCATION can be either a single location, or list of locations,
4949
that are all expected to have the same face."
5050
(test-with-fontified-buffer
51-
contents
52-
;; Make sure our propertize function has been applied to the whole
53-
;; buffer.
54-
(syntax-propertize (point-max))
55-
(dolist (spec expected)
56-
(if (listp (car spec))
57-
(dolist (source (car spec))
58-
(should (eq (get-face-at source) (cdr spec))))
59-
(should (eq (get-face-at (car spec)) (cdr spec)))))))
51+
contents
52+
;; Make sure our propertize function has been applied to the whole
53+
;; buffer.
54+
(syntax-propertize (point-max))
55+
(dolist (spec expected)
56+
(if (listp (car spec))
57+
(dolist (source (car spec))
58+
(should (eq (get-face-at source) (cdr spec))))
59+
(should (eq (get-face-at (car spec)) (cdr spec)))))))
6060

6161
(provide 'typescript-mode-test-utilities)
6262

0 commit comments

Comments
 (0)