From 34c14430e9d070ffc98527fc95677dd5c5758536 Mon Sep 17 00:00:00 2001 From: Dmitry Gutov Date: Wed, 8 Mar 2023 22:41:23 +0200 Subject: [PATCH] Don't misindent 'else:' after 'if re.match:' in Python * lisp/progmodes/python.el (python-info-dedenter-opening-block-positions): Check that the supposed block start is not a method call (bug#62031). * test/lisp/progmodes/python-tests.el (python-indent-after-re-match): New test. Co-authored-by: Lele Gaifax --- lisp/progmodes/python.el | 4 +++- test/lisp/progmodes/python-tests.el | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 1f970633bfc..81475f31f60 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -5792,7 +5792,9 @@ likely an invalid python file." (catch 'exit (while (python-nav--syntactically (lambda () - (re-search-backward (python-rx block-start) nil t)) + (cl-loop for pt = (re-search-backward (python-rx block-start) nil t) + until (memq (char-before) '(nil ?\s ?\t ?\n)) + finally return pt)) #'<) (let ((indentation (current-indentation))) (when (and (not (memq indentation collected-indentations)) diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index 4f24c042c6a..6928e313dc4 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el @@ -1982,6 +1982,18 @@ match foo: (should (eq (car (python-indent-context)) :after-block-start)) (should (= (python-indent-calculate-indentation) 8)))) +(ert-deftest python-indent-after-re-match () + "Test BUG 62031 regression." + (python-tests-with-temp-buffer + " +def test_re(string): + if re.match('^[a-c]+$', string): + print('yes') + else: + " + (python-tests-look-at "else:") + (should (= (python-indent-calculate-indentation) 4)))) + ;;; Filling -- 2.39.2