aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorkobarity2022-08-17 13:10:16 +0200
committerLars Ingebrigtsen2022-08-17 13:10:16 +0200
commit4915ca5dd4245a909c046e6691e8d4a1919890c8 (patch)
treea48242b42ff526acd35579ff0596e0add4887949 /lisp/progmodes/python.el
parent31e32212670f5774a6dbc0debac8854fa01d8f92 (diff)
downloademacs-4915ca5dd4245a909c046e6691e8d4a1919890c8.tar.gz
emacs-4915ca5dd4245a909c046e6691e8d4a1919890c8.zip
Enhance Python font-lock to support multilines
* test/lisp/progmodes/python-tests.el (python-tests-assert-faces-after-change): New helper function. (python-font-lock-keywords-level-1-3) (python-font-lock-assignment-statement-multiline-*): New tests. * lisp/progmodes/python.el (python-rx): Add `sp-nl' to represent space or newline (with/without backslash). (python-font-lock-keywords-level-1) (python-font-lock-keywords-maximum-decoration): Allow newlines where appropriate. (python-font-lock-extend-region): New function. (python-mode): Set `python-font-lock-extend-region' to `font-lock-extend-after-change-region-function'.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el48
1 files changed, 32 insertions, 16 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 44df3186b27..e1350391994 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -359,6 +359,7 @@
359 "Python mode specialized rx macro. 359 "Python mode specialized rx macro.
360This variant of `rx' supports common Python named REGEXPS." 360This variant of `rx' supports common Python named REGEXPS."
361 `(rx-let ((sp-bsnl (or space (and ?\\ ?\n))) 361 `(rx-let ((sp-bsnl (or space (and ?\\ ?\n)))
362 (sp-nl (or space (and (? ?\\) ?\n)))
362 (block-start (seq symbol-start 363 (block-start (seq symbol-start
363 (or "def" "class" "if" "elif" "else" "try" 364 (or "def" "class" "if" "elif" "else" "try"
364 "except" "finally" "for" "while" "with" 365 "except" "finally" "for" "while" "with"
@@ -583,9 +584,9 @@ the {...} holes that appear within f-strings."
583 finally return (and result-valid result)))) 584 finally return (and result-valid result))))
584 585
585(defvar python-font-lock-keywords-level-1 586(defvar python-font-lock-keywords-level-1
586 `((,(python-rx symbol-start "def" (1+ space) (group symbol-name)) 587 `((,(python-rx symbol-start "def" (1+ sp-bsnl) (group symbol-name))
587 (1 font-lock-function-name-face)) 588 (1 font-lock-function-name-face))
588 (,(python-rx symbol-start "class" (1+ space) (group symbol-name)) 589 (,(python-rx symbol-start "class" (1+ sp-bsnl) (group symbol-name))
589 (1 font-lock-type-face))) 590 (1 font-lock-type-face)))
590 "Font lock keywords to use in `python-mode' for level 1 decoration. 591 "Font lock keywords to use in `python-mode' for level 1 decoration.
591 592
@@ -725,12 +726,12 @@ sign in chained assignment."
725 ;; [*a] = 5, 6 726 ;; [*a] = 5, 6
726 ;; are handled separately below 727 ;; are handled separately below
727 (,(python-font-lock-assignment-matcher 728 (,(python-font-lock-assignment-matcher
728 (python-rx (? (or "[" "(") (* space)) 729 (python-rx (? (or "[" "(") (* sp-nl))
729 grouped-assignment-target (* space) ?, (* space) 730 grouped-assignment-target (* sp-nl) ?, (* sp-nl)
730 (* assignment-target (* space) ?, (* space)) 731 (* assignment-target (* sp-nl) ?, (* sp-nl))
731 (? assignment-target (* space)) 732 (? assignment-target (* sp-nl))
732 (? ?, (* space)) 733 (? ?, (* sp-nl))
733 (? (or ")" "]") (* space)) 734 (? (or ")" "]") (* sp-bsnl))
734 (group assignment-operator))) 735 (group assignment-operator)))
735 (1 font-lock-variable-name-face) 736 (1 font-lock-variable-name-face)
736 (,(python-rx grouped-assignment-target) 737 (,(python-rx grouped-assignment-target)
@@ -745,19 +746,20 @@ sign in chained assignment."
745 ;; c: Collection = {1, 2, 3} 746 ;; c: Collection = {1, 2, 3}
746 ;; d: Mapping[int, str] = {1: 'bar', 2: 'baz'} 747 ;; d: Mapping[int, str] = {1: 'bar', 2: 'baz'}
747 (,(python-font-lock-assignment-matcher 748 (,(python-font-lock-assignment-matcher
748 (python-rx grouped-assignment-target (* space) 749 (python-rx (or line-start ?\;) (* sp-bsnl)
749 (? ?: (* space) (+ not-simple-operator) (* space)) 750 grouped-assignment-target (* sp-bsnl)
750 assignment-operator)) 751 (? ?: (* sp-bsnl) (+ not-simple-operator) (* sp-bsnl))
752 assignment-operator))
751 (1 font-lock-variable-name-face)) 753 (1 font-lock-variable-name-face))
752 ;; special cases 754 ;; special cases
753 ;; (a) = 5 755 ;; (a) = 5
754 ;; [a] = 5, 756 ;; [a] = 5,
755 ;; [*a] = 5, 6 757 ;; [*a] = 5, 6
756 (,(python-font-lock-assignment-matcher 758 (,(python-font-lock-assignment-matcher
757 (python-rx (or line-start ?\; ?=) (* space) 759 (python-rx (or line-start ?\; ?=) (* sp-bsnl)
758 (or "[" "(") (* space) 760 (or "[" "(") (* sp-nl)
759 grouped-assignment-target (* space) 761 grouped-assignment-target (* sp-nl)
760 (or ")" "]") (* space) 762 (or ")" "]") (* sp-bsnl)
761 assignment-operator)) 763 assignment-operator))
762 (1 font-lock-variable-name-face)) 764 (1 font-lock-variable-name-face))
763 ;; escape sequences within bytes literals 765 ;; escape sequences within bytes literals
@@ -796,6 +798,18 @@ decorators, exceptions, and assignments.")
796Which one will be chosen depends on the value of 798Which one will be chosen depends on the value of
797`font-lock-maximum-decoration'.") 799`font-lock-maximum-decoration'.")
798 800
801(defun python-font-lock-extend-region (beg end _old-len)
802 "Extend font-lock region given by BEG and END to statement boundaries."
803 (save-excursion
804 (save-match-data
805 (goto-char beg)
806 (python-nav-beginning-of-statement)
807 (setq beg (point))
808 (goto-char end)
809 (python-nav-end-of-statement)
810 (setq end (point))
811 (cons beg end))))
812
799 813
800(defconst python-syntax-propertize-function 814(defconst python-syntax-propertize-function
801 (syntax-propertize-rules 815 (syntax-propertize-rules
@@ -5780,7 +5794,9 @@ REPORT-FN is Flymake's callback function."
5780 `(,python-font-lock-keywords 5794 `(,python-font-lock-keywords
5781 nil nil nil nil 5795 nil nil nil nil
5782 (font-lock-syntactic-face-function 5796 (font-lock-syntactic-face-function
5783 . python-font-lock-syntactic-face-function))) 5797 . python-font-lock-syntactic-face-function)
5798 (font-lock-extend-after-change-region-function
5799 . python-font-lock-extend-region)))
5784 5800
5785 (setq-local syntax-propertize-function 5801 (setq-local syntax-propertize-function
5786 python-syntax-propertize-function) 5802 python-syntax-propertize-function)