]> git.eshelyaron.com Git - emacs.git/commitdiff
Make "case" keyword a dedenter in Python
authorkobarity <kobarity@gmail.com>
Fri, 10 Mar 2023 05:26:22 +0000 (14:26 +0900)
committerDmitry Gutov <dgutov@yandex.ru>
Sat, 11 Mar 2023 13:18:47 +0000 (15:18 +0200)
* lisp/progmodes/python.el (python-rx): Add "case" to dedenter.
(python-info-dedenter-opening-block-positions): Add "case" to pairs.
* test/lisp/progmodes/python-tests.el (python-indent-dedenters-9):
New test.

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

index 1980815bae95e9576ae63419db38578840abe47a..8793fdc6458a777f3c7480293de3cfaf269f9ed1 100644 (file)
@@ -426,7 +426,7 @@ This variant of `rx' supports common Python named REGEXPS."
                                              (or "def" "for" "with")))
                                     symbol-end))
             (dedenter          (seq symbol-start
-                                    (or "elif" "else" "except" "finally")
+                                    (or "elif" "else" "except" "finally" "case")
                                     symbol-end))
             (block-ender       (seq symbol-start
                                     (or
@@ -5783,7 +5783,8 @@ likely an invalid python file."
                (pairs '(("elif" "elif" "if")
                         ("else" "if" "elif" "except" "for" "while")
                         ("except" "except" "try")
-                        ("finally" "else" "except" "try")))
+                        ("finally" "else" "except" "try")
+                        ("case" "case")))
                (dedenter (match-string-no-properties 0))
                (possible-opening-blocks (cdr (assoc-string dedenter pairs)))
                (collected-indentations)
index 2568299bb665c6c34f1114970a5469fa5f9a4a1c..e5a9d128bc5aefc2e49cda22916e663b8de612fa 100644 (file)
@@ -1658,6 +1658,21 @@ a == 4):
    (python-indent-line t)
    (should (= (python-indent-calculate-indentation t) 6))))
 
+(ert-deftest python-indent-dedenters-9 ()
+  "Test de-indentation for the case keyword."
+  (python-tests-with-temp-buffer
+   "
+match a:
+    case 1:
+        print(1)
+        case 2
+"
+   (python-tests-look-at "case 2")
+   (should (eq (car (python-indent-context)) :at-dedenter-block-start))
+   (should (= (python-indent-calculate-indentation) 4))
+   (python-indent-line t)
+   (should (= (python-indent-calculate-indentation t) 4))))
+
 (ert-deftest python-indent-inside-string-1 ()
   "Test indentation for strings."
   (python-tests-with-temp-buffer