Fix electric indent bug in python-mode after dedenting colon

* list/progmodes/python.el (python-indent-post-self-insert-function):
Use markers instead of positions when reindenting statement(s) after
inserting electric colon to avoid reindenting too many
statements (bug#22663).

* test/lisp/progmodes/python-tests.el (python-indent-electric-colon-2):
Improve test case to also verify the fix of bug#22663.

Copyright-paperwork-exempt: yes
This commit is contained in:
Joel Rosdahl 2018-12-27 16:52:07 +01:00 committed by Eli Zaretskii
parent f6eacc468b
commit a3c79d44cc
2 changed files with 13 additions and 9 deletions

View file

@ -1334,16 +1334,17 @@ the line will be re-indented automatically if needed."
(not (equal ?: (char-before (1- (point)))))
(not (python-syntax-comment-or-string-p)))
;; Just re-indent dedenters
(let ((dedenter-pos (python-info-dedenter-statement-p))
(current-pos (point)))
(let ((dedenter-pos (python-info-dedenter-statement-p)))
(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)))))))))
(let ((start (copy-marker dedenter-pos))
(end (point-marker)))
(save-excursion
(goto-char start)
(python-indent-line)
(unless (= (line-number-at-pos start)
(line-number-at-pos end))
;; Reindent region if this is a multiline statement
(python-indent-region start end))))))))))
;;; Mark

View file

@ -1161,10 +1161,13 @@ def b()
if do:
something()
else
outside
"
(python-tests-look-at "else")
(goto-char (line-end-position))
(python-tests-self-insert ":")
(should (= (current-indentation) 0))
(python-tests-look-at "outside")
(should (= (current-indentation) 0))))
(ert-deftest python-indent-electric-colon-3 ()