emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 296e4dd: Fix font lock of assignments with type hints in Python


From: Lars Ingebrigtsen
Subject: master 296e4dd: Fix font lock of assignments with type hints in Python
Date: Wed, 11 Nov 2020 04:50:08 -0500 (EST)

branch: master
commit 296e4dd15e3754fdddf70d33f2d630462d4b3309
Author: Dario Gjorgjevski <dario.gjorgjevski@gmail.com>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    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.
---
 lisp/progmodes/python.el | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 378ff8c..eb84b49 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))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]