]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix python navigation problem with an empty line in nested defun
authorkobarity <kobarity@gmail.com>
Sat, 16 Jul 2022 16:14:15 +0000 (18:14 +0200)
committerLars Ingebrigtsen <larsi@gnus.org>
Sat, 16 Jul 2022 16:26:58 +0000 (18:26 +0200)
* lisp/progmodes/python.el (python-nav--beginning-of-defun): Fix
bug when point is on an empty line (bug#56600).

lisp/progmodes/python.el
test/lisp/progmodes/python-tests.el

index 1c99937c4b9a05fc2d513e33598bb037315cfd7b..f31832fec9c02da2c0e1fdf6f38f2cdebb8de057 100644 (file)
@@ -1454,21 +1454,24 @@ With positive ARG search backwards, else search forwards."
          (line-beg-pos (line-beginning-position))
          (line-content-start (+ line-beg-pos (current-indentation)))
          (pos (point-marker))
-         (min-indentation (+ (current-indentation)
-                             (if (python-info-looking-at-beginning-of-defun)
-                                 python-indent-offset 0)))
+         (min-indentation (if (python-info-current-line-empty-p)
+                              most-positive-fixnum
+                            (current-indentation)))
          (body-indentation
           (and (> arg 0)
-               (save-excursion
-                 (while (and
-                         (or (not (python-info-looking-at-beginning-of-defun))
-                             (>= (current-indentation) min-indentation))
-                         (setq min-indentation
-                               (min min-indentation (current-indentation)))
-                         (python-nav-backward-block)))
-                 (or (and (python-info-looking-at-beginning-of-defun)
-                          (+ (current-indentation) python-indent-offset))
-                     0))))
+               (or (and (python-info-looking-at-beginning-of-defun)
+                        (+ (current-indentation) python-indent-offset))
+                   (save-excursion
+                     (while
+                         (and
+                          (python-nav-backward-block)
+                          (or (not (python-info-looking-at-beginning-of-defun))
+                              (>= (current-indentation) min-indentation))
+                          (setq min-indentation
+                                (min min-indentation (current-indentation)))))
+                     (or (and (python-info-looking-at-beginning-of-defun)
+                              (+ (current-indentation) python-indent-offset))
+                         0)))))
          (found
           (progn
             (when (and (python-info-looking-at-beginning-of-defun nil t)
index 92c20288c8ead2fc6202b431745e1b3c75e18cdc..b2cccdd95694e5f7f683ab717ad8d67e0ce4b7c2 100644 (file)
@@ -1876,7 +1876,7 @@ class C(object):
                 (beginning-of-line)
                 (point))))
    ;; Nested defuns should be skipped.
-   (python-tests-look-at "return a" -1)
+   (forward-line -1)
    (should (= (save-excursion
                 (python-nav-beginning-of-defun)
                 (point))
@@ -1885,6 +1885,15 @@ class C(object):
                 (beginning-of-line)
                 (point))))
    ;; Defuns on same levels should be respected.
+   (python-tests-look-at "if True:" -1)
+   (forward-line -1)
+   (should (= (save-excursion
+                (python-nav-beginning-of-defun)
+                (point))
+              (save-excursion
+                (python-tests-look-at "def a():" -1)
+                (beginning-of-line)
+                (point))))
    (python-tests-look-at "def a():" -1)
    (should (= (save-excursion
                 (python-nav-beginning-of-defun)
@@ -1893,8 +1902,16 @@ class C(object):
                 (python-tests-look-at "def b():" -1)
                 (beginning-of-line)
                 (point))))
-   ;; Jump to a top level defun.
+   ;; Jump to an upper level defun.
    (python-tests-look-at "def b():" -1)
+   (should (= (save-excursion
+                (python-nav-beginning-of-defun)
+                (point))
+              (save-excursion
+                (python-tests-look-at "def m(self):" -1)
+                (beginning-of-line)
+                (point))))
+   (forward-line -1)
    (should (= (save-excursion
                 (python-nav-beginning-of-defun)
                 (point))
@@ -2009,6 +2026,15 @@ class C(object):
                 (python-tests-look-at "def c(self):")
                 (forward-line -1)
                 (point))))
+   (should (= (save-excursion
+                (python-tests-look-at "def b():")
+                (forward-line -1)
+                (python-nav-end-of-defun)
+                (point))
+              (save-excursion
+                (python-tests-look-at "def c(self):")
+                (forward-line -1)
+                (point))))
    (should (= (save-excursion
                 (python-tests-look-at "def b():")
                 (python-nav-end-of-defun)
@@ -2017,6 +2043,10 @@ class C(object):
                 (python-tests-look-at "def b():")
                 (forward-line 2)
                 (point))))
+   (should (not (save-excursion
+                  (python-tests-look-at "def a():")
+                  (forward-line -1)
+                  (python-nav-end-of-defun))))
    (should (= (save-excursion
                 (python-tests-look-at "def c(self):")
                 (python-nav-end-of-defun)