* lisp/progmodes/python.el (python-indent-context): Respect user

indentation after comment.

* test/automated/python-tests.el (python-indent-pep8-1)
(python-indent-pep8-2, python-indent-pep8-3)
(python-indent-after-comment-2): Fix tests.
(python-indent-after-comment-3): New test.
This commit is contained in:
Fabián Ezequiel Gallina 2015-01-30 00:19:55 -03:00
parent 868df45153
commit 41c3b9241c
4 changed files with 66 additions and 41 deletions

View file

@ -1,3 +1,8 @@
2015-01-30 Fabián Ezequiel Gallina <fgallina@gnu.org>
* progmodes/python.el (python-indent-context): Respect user
indentation after comment.
2015-01-29 Tassilo Horn <tsdh@gnu.org> 2015-01-29 Tassilo Horn <tsdh@gnu.org>
* textmodes/reftex-vars.el (featurep): Conditionalize value of * textmodes/reftex-vars.el (featurep): Conditionalize value of

View file

@ -810,15 +810,6 @@ keyword
;; Beginning of buffer. ;; Beginning of buffer.
((= (line-number-at-pos) 1) ((= (line-number-at-pos) 1)
(cons :no-indent 0)) (cons :no-indent 0))
;; Comment continuation (maybe).
((save-excursion
(when (and
(or
(python-info-current-line-comment-p)
(python-info-current-line-empty-p))
(forward-comment -1)
(python-info-current-line-comment-p))
(cons :after-comment (point)))))
;; Inside a string. ;; Inside a string.
((let ((start (python-syntax-context 'string ppss))) ((let ((start (python-syntax-context 'string ppss)))
(when start (when start
@ -930,21 +921,22 @@ keyword
((let ((start (python-info-dedenter-statement-p))) ((let ((start (python-info-dedenter-statement-p)))
(when start (when start
(cons :at-dedenter-block-start start)))) (cons :at-dedenter-block-start start))))
;; After normal line. ;; After normal line, comment or ender (default case).
((let ((start (save-excursion ((save-excursion
(back-to-indentation) (back-to-indentation)
(skip-chars-backward " \t\n") (skip-chars-backward " \t\n")
(python-nav-beginning-of-statement) (python-nav-beginning-of-statement)
(point)))) (cons
(when start (cond ((python-info-current-line-comment-p)
(if (save-excursion :after-comment)
((save-excursion
(goto-char (line-end-position))
(python-util-forward-comment -1) (python-util-forward-comment -1)
(python-nav-beginning-of-statement) (python-nav-beginning-of-statement)
(looking-at (python-rx block-ender))) (looking-at (python-rx block-ender)))
(cons :after-block-end start) :after-block-end)
(cons :after-line start))))) (t :after-line))
;; Default case: do not indent. (point))))))))
(t (cons :no-indent 0))))))
(defun python-indent--calculate-indentation () (defun python-indent--calculate-indentation ()
"Internal implementation of `python-indent-calculate-indentation'. "Internal implementation of `python-indent-calculate-indentation'.

View file

@ -1,3 +1,10 @@
2015-01-30 Fabián Ezequiel Gallina <fgallina@gnu.org>
* automated/python-tests.el (python-indent-pep8-1)
(python-indent-pep8-2, python-indent-pep8-3)
(python-indent-after-comment-2): Fix tests.
(python-indent-after-comment-3): New test.
2015-01-26 Fabián Ezequiel Gallina <fgallina@gnu.org> 2015-01-26 Fabián Ezequiel Gallina <fgallina@gnu.org>
* automated/python-tests.el (python-indent-pep8-1) * automated/python-tests.el (python-indent-pep8-1)

View file

@ -177,7 +177,7 @@ foo = long_function_name(var_one, var_two,
(should (eq (car (python-indent-context)) :no-indent)) (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0)) (should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "foo = long_function_name(var_one, var_two,") (python-tests-look-at "foo = long_function_name(var_one, var_two,")
(should (eq (car (python-indent-context)) :after-line)) (should (eq (car (python-indent-context)) :after-comment))
(should (= (python-indent-calculate-indentation) 0)) (should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "var_three, var_four)") (python-tests-look-at "var_three, var_four)")
(should (eq (car (python-indent-context)) :inside-paren)) (should (eq (car (python-indent-context)) :inside-paren))
@ -195,7 +195,7 @@ def long_function_name(
(should (eq (car (python-indent-context)) :no-indent)) (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0)) (should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "def long_function_name(") (python-tests-look-at "def long_function_name(")
(should (eq (car (python-indent-context)) :after-line)) (should (eq (car (python-indent-context)) :after-comment))
(should (= (python-indent-calculate-indentation) 0)) (should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "var_one, var_two, var_three,") (python-tests-look-at "var_one, var_two, var_three,")
(should (eq (car (python-indent-context)) (should (eq (car (python-indent-context))
@ -221,7 +221,7 @@ foo = long_function_name(
(should (eq (car (python-indent-context)) :no-indent)) (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0)) (should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "foo = long_function_name(") (python-tests-look-at "foo = long_function_name(")
(should (eq (car (python-indent-context)) :after-line)) (should (eq (car (python-indent-context)) :after-comment))
(should (= (python-indent-calculate-indentation) 0)) (should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "var_one, var_two,") (python-tests-look-at "var_one, var_two,")
(should (eq (car (python-indent-context)) :inside-paren-newline-start)) (should (eq (car (python-indent-context)) :inside-paren-newline-start))
@ -286,10 +286,10 @@ class Blag(object):
def func(arg): def func(arg):
# I don't do much # I don't do much
return arg return arg
# This comment is badly indented just because. # This comment is badly indented because the user forced so.
# But we won't mess with the user in this line. # At this line python.el wont dedent, user is always right.
now_we_do_mess_cause_this_is_not_a_comment = 1 comment_wins_over_ender = True
# yeah, that. # yeah, that.
" "
@ -301,28 +301,49 @@ now_we_do_mess_cause_this_is_not_a_comment = 1
;; the rules won't apply here. ;; the rules won't apply here.
(should (eq (car (python-indent-context)) :after-block-start)) (should (eq (car (python-indent-context)) :after-block-start))
(should (= (python-indent-calculate-indentation) 4)) (should (= (python-indent-calculate-indentation) 4))
(python-tests-look-at "# This comment is badly") (python-tests-look-at "# This comment is badly indented")
(should (eq (car (python-indent-context)) :after-block-end)) (should (eq (car (python-indent-context)) :after-block-end))
;; The return keyword moves indentation backwards 4 spaces, but ;; The return keyword do make indentation lose a level...
;; let's assume this comment was placed there because the user
;; wanted to (manually adding spaces or whatever).
(should (= (python-indent-calculate-indentation) 0)) (should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "# but we won't mess") ;; ...but the current indentation was forced by the user.
(python-tests-look-at "# At this line python.el wont dedent")
(should (eq (car (python-indent-context)) :after-comment)) (should (eq (car (python-indent-context)) :after-comment))
(should (= (python-indent-calculate-indentation) 4)) (should (= (python-indent-calculate-indentation) 4))
;; Behave the same for blank lines: potentially a comment. ;; Should behave the same for blank lines: potentially a comment.
(forward-line 1) (forward-line 1)
(should (eq (car (python-indent-context)) :after-comment)) (should (eq (car (python-indent-context)) :after-comment))
(should (= (python-indent-calculate-indentation) 4)) (should (= (python-indent-calculate-indentation) 4))
(python-tests-look-at "now_we_do_mess") (python-tests-look-at "comment_wins_over_ender")
;; Here is where comment indentation starts to get ignored and ;; The comment won over the ender because the user said so.
;; where the user can't freely indent anymore. (should (eq (car (python-indent-context)) :after-comment))
(should (eq (car (python-indent-context)) :after-block-end)) (should (= (python-indent-calculate-indentation) 4))
(should (= (python-indent-calculate-indentation) 0)) ;; The indentation calculated fine for the assignment, but the user
;; choose to force it back to the first column. Next line should
;; be aware of that.
(python-tests-look-at "# yeah, that.") (python-tests-look-at "# yeah, that.")
(should (eq (car (python-indent-context)) :after-line)) (should (eq (car (python-indent-context)) :after-line))
(should (= (python-indent-calculate-indentation) 0)))) (should (= (python-indent-calculate-indentation) 0))))
(ert-deftest python-indent-after-comment-3 ()
"Test after-comment in buggy case."
(python-tests-with-temp-buffer
"
class A(object):
def something(self, arg):
if True:
return arg
# A comment
@adecorator
def method(self, a, b):
pass
"
(python-tests-look-at "@adecorator")
(should (eq (car (python-indent-context)) :after-comment))
(should (= (python-indent-calculate-indentation) 4))))
(ert-deftest python-indent-inside-paren-1 () (ert-deftest python-indent-inside-paren-1 ()
"The most simple inside-paren case that shouldn't fail." "The most simple inside-paren case that shouldn't fail."
(python-tests-with-temp-buffer (python-tests-with-temp-buffer