]> git.eshelyaron.com Git - emacs.git/commitdiff
python: Better support := in electric-layout-mode
authorStefan Kangas <stefankangas@gmail.com>
Fri, 4 Oct 2024 21:31:09 +0000 (23:31 +0200)
committerEshel Yaron <me@eshelyaron.com>
Sun, 6 Oct 2024 06:48:46 +0000 (08:48 +0200)
* lisp/progmodes/python.el (python-base-mode): Use heuristic to better
support the Python walrus operator := in electric-layout-rules.

(cherry picked from commit ee6e737fdf7a1bea3abec383a1bf0ae69ac8ca77)

lisp/progmodes/python.el

index 566f7716a69e1ac9aeb2613d3a63be64cee5ba69..ece90b9f18fb0898dfb924cfc7e366ff2f81c3f7 100644 (file)
@@ -7072,6 +7072,11 @@ implementations: `python-mode' and `python-ts-mode'."
               `((?: . ,(lambda ()
                          (and (zerop (car (syntax-ppss)))
                               (python-info-statement-starts-block-p)
+                              ;; Heuristic: assume walrus operator :=
+                              ;; when colon is preceded by space.
+                              (save-excursion
+                                (goto-char (- (point) 2))
+                                (looking-at (rx (not space) ":")))
                               'after)))))
 
   ;; Add """ ... """ pairing to electric-pair-mode.