From: Dmitry Gutov Date: Wed, 8 Mar 2023 20:41:23 +0000 (+0200) Subject: Don't misindent 'else:' after 'if re.match:' in Python X-Git-Tag: emacs-29.0.90~218 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=34c14430e9d;p=emacs.git 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 --- 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