]> git.eshelyaron.com Git - emacs.git/commitdiff
python-mode: Fix detection for opening blocks.
authorHong Xu <hong@topbug.net>
Tue, 13 Dec 2016 01:55:25 +0000 (17:55 -0800)
committerNoam Postavsky <npostavs@gmail.com>
Fri, 27 Jan 2017 01:14:19 +0000 (20:14 -0500)
* python.el (python-info-dedenter-opening-block-positions): There
can't be any back-indented lines between an opening block and the
current line.

* python-tests.el (python-indent-electric-colon-4): Add an indent
test case where there is one-more indented previous opening block.

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

index d8262dd0a750948e91134a196c315deef0cde7d9..90b5e4e0dc67e1dcc2663cbe49a4c7e167680d16 100644 (file)
@@ -4693,7 +4693,8 @@ likely an invalid python file."
     (let ((dedenter-pos (python-info-dedenter-statement-p)))
       (when dedenter-pos
         (goto-char dedenter-pos)
-        (let* ((pairs '(("elif" "elif" "if")
+        (let* ((cur-line (line-beginning-position))
+               (pairs '(("elif" "elif" "if")
                         ("else" "if" "elif" "except" "for" "while")
                         ("except" "except" "try")
                         ("finally" "else" "except" "try")))
@@ -4709,7 +4710,22 @@ likely an invalid python file."
               (let ((indentation (current-indentation)))
                 (when (and (not (memq indentation collected-indentations))
                            (or (not collected-indentations)
-                               (< indentation (apply #'min collected-indentations))))
+                               (< indentation (apply #'min collected-indentations)))
+                           ;; There must be no line with indentation
+                           ;; smaller than `indentation' (except for
+                           ;; blank lines) between the found opening
+                           ;; block and the current line, otherwise it
+                           ;; is not an opening block.
+                           (save-excursion
+                             (forward-line)
+                             (let ((no-back-indent t))
+                               (save-match-data
+                                 (while (and (< (point) cur-line)
+                                             (setq no-back-indent
+                                                   (or (> (current-indentation) indentation)
+                                                       (python-info-current-line-empty-p))))
+                                   (forward-line)))
+                               no-back-indent)))
                   (setq collected-indentations
                         (cons indentation collected-indentations))
                   (when (member (match-string-no-properties 0)
index 2df1bbf50d812bcbd59cb24f6ee974da7880025c..158c52f080c463b93fa09d7dd00cc6506cb3e6c4 100644 (file)
@@ -1156,6 +1156,27 @@ if do:
    (python-tests-look-at "that)")
    (should (= (current-indentation) 6))))
 
+(ert-deftest python-indent-electric-colon-4 ()
+  "Test indentation case where there is one more-indented previous open block."
+  (python-tests-with-temp-buffer
+   "
+def f():
+    if True:
+        a = 5
+
+        if True:
+            a = 10
+
+        b = 3
+
+else
+"
+   (python-tests-look-at "else")
+   (goto-char (line-end-position))
+   (python-tests-self-insert ":")
+   (python-tests-look-at "else" -1)
+   (should (= (current-indentation) 4))))
+
 (ert-deftest python-indent-region-1 ()
   "Test indentation case from Bug#18843."
   (let ((contents "