From 8f42db010d15efa21fb9007e61daedbe1e2dfa53 Mon Sep 17 00:00:00 2001 From: kobarity Date: Sat, 25 Mar 2023 22:59:05 +0900 Subject: [PATCH] Improve indenting "case" in Python * lisp/progmodes/python.el (python-info-dedenter-statement-p): Do not consider the first "case" in the block as dedenter. * test/lisp/progmodes/python-tests.el (python-info-dedenter-opening-block-positions-7) (python-info-dedenter-statement-p-6): New tests. (Bug#62092) --- lisp/progmodes/python.el | 9 ++++++- test/lisp/progmodes/python-tests.el | 38 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 2fe88323c35..bbabce80b4d 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -5854,7 +5854,14 @@ statement." (save-excursion (python-nav-beginning-of-statement) (when (and (not (python-syntax-context-type)) - (looking-at (python-rx dedenter))) + (looking-at (python-rx dedenter)) + ;; Exclude the first "case" in the block. + (not (and (string= (match-string-no-properties 0) + "case") + (save-excursion + (back-to-indentation) + (python-util-forward-comment -1) + (equal (char-before) ?:))))) (point)))) (defun python-info-line-ends-backslash-p (&optional line-number) diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index ed4a08da6ab..50153e66da5 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el @@ -5940,6 +5940,26 @@ def func(): (equal (list (python-tests-look-at "if (" -1 t)) (python-info-dedenter-opening-block-positions))))) +(ert-deftest python-info-dedenter-opening-block-positions-7 () + "Test case blocks." + (python-tests-with-temp-buffer + " +match a: + case 1: + match b: + case 2: + something() + case 3: +" + (python-tests-look-at "case 1:") + (should-not (python-info-dedenter-opening-block-positions)) + (python-tests-look-at "case 2:") + (should-not (python-info-dedenter-opening-block-positions)) + (python-tests-look-at "case 3:") + (equal (list (python-tests-look-at "case 2:" -1) + (python-tests-look-at "case 1:" -1 t)) + (python-info-dedenter-opening-block-positions)))) + (ert-deftest python-info-dedenter-opening-block-message-1 () "Test dedenters inside strings are ignored." (python-tests-with-temp-buffer @@ -6125,6 +6145,24 @@ elif b: (point)) (python-info-dedenter-statement-p))))) +(ert-deftest python-info-dedenter-statement-p-6 () + "Test case keyword." + (python-tests-with-temp-buffer + " +match a: # Comment + case 1: + match b: + case 2: + something() + case 3: +" + (python-tests-look-at "case 1:") + (should-not (python-info-dedenter-statement-p)) + (python-tests-look-at "case 2:") + (should-not (python-info-dedenter-statement-p)) + (python-tests-look-at "case 3:") + (should (= (point) (python-info-dedenter-statement-p))))) + (ert-deftest python-info-line-ends-backslash-p-1 () (python-tests-with-temp-buffer "