From 493be614dfedc3460e892469a52677f80977584d Mon Sep 17 00:00:00 2001 From: kobarity Date: Sun, 20 Apr 2025 21:14:46 +0900 Subject: [PATCH] Fix Python block end predicates (bug#77941) * lisp/progmodes/python.el (python-info-statement-ends-block-p) (python-info-end-of-block-p): Add consideration of comments. * test/lisp/progmodes/python-tests.el (python-info-statement-ends-block-p-3) (python-info-end-of-block-p-3): New tests. (cherry picked from commit 55cf15e163b407878921b4428e5436f01cf1fd90) --- lisp/progmodes/python.el | 9 +++++++-- test/lisp/progmodes/python-tests.el | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 3746ce3803f..95a74ccd52b 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -6107,7 +6107,9 @@ parent defun name." (let ((end-of-block-pos (save-excursion (python-nav-end-of-block))) (end-of-statement-pos (save-excursion - (python-nav-end-of-statement)))) + (python-nav-end-of-statement) + (python-util-forward-comment -1) + (point)))) (and end-of-block-pos end-of-statement-pos (= end-of-block-pos end-of-statement-pos)))) @@ -6130,7 +6132,10 @@ parent defun name." (defun python-info-end-of-block-p () "Return non-nil if point is at end of block." - (and (python-info-end-of-statement-p) + (and (= (point) (save-excursion + (python-nav-end-of-statement) + (python-util-forward-comment -1) + (point))) (python-info-statement-ends-block-p))) (define-obsolete-function-alias diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index dead3ab7346..6f9ce8c1cbb 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el @@ -5742,6 +5742,15 @@ if width == 0 and height == 0 and \\ (python-tests-look-at "raise ValueError(") (should (python-info-statement-ends-block-p)))) +(ert-deftest python-info-statement-ends-block-p-3 () + (python-tests-with-temp-buffer + " +def function(): + print() # Comment +" + (python-tests-look-at "print()") + (should (python-info-statement-ends-block-p)))) + (ert-deftest python-info-beginning-of-statement-p-1 () (python-tests-with-temp-buffer " @@ -5904,6 +5913,15 @@ if width == 0 and height == 0 and \\ (python-util-forward-comment -1) (should (python-info-end-of-block-p)))) +(ert-deftest python-info-end-of-block-p-3 () + (python-tests-with-temp-buffer + " +def function(): + print() # Comment +" + (python-tests-look-at " # Comment") + (should (python-info-end-of-block-p)))) + (ert-deftest python-info-dedenter-opening-block-position-1 () (python-tests-with-temp-buffer " -- 2.39.5