From 41c3b9241cd78a1eaeb159572b6f4e546b1f8d7b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fabi=C3=A1n=20Ezequiel=20Gallina?= Date: Fri, 30 Jan 2015 00:19:55 -0300 Subject: [PATCH] * 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. --- lisp/ChangeLog | 5 ++++ lisp/progmodes/python.el | 40 ++++++++++--------------- test/ChangeLog | 7 +++++ test/automated/python-tests.el | 55 +++++++++++++++++++++++----------- 4 files changed, 66 insertions(+), 41 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 0d0f7aa2f9f..20686014bf3 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,8 @@ +2015-01-30 Fabián Ezequiel Gallina + + * progmodes/python.el (python-indent-context): Respect user + indentation after comment. + 2015-01-29 Tassilo Horn * textmodes/reftex-vars.el (featurep): Conditionalize value of diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index d0a83087554..5842be7cf64 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -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'. diff --git a/test/ChangeLog b/test/ChangeLog index 62876e9ecae..72e1b854fd9 100644 --- a/test/ChangeLog +++ b/test/ChangeLog @@ -1,3 +1,10 @@ +2015-01-30 Fabián Ezequiel Gallina + + * 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 * automated/python-tests.el (python-indent-pep8-1) diff --git a/test/automated/python-tests.el b/test/automated/python-tests.el index 5bddfe845ed..4972731d0d2 100644 --- a/test/automated/python-tests.el +++ b/test/automated/python-tests.el @@ -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 -- 2.39.2