]> git.eshelyaron.com Git - emacs.git/commitdiff
python.el: Fix electric colon behavior
authorFabián Ezequiel Gallina <fgallina@gnu.org>
Mon, 22 Dec 2014 05:24:42 +0000 (02:24 -0300)
committerFabián Ezequiel Gallina <fgallina@gnu.org>
Mon, 22 Dec 2014 05:24:42 +0000 (02:24 -0300)
* lisp/progmodes/python.el (python-indent-post-self-insert-function):
Make colon to re-indent only for dedenters, handling
multiline-statements gracefully.

* test/automated/python-tests.el (python-indent-electric-colon-2)
(python-indent-electric-colon-3): New tests.

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

index e4f620ecb40e078fbc02d56ff88a1c77fe9117c1..c00d6bcc69019971d5bd790a7da242c608c104d5 100644 (file)
@@ -1,3 +1,9 @@
+2014-12-22  Fabián Ezequiel Gallina  <fgallina@gnu.org>
+
+       * progmodes/python.el (python-indent-post-self-insert-function):
+       Make colon to re-indent only for dedenters, handling
+       multiline-statements gracefully.
+
 2014-12-21  Michael Albinus  <michael.albinus@gmx.de>
 
        * net/tramp.el (tramp-handle-insert-file-contents):
index 357ca5b56d8306802981c806aa4fa7e13d33a844..6d3916c07a5619b72040ba24c4eb24fc793a1bfe 100644 (file)
@@ -1175,12 +1175,18 @@ the line will be re-indented automatically if needed."
            (eolp)
            ;; Avoid re-indenting on extra colon
            (not (equal ?: (char-before (1- (point)))))
-           (not (python-syntax-comment-or-string-p))
-           ;; Never re-indent at beginning of defun
-           (not (save-excursion
-                  (python-nav-beginning-of-statement)
-                  (python-info-looking-at-beginning-of-defun))))
-      (python-indent-line)))))
+           (not (python-syntax-comment-or-string-p)))
+      ;; Just re-indent dedenters
+      (let ((dedenter-pos (python-info-dedenter-statement-p))
+            (current-pos (point)))
+        (when dedenter-pos
+          (save-excursion
+            (goto-char dedenter-pos)
+            (python-indent-line)
+            (unless (= (line-number-at-pos dedenter-pos)
+                       (line-number-at-pos current-pos))
+              ;; Reindent region if this is a multiline statement
+              (python-indent-region dedenter-pos current-pos)))))))))
 
 \f
 ;;; Navigation
index a117834cd345812eab5c5d1df9de2676bd6320ab..a91392d3fe758a0c1801a12cda95f57943af3e6f 100644 (file)
@@ -1,3 +1,8 @@
+2014-12-22  Fabián Ezequiel Gallina  <fgallina@gnu.org>
+
+       * automated/python-tests.el (python-indent-electric-colon-2)
+       (python-indent-electric-colon-3): New tests.
+
 2014-12-14  João Távora  <joaotavora@gmail.com>
 
        * automated/electric-tests.el (autowrapping-7): Tests for
index f84ded8cad20f4e02a6d334d4c679a112ead5b93..d1713ac185182247ba3cb7db92bf2a435187029b 100644 (file)
@@ -740,6 +740,39 @@ def b()
    (python-tests-self-insert ":")
    (should (= (current-indentation) 0))))
 
+(ert-deftest python-indent-electric-colon-2 ()
+  "Test indentation case for dedenter."
+  (python-tests-with-temp-buffer
+   "
+if do:
+    something()
+    else
+"
+   (python-tests-look-at "else")
+   (goto-char (line-end-position))
+   (python-tests-self-insert ":")
+   (should (= (current-indentation) 0))))
+
+(ert-deftest python-indent-electric-colon-3 ()
+  "Test indentation case for multi-line dedenter."
+  (python-tests-with-temp-buffer
+   "
+if do:
+    something()
+    elif (this
+          and
+          that)
+"
+   (python-tests-look-at "that)")
+   (goto-char (line-end-position))
+   (python-tests-self-insert ":")
+   (python-tests-look-at "elif" -1)
+   (should (= (current-indentation) 0))
+   (python-tests-look-at "and")
+   (should (= (current-indentation) 6))
+   (python-tests-look-at "that)")
+   (should (= (current-indentation) 6))))
+
 (ert-deftest python-indent-region-1 ()
   "Test indentation case from Bug#18843."
   (let ((contents "