python.el: Fix python-shell-buffer-substring on indented code
Fixes: debbugs:21086 * lisp/progmodes/python.el (python-shell-buffer-substring): Respect current line indentation when calculating string. * test/automated/python-tests.el (python-shell-buffer-substring-10) (python-shell-buffer-substring-11) (python-shell-buffer-substring-12): New tests.
This commit is contained in:
parent
41cb0162c5
commit
af013e0d4a
2 changed files with 68 additions and 10 deletions
|
@ -2986,29 +2986,32 @@ the python shell:
|
|||
coding cookie is added.
|
||||
4. Wraps indented regions under an \"if True:\" block so the
|
||||
interpreter evaluates them correctly."
|
||||
(let* ((substring (buffer-substring-no-properties start end))
|
||||
(let* ((start (save-excursion
|
||||
;; Normalize start to the line beginning position.
|
||||
(goto-char start)
|
||||
(line-beginning-position)))
|
||||
(substring (buffer-substring-no-properties start end))
|
||||
(starts-at-point-min-p (save-restriction
|
||||
(widen)
|
||||
(= (point-min) start)))
|
||||
(encoding (python-info-encoding))
|
||||
(toplevel-p (zerop (save-excursion
|
||||
(goto-char start)
|
||||
(python-util-forward-comment 1)
|
||||
(current-indentation))))
|
||||
(fillstr (when (not starts-at-point-min-p)
|
||||
(concat
|
||||
(format "# -*- coding: %s -*-\n" encoding)
|
||||
(make-string
|
||||
;; Subtract 2 because of the coding cookie.
|
||||
(- (line-number-at-pos start) 2) ?\n))))
|
||||
(toplevel-block-p (save-excursion
|
||||
(goto-char start)
|
||||
(or (zerop (line-number-at-pos start))
|
||||
(progn
|
||||
(python-util-forward-comment 1)
|
||||
(zerop (current-indentation)))))))
|
||||
(- (line-number-at-pos start) 2) ?\n)))))
|
||||
(with-temp-buffer
|
||||
(python-mode)
|
||||
(if fillstr (insert fillstr))
|
||||
(when fillstr
|
||||
(insert fillstr))
|
||||
(insert substring)
|
||||
(goto-char (point-min))
|
||||
(when (not toplevel-block-p)
|
||||
(when (not toplevel-p)
|
||||
(insert "if True:")
|
||||
(delete-region (point) (line-end-position)))
|
||||
(when nomain
|
||||
|
|
|
@ -3276,6 +3276,61 @@ class Foo(models.Model):
|
|||
|
||||
"))))
|
||||
|
||||
(ert-deftest python-shell-buffer-substring-10 ()
|
||||
"Check substring from partial block."
|
||||
(python-tests-with-temp-buffer
|
||||
"
|
||||
def foo():
|
||||
print ('a')
|
||||
"
|
||||
(should (string= (python-shell-buffer-substring
|
||||
(python-tests-look-at "print ('a')")
|
||||
(point-max))
|
||||
"if True:
|
||||
|
||||
print ('a')
|
||||
"))))
|
||||
|
||||
(ert-deftest python-shell-buffer-substring-11 ()
|
||||
"Check substring from partial block and point within indentation."
|
||||
(python-tests-with-temp-buffer
|
||||
"
|
||||
def foo():
|
||||
print ('a')
|
||||
"
|
||||
(should (string= (python-shell-buffer-substring
|
||||
(progn
|
||||
(python-tests-look-at "print ('a')")
|
||||
(backward-char 1)
|
||||
(point))
|
||||
(point-max))
|
||||
"if True:
|
||||
|
||||
print ('a')
|
||||
"))))
|
||||
|
||||
(ert-deftest python-shell-buffer-substring-12 ()
|
||||
"Check substring from partial block and point in whitespace."
|
||||
(python-tests-with-temp-buffer
|
||||
"
|
||||
def foo():
|
||||
|
||||
# Whitespace
|
||||
|
||||
print ('a')
|
||||
"
|
||||
(should (string= (python-shell-buffer-substring
|
||||
(python-tests-look-at "# Whitespace")
|
||||
(point-max))
|
||||
"if True:
|
||||
|
||||
|
||||
# Whitespace
|
||||
|
||||
print ('a')
|
||||
"))))
|
||||
|
||||
|
||||
|
||||
;;; Shell completion
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue