diff options
| author | kobarity | 2024-02-24 23:11:02 +0900 |
|---|---|---|
| committer | Stefan Kangas | 2024-05-18 22:22:34 +0200 |
| commit | c97e7a2da2e5707fc94c2c5e2ddd5f2395cdb80b (patch) | |
| tree | c63927620fc855bd3c4c36c09890fffb318e1234 /lisp/progmodes/python.el | |
| parent | bbb3038ad9feb1f37e4bef7151b2bc6f18c4123d (diff) | |
| download | emacs-c97e7a2da2e5707fc94c2c5e2ddd5f2395cdb80b.tar.gz emacs-c97e7a2da2e5707fc94c2c5e2ddd5f2395cdb80b.zip | |
Improve fontification of Python assignments with type hints
* lisp/progmodes/python.el (python-font-lock-keywords-maximum-decoration):
Fontify type hints of assignment statement. (Bug#69357)
* test/lisp/progmodes/python-tests.el
(python-font-lock-assignment-statement-11)
(python-font-lock-assignment-statement-12)
(python-font-lock-assignment-statement-13)
(python-font-lock-assignment-statement-18): Add fontification of type
hints.
(python-font-lock-assignment-statement-19): New test.
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 831bec7f4af..360936c9efd 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -794,6 +794,25 @@ sign in chained assignment." | |||
| 794 | ) | 794 | ) |
| 795 | symbol-end) | 795 | symbol-end) |
| 796 | . font-lock-type-face) | 796 | . font-lock-type-face) |
| 797 | ;; single assignment with/without type hints, e.g. | ||
| 798 | ;; a: int = 5 | ||
| 799 | ;; b: Tuple[Optional[int], Union[Sequence[str], str]] = (None, 'foo') | ||
| 800 | ;; c: Collection = {1, 2, 3} | ||
| 801 | ;; d: Mapping[int, str] = {1: 'bar', 2: 'baz'} | ||
| 802 | (,(python-font-lock-assignment-matcher | ||
| 803 | (python-rx (or line-start ?\;) (* space) | ||
| 804 | grouped-assignment-target (* space) | ||
| 805 | (? ?: (* space) (group (+ not-simple-operator)) (* space)) | ||
| 806 | (group assignment-operator))) | ||
| 807 | (1 font-lock-variable-name-face) | ||
| 808 | (3 'font-lock-operator-face) | ||
| 809 | (,(python-rx symbol-name) | ||
| 810 | (progn | ||
| 811 | (when-let ((type-start (match-beginning 2))) | ||
| 812 | (goto-char type-start)) | ||
| 813 | (match-end 0)) | ||
| 814 | nil | ||
| 815 | (0 font-lock-type-face))) | ||
| 797 | ;; multiple assignment | 816 | ;; multiple assignment |
| 798 | ;; (note that type hints are not allowed for multiple assignments) | 817 | ;; (note that type hints are not allowed for multiple assignments) |
| 799 | ;; a, b, c = 1, 2, 3 | 818 | ;; a, b, c = 1, 2, 3 |
| @@ -826,18 +845,6 @@ sign in chained assignment." | |||
| 826 | (match-beginning 2)) ; limit the search until the assignment | 845 | (match-beginning 2)) ; limit the search until the assignment |
| 827 | nil | 846 | nil |
| 828 | (1 font-lock-variable-name-face))) | 847 | (1 font-lock-variable-name-face))) |
| 829 | ;; single assignment with type hints, e.g. | ||
| 830 | ;; a: int = 5 | ||
| 831 | ;; b: Tuple[Optional[int], Union[Sequence[str], str]] = (None, 'foo') | ||
| 832 | ;; c: Collection = {1, 2, 3} | ||
| 833 | ;; d: Mapping[int, str] = {1: 'bar', 2: 'baz'} | ||
| 834 | (,(python-font-lock-assignment-matcher | ||
| 835 | (python-rx (or line-start ?\;) (* space) | ||
| 836 | grouped-assignment-target (* space) | ||
| 837 | (? ?: (* space) (+ not-simple-operator) (* space)) | ||
| 838 | (group assignment-operator))) | ||
| 839 | (1 font-lock-variable-name-face) | ||
| 840 | (2 'font-lock-operator-face)) | ||
| 841 | ;; special cases | 848 | ;; special cases |
| 842 | ;; (a) = 5 | 849 | ;; (a) = 5 |
| 843 | ;; [a] = 5, | 850 | ;; [a] = 5, |