;; 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
((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'.
(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))
(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))
(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))
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.
"
;; 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