From 11e6c12def5e617fb07d604e82c9bf5289c6e827 Mon Sep 17 00:00:00 2001 From: kobarity Date: Sun, 7 Aug 2022 16:10:26 +0200 Subject: [PATCH] Fix blank/comment line handling in python-nav-beginning-of-block * lisp/progmodes/python.el (python-nav-beginning-of-block): Fix handling of blank/comment line right after block start (bug#57038). --- lisp/progmodes/python.el | 12 ++++++------ test/lisp/progmodes/python-tests.el | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 27bdbae3113..88b19c88cd9 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -1761,16 +1761,16 @@ backward to previous statement." "Move to start of current block." (interactive "^") (let ((starting-pos (point))) + ;; Go to first line beginning a statement + (while (and (not (bobp)) + (or (and (python-nav-beginning-of-statement) nil) + (python-info-current-line-comment-p) + (python-info-current-line-empty-p))) + (forward-line -1)) (if (progn (python-nav-beginning-of-statement) (looking-at (python-rx block-start))) (point-marker) - ;; Go to first line beginning a statement - (while (and (not (bobp)) - (or (and (python-nav-beginning-of-statement) nil) - (python-info-current-line-comment-p) - (python-info-current-line-empty-p))) - (forward-line -1)) (let ((block-matching-indent (- (current-indentation) python-indent-offset))) (while diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index 07f2c4f09a3..eb571226909 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el @@ -2734,6 +2734,28 @@ def decoratorFunctionWithArguments(arg1, arg2, arg3): (point)) (python-tests-look-at "def wwrap(f):" -1))))) +(ert-deftest python-nav-beginning-of-block-2 () + (python-tests-with-temp-buffer + " +if True: + + pass +if False: + # comment + pass +" + (python-tests-look-at "if True:") + (forward-line) + (should (= (save-excursion + (python-nav-beginning-of-block) + (point)) + (python-tests-look-at "if True:" -1))) + (python-tests-look-at "# comment") + (should (= (save-excursion + (python-nav-beginning-of-block) + (point)) + (python-tests-look-at "if False:" -1))))) + (ert-deftest python-nav-end-of-block-1 () (python-tests-with-temp-buffer " -- 2.39.2