aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2014-11-15 18:10:58 -0300
committerFabián Ezequiel Gallina2014-11-15 18:10:58 -0300
commit89ebffc1f60ead9b35c88663eadb345c3f569c3d (patch)
treed40ecde08eac62c35e4016caec0b41fbb28efa9d
parenta6b42789b55688822b762a20865c8d2c812125b9 (diff)
downloademacs-89ebffc1f60ead9b35c88663eadb345c3f569c3d.tar.gz
emacs-89ebffc1f60ead9b35c88663eadb345c3f569c3d.zip
Fix region indentation
Fixes: debbugs:18843 * lisp/progmodes/python.el (python-indent-region): Use python-indent-line and skip special cases. * test/automated/python-tests.el (python-indent-region-1) (python-indent-region-2, python-indent-region-3) (python-indent-region-4, python-indent-region-5): New tests.
-rw-r--r--lisp/ChangeLog5
-rw-r--r--lisp/progmodes/python.el46
-rw-r--r--test/ChangeLog6
-rw-r--r--test/automated/python-tests.el104
4 files changed, 143 insertions, 18 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 8f00403c39b..fe06ac671b7 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,8 @@
12014-11-15 Fabián Ezequiel Gallina <fgallina@gnu.org>
2
3 * progmodes/python.el (python-indent-region): Use
4 python-indent-line and skip special cases. (Bug#18843)
5
12014-11-15 Michael Albinus <michael.albinus@gmx.de> 62014-11-15 Michael Albinus <michael.albinus@gmx.de>
2 7
3 * vc/vc-hg.el (vc-hg-state): Disable pager. (Bug#18940) 8 * vc/vc-hg.el (vc-hg-state): Disable pager. (Bug#18940)
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 95fc52d4d54..5f8d7a29fa6 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1061,24 +1061,34 @@ Called from a program, START and END specify the region to indent."
1061 (or (bolp) (forward-line 1)) 1061 (or (bolp) (forward-line 1))
1062 (while (< (point) end) 1062 (while (< (point) end)
1063 (or (and (bolp) (eolp)) 1063 (or (and (bolp) (eolp))
1064 (let (word) 1064 (when (and
1065 (forward-line -1) 1065 ;; Skip if previous line is empty or a comment.
1066 (back-to-indentation) 1066 (save-excursion
1067 (setq word (current-word)) 1067 (let ((line-is-comment-p
1068 (forward-line 1) 1068 (python-info-current-line-comment-p)))
1069 (when (and word 1069 (forward-line -1)
1070 ;; Don't mess with strings, unless it's the 1070 (not
1071 ;; enclosing set of quotes. 1071 (or (and (python-info-current-line-comment-p)
1072 (or (not (python-syntax-context 'string)) 1072 ;; Unless this line is a comment too.
1073 (eq 1073 (not line-is-comment-p))
1074 (syntax-after 1074 (python-info-current-line-empty-p)))))
1075 (+ (1- (point)) 1075 ;; Don't mess with strings, unless it's the
1076 (current-indentation) 1076 ;; enclosing set of quotes.
1077 (python-syntax-count-quotes (char-after) (point)))) 1077 (or (not (python-syntax-context 'string))
1078 (string-to-syntax "|")))) 1078 (eq
1079 (beginning-of-line) 1079 (syntax-after
1080 (delete-horizontal-space) 1080 (+ (1- (point))
1081 (indent-to (python-indent-calculate-indentation))))) 1081 (current-indentation)
1082 (python-syntax-count-quotes (char-after) (point))))
1083 (string-to-syntax "|")))
1084 ;; Skip if current line is a block start, a
1085 ;; dedenter or block ender.
1086 (save-excursion
1087 (back-to-indentation)
1088 (not (looking-at
1089 (python-rx
1090 (or block-start dedenter block-ender))))))
1091 (python-indent-line)))
1082 (forward-line 1)) 1092 (forward-line 1))
1083 (move-marker end nil)))) 1093 (move-marker end nil))))
1084 1094
diff --git a/test/ChangeLog b/test/ChangeLog
index a5ee4b60589..971a4f8f400 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,3 +1,9 @@
12014-11-15 Fabián Ezequiel Gallina <fgallina@gnu.org>
2
3 * automated/python-tests.el (python-indent-region-1)
4 (python-indent-region-2, python-indent-region-3)
5 (python-indent-region-4, python-indent-region-5): New tests.
6
12014-11-08 Michael Albinus <michael.albinus@gmx.de> 72014-11-08 Michael Albinus <michael.albinus@gmx.de>
2 8
3 Backport Tramp changes from trunk. 9 Backport Tramp changes from trunk.
diff --git a/test/automated/python-tests.el b/test/automated/python-tests.el
index 39195fd7086..8c657c38b64 100644
--- a/test/automated/python-tests.el
+++ b/test/automated/python-tests.el
@@ -725,6 +725,110 @@ def b()
725 (python-tests-self-insert ":") 725 (python-tests-self-insert ":")
726 (should (= (current-indentation) 0)))) 726 (should (= (current-indentation) 0))))
727 727
728(ert-deftest python-indent-region-1 ()
729 "Test indentation case from Bug#18843."
730 (let ((contents "
731def foo ():
732 try:
733 pass
734 except:
735 pass
736"))
737 (python-tests-with-temp-buffer
738 contents
739 (python-indent-region (point-min) (point-max))
740 (should (string= (buffer-substring-no-properties (point-min) (point-max))
741 contents)))))
742
743(ert-deftest python-indent-region-2 ()
744 "Test region indentation on comments."
745 (let ((contents "
746def f():
747 if True:
748 pass
749
750# This is
751# some multiline
752# comment
753"))
754 (python-tests-with-temp-buffer
755 contents
756 (python-indent-region (point-min) (point-max))
757 (should (string= (buffer-substring-no-properties (point-min) (point-max))
758 contents)))))
759
760(ert-deftest python-indent-region-3 ()
761 "Test region indentation on comments."
762 (let ((contents "
763def f():
764 if True:
765 pass
766# This is
767# some multiline
768# comment
769")
770 (expected "
771def f():
772 if True:
773 pass
774 # This is
775 # some multiline
776 # comment
777"))
778 (python-tests-with-temp-buffer
779 contents
780 (python-indent-region (point-min) (point-max))
781 (should (string= (buffer-substring-no-properties (point-min) (point-max))
782 expected)))))
783
784(ert-deftest python-indent-region-4 ()
785 "Test region indentation block starts, dedenders and enders."
786 (let ((contents "
787def f():
788 if True:
789a = 5
790 else:
791 a = 10
792 return a
793")
794 (expected "
795def f():
796 if True:
797 a = 5
798 else:
799 a = 10
800 return a
801"))
802 (python-tests-with-temp-buffer
803 contents
804 (python-indent-region (point-min) (point-max))
805 (should (string= (buffer-substring-no-properties (point-min) (point-max))
806 expected)))))
807
808(ert-deftest python-indent-region-5 ()
809 "Test region indentation leaves strings untouched (start delimiter)."
810 (let ((contents "
811def f():
812'''
813this is
814a multiline
815string
816'''
817")
818 (expected "
819def f():
820 '''
821this is
822a multiline
823string
824'''
825"))
826 (python-tests-with-temp-buffer
827 contents
828 (python-indent-region (point-min) (point-max))
829 (should (string= (buffer-substring-no-properties (point-min) (point-max))
830 expected)))))
831
728 832
729;;; Navigation 833;;; Navigation
730 834