* 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>
* textmodes/reftex-vars.el (featurep): Conditionalize value of

View file

@ -810,15 +810,6 @@ keyword
;; Beginning of buffer.
((= (line-number-at-pos) 1)
(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.
((let ((start (python-syntax-context 'string ppss)))
(when start
@ -930,21 +921,22 @@ keyword
((let ((start (python-info-dedenter-statement-p)))
(when start
(cons :at-dedenter-block-start start))))
;; After normal line.
((let ((start (save-excursion
(back-to-indentation)
(skip-chars-backward " \t\n")
(python-nav-beginning-of-statement)
(point))))
(when start
(if (save-excursion
(python-util-forward-comment -1)
(python-nav-beginning-of-statement)
(looking-at (python-rx block-ender)))
(cons :after-block-end start)
(cons :after-line start)))))
;; Default case: do not indent.
(t (cons :no-indent 0))))))
;; After normal line, comment or ender (default case).
((save-excursion
(back-to-indentation)
(skip-chars-backward " \t\n")
(python-nav-beginning-of-statement)
(cons
(cond ((python-info-current-line-comment-p)
:after-comment)
((save-excursion
(goto-char (line-end-position))
(python-util-forward-comment -1)
(python-nav-beginning-of-statement)
(looking-at (python-rx block-ender)))
:after-block-end)
(t :after-line))
(point))))))))
(defun 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>
* 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 (= (python-indent-calculate-indentation) 0))
(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))
(python-tests-look-at "var_three, var_four)")
(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 (= (python-indent-calculate-indentation) 0))
(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))
(python-tests-look-at "var_one, var_two, var_three,")
(should (eq (car (python-indent-context))
@ -221,7 +221,7 @@ foo = long_function_name(
(should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(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))
(python-tests-look-at "var_one, var_two,")
(should (eq (car (python-indent-context)) :inside-paren-newline-start))
@ -286,10 +286,10 @@ class Blag(object):
def func(arg):
# I don't do much
return arg
# This comment is badly indented just because.
# But we won't mess with the user in this line.
# This comment is badly indented because the user forced so.
# 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.
"
@ -301,28 +301,49 @@ now_we_do_mess_cause_this_is_not_a_comment = 1
;; the rules won't apply here.
(should (eq (car (python-indent-context)) :after-block-start))
(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))
;; The return keyword moves indentation backwards 4 spaces, but
;; let's assume this comment was placed there because the user
;; wanted to (manually adding spaces or whatever).
;; The return keyword do make indentation lose a level...
(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 (= (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)
(should (eq (car (python-indent-context)) :after-comment))
(should (= (python-indent-calculate-indentation) 4))
(python-tests-look-at "now_we_do_mess")
;; Here is where comment indentation starts to get ignored and
;; where the user can't freely indent anymore.
(should (eq (car (python-indent-context)) :after-block-end))
(should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "comment_wins_over_ender")
;; The comment won over the ender because the user said so.
(should (eq (car (python-indent-context)) :after-comment))
(should (= (python-indent-calculate-indentation) 4))
;; 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.")
(should (eq (car (python-indent-context)) :after-line))
(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 ()
"The most simple inside-paren case that shouldn't fail."
(python-tests-with-temp-buffer