emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] emacs-24 749813e: python.el: Fix electric colon behavior


From: Fabián Ezequiel Gallina
Subject: [Emacs-diffs] emacs-24 749813e: python.el: Fix electric colon behavior
Date: Mon, 22 Dec 2014 05:24:54 +0000

branch: emacs-24
commit 749813e9d4a844384e0450f6f7f88484b15e348a
Author: Fabián Ezequiel Gallina <address@hidden>
Commit: Fabián Ezequiel Gallina <address@hidden>

    python.el: Fix electric colon behavior
    
    * 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                 |    6 ++++++
 lisp/progmodes/python.el       |   18 ++++++++++++------
 test/ChangeLog                 |    5 +++++
 test/automated/python-tests.el |   33 +++++++++++++++++++++++++++++++++
 4 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index e4f620e..c00d6bc 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,9 @@
+2014-12-22  Fabián Ezequiel Gallina  <address@hidden>
+
+       * 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  <address@hidden>
 
        * net/tramp.el (tramp-handle-insert-file-contents):
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 357ca5b..6d3916c 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -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)))))))))
 
 
 ;;; Navigation
diff --git a/test/ChangeLog b/test/ChangeLog
index a117834c..a91392d 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,3 +1,8 @@
+2014-12-22  Fabián Ezequiel Gallina  <address@hidden>
+
+       * automated/python-tests.el (python-indent-electric-colon-2)
+       (python-indent-electric-colon-3): New tests.
+
 2014-12-14  João Távora  <address@hidden>
 
        * automated/electric-tests.el (autowrapping-7): Tests for
diff --git a/test/automated/python-tests.el b/test/automated/python-tests.el
index f84ded8..d1713ac 100644
--- a/test/automated/python-tests.el
+++ b/test/automated/python-tests.el
@@ -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 "



reply via email to

[Prev in Thread] Current Thread [Next in Thread]