diff options
| author | Dario Gjorgjevski | 2020-11-11 10:49:04 +0100 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2020-11-11 10:49:04 +0100 |
| commit | 296e4dd15e3754fdddf70d33f2d630462d4b3309 (patch) | |
| tree | c68acf1a006fefda69f8a5ec65807a10f38e5ba9 /lisp/progmodes/python.el | |
| parent | 9038890ae21eed644251df9511cfda298a594ed9 (diff) | |
| download | emacs-296e4dd15e3754fdddf70d33f2d630462d4b3309.tar.gz emacs-296e4dd15e3754fdddf70d33f2d630462d4b3309.zip | |
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.
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 16 |
1 files changed, 10 insertions, 6 deletions
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.") | |||
| 662 | ;; assignments | 662 | ;; assignments |
| 663 | ;; support for a = b = c = 5 | 663 | ;; support for a = b = c = 5 |
| 664 | (,(lambda (limit) | 664 | (,(lambda (limit) |
| 665 | (let ((re (python-rx (group (+ (any word ?. ?_))) | 665 | (let ((re (python-rx (group (+ symbol-name)) |
| 666 | (? ?\[ (+ (not (any ?\]))) ?\]) (* space) | 666 | (? ?\[ (+ (not ?\])) ?\]) |
| 667 | (* space) | ||
| 667 | ;; A type, like " : int ". | 668 | ;; A type, like " : int ". |
| 668 | (? ?: (* space) (+ (any word ?. ?_)) (* space)) | 669 | (? ?: |
| 670 | (* space) | ||
| 671 | (+ not-simple-operator) | ||
| 672 | (* space)) | ||
| 669 | assignment-operator)) | 673 | assignment-operator)) |
| 670 | (res nil)) | 674 | (res nil)) |
| 671 | (while (and (setq res (re-search-forward re limit t)) | 675 | (while (and (setq res (re-search-forward re limit t)) |
| @@ -675,9 +679,9 @@ builtins.") | |||
| 675 | (1 font-lock-variable-name-face nil nil)) | 679 | (1 font-lock-variable-name-face nil nil)) |
| 676 | ;; support for a, b, c = (1, 2, 3) | 680 | ;; support for a, b, c = (1, 2, 3) |
| 677 | (,(lambda (limit) | 681 | (,(lambda (limit) |
| 678 | (let ((re (python-rx (group (+ (any word ?. ?_))) (* space) | 682 | (let ((re (python-rx (group (+ symbol-name)) (* space) |
| 679 | (* ?, (* space) (+ (any word ?. ?_)) (* space)) | 683 | (* ?, (* space) (+ symbol-name) (* space)) |
| 680 | ?, (* space) (+ (any word ?. ?_)) (* space) | 684 | ?, (* space) (+ symbol-name) (* space) |
| 681 | assignment-operator)) | 685 | assignment-operator)) |
| 682 | (res nil)) | 686 | (res nil)) |
| 683 | (while (and (setq res (re-search-forward re limit t)) | 687 | (while (and (setq res (re-search-forward re limit t)) |