]> git.eshelyaron.com Git - emacs.git/commitdiff
Properly indent Python PEP634 match/case blocks
authorLele Gaifax <lele@metapensiero.it>
Sun, 22 May 2022 08:44:31 +0000 (10:44 +0200)
committerLars Ingebrigtsen <larsi@gnus.org>
Sun, 22 May 2022 11:22:59 +0000 (13:22 +0200)
Python 3.10 introduced the "structural pattern matching" syntax, and
commit 139042eb8629e6fd49b2c3002a8fc4d1aabd174d told font-lock about the
new keywords. This adds them also as block-start statements, to enable
proper indentation of such blocks.

* lisp/progmodes/python.el (python-rx): Add "match" and "case" as
block-start keywords.
* test/lisp/progmodes/python-tests.el (python-indent-after-match-block,
python-indent-after-case-block): New tests to verify indentation of
"match" and "case" blocks (bug#55572).

lisp/progmodes/python.el
test/lisp/progmodes/python-tests.el

index 0761aaebdcc3f51474bec4ec8d873adff7b01244..94297d4ea5b3c6b06f6135846b227b326ba3e2b5 100644 (file)
@@ -362,6 +362,8 @@ This variant of `rx' supports common Python named REGEXPS."
   `(rx-let ((block-start       (seq symbol-start
                                     (or "def" "class" "if" "elif" "else" "try"
                                         "except" "finally" "for" "while" "with"
+                                        ;; Python 3.10+ PEP634
+                                        "match" "case"
                                         ;; Python 3.5+ PEP492
                                         (and "async" (+ space)
                                              (or "def" "for" "with")))
index ee7b66610aae922fc0b8b30132f737f7a9dbd951..a3f778bbbe9db0a7f55f8c00ae87fba18a1e3535 100644 (file)
@@ -1516,6 +1516,31 @@ this is an arbitrarily
      (should (string= (buffer-substring-no-properties (point-min) (point-max))
                       expected)))))
 
+(ert-deftest python-indent-after-match-block ()
+  "Test PEP634 match."
+  (python-tests-with-temp-buffer
+   "
+match foo:
+"
+   (should (eq (car (python-indent-context)) :no-indent))
+   (should (= (python-indent-calculate-indentation) 0))
+   (goto-char (point-max))
+   (should (eq (car (python-indent-context)) :after-block-start))
+   (should (= (python-indent-calculate-indentation) 4))))
+
+(ert-deftest python-indent-after-case-block ()
+  "Test PEP634 case."
+  (python-tests-with-temp-buffer
+   "
+match foo:
+    case 1:
+"
+   (should (eq (car (python-indent-context)) :no-indent))
+   (should (= (python-indent-calculate-indentation) 0))
+   (goto-char (point-max))
+   (should (eq (car (python-indent-context)) :after-block-start))
+   (should (= (python-indent-calculate-indentation) 8))))
+
 \f
 ;;; Filling