]> git.eshelyaron.com Git - emacs.git/commitdiff
* lisp/progmodes/python.el (python-indent-calculate-indentation): Fix
authorFabián Ezequiel Gallina <fgallina@gnu.org>
Thu, 12 Dec 2013 23:32:05 +0000 (20:32 -0300)
committerFabián Ezequiel Gallina <fgallina@gnu.org>
Thu, 12 Dec 2013 23:32:05 +0000 (20:32 -0300)
de-denters cornercase.

* test/automated/python-tests.el (python-indent-dedenters-2): New test.

Fixes: debbugs:15731
lisp/ChangeLog
lisp/progmodes/python.el
test/ChangeLog
test/automated/python-tests.el

index 59fe5fff0b8b7082b86bf1cb8875fa9af6884f7c..11dd2d69c0413611a6224e5d9e051befe05e2415 100644 (file)
@@ -1,3 +1,8 @@
+2013-12-12  Fabián Ezequiel Gallina  <fgallina@gnu.org>
+
+       * progmodes/python.el (python-indent-calculate-indentation): Fix
+       de-denters cornercase. (Bug#15731)
+
 2013-12-12  Stefan Monnier  <monnier@iro.umontreal.ca>
 
        * emacs-lisp/nadvice.el: Add `depth' property to manage ordering.
index 33039a4d087095451f4ade153656c20fbae00ed3..8de1717096f3e1c506ea19502799c95e741643e8 100644 (file)
@@ -780,19 +780,31 @@ START is the buffer position where the sexp starts."
           ;; indentation, in the case current line starts with a
           ;; `python-indent-dedenters' de-indent one level.
           (`after-line
-           (-
-            (save-excursion
-              (goto-char context-start)
-              (current-indentation))
-            (if (or (save-excursion
-                      (back-to-indentation)
-                      (looking-at (regexp-opt python-indent-dedenters)))
-                    (save-excursion
-                      (python-util-forward-comment -1)
-                      (python-nav-beginning-of-statement)
-                      (looking-at (regexp-opt python-indent-block-enders))))
-                python-indent-offset
-              0)))
+           (let* ((pair (save-excursion
+                          (goto-char context-start)
+                          (cons
+                           (current-indentation)
+                           (python-info-beginning-of-block-p))))
+                  (context-indentation (car pair))
+                  (after-block-start-p (cdr pair))
+                  (adjustment
+                   (if (or (save-excursion
+                             (back-to-indentation)
+                             (and
+                              ;; De-indent only when dedenters are not
+                              ;; next to a block start. This allows
+                              ;; one-liner constructs such as:
+                              ;;     if condition: print "yay"
+                              ;;     else: print "wry"
+                              (not after-block-start-p)
+                              (looking-at (regexp-opt python-indent-dedenters))))
+                           (save-excursion
+                             (python-util-forward-comment -1)
+                             (python-nav-beginning-of-statement)
+                             (looking-at (regexp-opt python-indent-block-enders))))
+                       python-indent-offset
+                     0)))
+             (- context-indentation adjustment)))
           ;; When inside of a string, do nothing. just use the current
           ;; indentation.  XXX: perhaps it would be a good idea to
           ;; invoke standard text indentation here
index 9931e81eb43783a8d90f15c7d38761c053e5c421..6f75b33af9500710f56bfc20ebd78ad368e3f94a 100644 (file)
@@ -1,3 +1,7 @@
+2013-12-12  Fabián Ezequiel Gallina  <fgallina@gnu.org>
+
+       * automated/python-tests.el (python-indent-dedenters-2): New test.
+
 2013-12-12  Fabián Ezequiel Gallina  <fgallina@gnu.org>
 
        * automated/python-tests.el (python-indent-after-comment-1)
index 84be598b6dd0edf213cf935e4adf7a05c399cd30..5756507fc92ca34ab1d0d3a3026c055b2c0936cd 100644 (file)
@@ -458,6 +458,28 @@ def foo(a, b, c):
    (should (eq (car (python-indent-context)) 'after-beginning-of-block))
    (should (= (python-indent-calculate-indentation) 12))))
 
+(ert-deftest python-indent-dedenters-2 ()
+  "Check one-liner block special case.."
+  (python-tests-with-temp-buffer
+   "
+cond = True
+if cond:
+
+    if cond: print 'True'
+else: print 'False'
+
+else:
+    return
+"
+   (python-tests-look-at "else: print 'False'")
+   ;; When a block has code after ":" it's just considered a simple
+   ;; line as that's a common thing to happen in one-liners.
+   (should (eq (car (python-indent-context)) 'after-line))
+   (should (= (python-indent-calculate-indentation) 4))
+   (python-tests-look-at "else:")
+   (should (eq (car (python-indent-context)) 'after-line))
+   (should (= (python-indent-calculate-indentation) 0))))
+
 (ert-deftest python-indent-after-backslash-1 ()
   "The most common case."
   (python-tests-with-temp-buffer