Make "case" keyword a dedenter in Python

* 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.
This commit is contained in:
kobarity 2023-03-10 14:26:22 +09:00 committed by Dmitry Gutov
parent db178517ce
commit c0cf69f7a1
2 changed files with 18 additions and 2 deletions

View file

@ -426,7 +426,7 @@ This variant of `rx' supports common Python named REGEXPS."
(or "def" "for" "with"))) (or "def" "for" "with")))
symbol-end)) symbol-end))
(dedenter (seq symbol-start (dedenter (seq symbol-start
(or "elif" "else" "except" "finally") (or "elif" "else" "except" "finally" "case")
symbol-end)) symbol-end))
(block-ender (seq symbol-start (block-ender (seq symbol-start
(or (or
@ -5783,7 +5783,8 @@ likely an invalid python file."
(pairs '(("elif" "elif" "if") (pairs '(("elif" "elif" "if")
("else" "if" "elif" "except" "for" "while") ("else" "if" "elif" "except" "for" "while")
("except" "except" "try") ("except" "except" "try")
("finally" "else" "except" "try"))) ("finally" "else" "except" "try")
("case" "case")))
(dedenter (match-string-no-properties 0)) (dedenter (match-string-no-properties 0))
(possible-opening-blocks (cdr (assoc-string dedenter pairs))) (possible-opening-blocks (cdr (assoc-string dedenter pairs)))
(collected-indentations) (collected-indentations)

View file

@ -1658,6 +1658,21 @@ a == 4):
(python-indent-line t) (python-indent-line t)
(should (= (python-indent-calculate-indentation t) 6)))) (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 () (ert-deftest python-indent-inside-string-1 ()
"Test indentation for strings." "Test indentation for strings."
(python-tests-with-temp-buffer (python-tests-with-temp-buffer