From: Dario Gjorgjevski Date: Wed, 11 Nov 2020 09:49:04 +0000 (+0100) Subject: Fix font lock of assignments with type hints in Python X-Git-Tag: emacs-28.0.90~5188 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=296e4dd15e3754fdddf70d33f2d630462d4b3309;p=emacs.git Fix font lock of assignments with type hints in Python * lisp/progmodes/python.el (python-font-lock-keywords-maximum-decoration): Fix regular expressions for font lock of assignments with type hints (bug#44568). The font lock of assignments with type hints in Python is rather bad. Consider the following example: from typing import Mapping, Tuple, Sequence var1: int = 5 var2: Mapping[int, int] = {10: 1024} var3: Mapping[Tuple[int, int], int] = {(2, 5): 32} var4: Sequence[Sequence[int]] = [[1], [1, 2], [1, 2, 3]] var5: Sequence[Mapping[str, Sequence[str]]] = [ { 'red': ['scarlet', 'vermilion', 'ruby'], 'green': ['emerald green', 'aqua'] }, { 'sword': ['cutlass', 'rapier'] } ] As things stand right now, only ‘var1’ would be highlighted. To make things worse, the ‘Mapping’ type hint of ‘var2’ would also be highlighted, which is entirely incorrect. This commit makes all of ‘var1’ through ‘var5’ be highlighted correctly. --- diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 378ff8cc2c1..eb84b494e35 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -662,10 +662,14 @@ builtins.") ;; assignments ;; support for a = b = c = 5 (,(lambda (limit) - (let ((re (python-rx (group (+ (any word ?. ?_))) - (? ?\[ (+ (not (any ?\]))) ?\]) (* space) + (let ((re (python-rx (group (+ symbol-name)) + (? ?\[ (+ (not ?\])) ?\]) + (* space) ;; A type, like " : int ". - (? ?: (* space) (+ (any word ?. ?_)) (* space)) + (? ?: + (* space) + (+ not-simple-operator) + (* space)) assignment-operator)) (res nil)) (while (and (setq res (re-search-forward re limit t)) @@ -675,9 +679,9 @@ builtins.") (1 font-lock-variable-name-face nil nil)) ;; support for a, b, c = (1, 2, 3) (,(lambda (limit) - (let ((re (python-rx (group (+ (any word ?. ?_))) (* space) - (* ?, (* space) (+ (any word ?. ?_)) (* space)) - ?, (* space) (+ (any word ?. ?_)) (* space) + (let ((re (python-rx (group (+ symbol-name)) (* space) + (* ?, (* space) (+ symbol-name) (* space)) + ?, (* space) (+ symbol-name) (* space) assignment-operator)) (res nil)) (while (and (setq res (re-search-forward re limit t))