aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHong Xu2016-12-12 17:55:25 -0800
committerNoam Postavsky2017-01-26 20:14:19 -0500
commit7cb7a582f44db94292709d35f4f5474f891f03b0 (patch)
treee0ab66d0b9409565128b65e01061dbd061f48502
parent3485c1693efb709cfc15023d9b38978278731a32 (diff)
downloademacs-7cb7a582f44db94292709d35f4f5474f891f03b0.tar.gz
emacs-7cb7a582f44db94292709d35f4f5474f891f03b0.zip
python-mode: Fix detection for opening blocks.
* 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.
-rw-r--r--lisp/progmodes/python.el20
-rw-r--r--test/lisp/progmodes/python-tests.el21
2 files changed, 39 insertions, 2 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index d8262dd0a75..90b5e4e0dc6 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -4693,7 +4693,8 @@ likely an invalid python file."
4693 (let ((dedenter-pos (python-info-dedenter-statement-p))) 4693 (let ((dedenter-pos (python-info-dedenter-statement-p)))
4694 (when dedenter-pos 4694 (when dedenter-pos
4695 (goto-char dedenter-pos) 4695 (goto-char dedenter-pos)
4696 (let* ((pairs '(("elif" "elif" "if") 4696 (let* ((cur-line (line-beginning-position))
4697 (pairs '(("elif" "elif" "if")
4697 ("else" "if" "elif" "except" "for" "while") 4698 ("else" "if" "elif" "except" "for" "while")
4698 ("except" "except" "try") 4699 ("except" "except" "try")
4699 ("finally" "else" "except" "try"))) 4700 ("finally" "else" "except" "try")))
@@ -4709,7 +4710,22 @@ likely an invalid python file."
4709 (let ((indentation (current-indentation))) 4710 (let ((indentation (current-indentation)))
4710 (when (and (not (memq indentation collected-indentations)) 4711 (when (and (not (memq indentation collected-indentations))
4711 (or (not collected-indentations) 4712 (or (not collected-indentations)
4712 (< indentation (apply #'min collected-indentations)))) 4713 (< indentation (apply #'min collected-indentations)))
4714 ;; There must be no line with indentation
4715 ;; smaller than `indentation' (except for
4716 ;; blank lines) between the found opening
4717 ;; block and the current line, otherwise it
4718 ;; is not an opening block.
4719 (save-excursion
4720 (forward-line)
4721 (let ((no-back-indent t))
4722 (save-match-data
4723 (while (and (< (point) cur-line)
4724 (setq no-back-indent
4725 (or (> (current-indentation) indentation)
4726 (python-info-current-line-empty-p))))
4727 (forward-line)))
4728 no-back-indent)))
4713 (setq collected-indentations 4729 (setq collected-indentations
4714 (cons indentation collected-indentations)) 4730 (cons indentation collected-indentations))
4715 (when (member (match-string-no-properties 0) 4731 (when (member (match-string-no-properties 0)
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index 2df1bbf50d8..158c52f080c 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -1156,6 +1156,27 @@ if do:
1156 (python-tests-look-at "that)") 1156 (python-tests-look-at "that)")
1157 (should (= (current-indentation) 6)))) 1157 (should (= (current-indentation) 6))))
1158 1158
1159(ert-deftest python-indent-electric-colon-4 ()
1160 "Test indentation case where there is one more-indented previous open block."
1161 (python-tests-with-temp-buffer
1162 "
1163def f():
1164 if True:
1165 a = 5
1166
1167 if True:
1168 a = 10
1169
1170 b = 3
1171
1172else
1173"
1174 (python-tests-look-at "else")
1175 (goto-char (line-end-position))
1176 (python-tests-self-insert ":")
1177 (python-tests-look-at "else" -1)
1178 (should (= (current-indentation) 4))))
1179
1159(ert-deftest python-indent-region-1 () 1180(ert-deftest python-indent-region-1 ()
1160 "Test indentation case from Bug#18843." 1181 "Test indentation case from Bug#18843."
1161 (let ((contents " 1182 (let ((contents "