From c97e7a2da2e5707fc94c2c5e2ddd5f2395cdb80b Mon Sep 17 00:00:00 2001 From: kobarity Date: Sat, 24 Feb 2024 23:11:02 +0900 Subject: 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. --- lisp/progmodes/python.el | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'lisp/progmodes/python.el') 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." ) symbol-end) . font-lock-type-face) + ;; single assignment with/without type hints, e.g. + ;; a: int = 5 + ;; b: Tuple[Optional[int], Union[Sequence[str], str]] = (None, 'foo') + ;; c: Collection = {1, 2, 3} + ;; d: Mapping[int, str] = {1: 'bar', 2: 'baz'} + (,(python-font-lock-assignment-matcher + (python-rx (or line-start ?\;) (* space) + grouped-assignment-target (* space) + (? ?: (* space) (group (+ not-simple-operator)) (* space)) + (group assignment-operator))) + (1 font-lock-variable-name-face) + (3 'font-lock-operator-face) + (,(python-rx symbol-name) + (progn + (when-let ((type-start (match-beginning 2))) + (goto-char type-start)) + (match-end 0)) + nil + (0 font-lock-type-face))) ;; multiple assignment ;; (note that type hints are not allowed for multiple assignments) ;; a, b, c = 1, 2, 3 @@ -826,18 +845,6 @@ sign in chained assignment." (match-beginning 2)) ; limit the search until the assignment nil (1 font-lock-variable-name-face))) - ;; single assignment with type hints, e.g. - ;; a: int = 5 - ;; b: Tuple[Optional[int], Union[Sequence[str], str]] = (None, 'foo') - ;; c: Collection = {1, 2, 3} - ;; d: Mapping[int, str] = {1: 'bar', 2: 'baz'} - (,(python-font-lock-assignment-matcher - (python-rx (or line-start ?\;) (* space) - grouped-assignment-target (* space) - (? ?: (* space) (+ not-simple-operator) (* space)) - (group assignment-operator))) - (1 font-lock-variable-name-face) - (2 'font-lock-operator-face)) ;; special cases ;; (a) = 5 ;; [a] = 5, -- cgit v1.2.1