From c0cf69f7a17b657c784518434e1a049ce6970a43 Mon Sep 17 00:00:00 2001 From: kobarity Date: Fri, 10 Mar 2023 14:26:22 +0900 Subject: [PATCH] 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. --- lisp/progmodes/python.el | 5 +++-- test/lisp/progmodes/python-tests.el | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 1980815bae9..8793fdc6458 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -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) diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index 2568299bb66..e5a9d128bc5 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el @@ -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 -- 2.39.2