diff options
| author | Fabián Ezequiel Gallina | 2015-01-30 00:19:55 -0300 |
|---|---|---|
| committer | Fabián Ezequiel Gallina | 2015-01-30 00:19:55 -0300 |
| commit | 41c3b9241cd78a1eaeb159572b6f4e546b1f8d7b (patch) | |
| tree | 1a1f80fc9e095acaf9ae2ffa969ddddb17ade7f9 | |
| parent | 868df451530c294cff3d4ccb98873626aa8105df (diff) | |
| download | emacs-41c3b9241cd78a1eaeb159572b6f4e546b1f8d7b.tar.gz emacs-41c3b9241cd78a1eaeb159572b6f4e546b1f8d7b.zip | |
* 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.
| -rw-r--r-- | lisp/ChangeLog | 5 | ||||
| -rw-r--r-- | lisp/progmodes/python.el | 40 | ||||
| -rw-r--r-- | test/ChangeLog | 7 | ||||
| -rw-r--r-- | 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 @@ | |||
| 1 | 2015-01-30 Fabián Ezequiel Gallina <fgallina@gnu.org> | ||
| 2 | |||
| 3 | * progmodes/python.el (python-indent-context): Respect user | ||
| 4 | indentation after comment. | ||
| 5 | |||
| 1 | 2015-01-29 Tassilo Horn <tsdh@gnu.org> | 6 | 2015-01-29 Tassilo Horn <tsdh@gnu.org> |
| 2 | 7 | ||
| 3 | * textmodes/reftex-vars.el (featurep): Conditionalize value of | 8 | * 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 | |||
| 810 | ;; Beginning of buffer. | 810 | ;; Beginning of buffer. |
| 811 | ((= (line-number-at-pos) 1) | 811 | ((= (line-number-at-pos) 1) |
| 812 | (cons :no-indent 0)) | 812 | (cons :no-indent 0)) |
| 813 | ;; Comment continuation (maybe). | ||
| 814 | ((save-excursion | ||
| 815 | (when (and | ||
| 816 | (or | ||
| 817 | (python-info-current-line-comment-p) | ||
| 818 | (python-info-current-line-empty-p)) | ||
| 819 | (forward-comment -1) | ||
| 820 | (python-info-current-line-comment-p)) | ||
| 821 | (cons :after-comment (point))))) | ||
| 822 | ;; Inside a string. | 813 | ;; Inside a string. |
| 823 | ((let ((start (python-syntax-context 'string ppss))) | 814 | ((let ((start (python-syntax-context 'string ppss))) |
| 824 | (when start | 815 | (when start |
| @@ -930,21 +921,22 @@ keyword | |||
| 930 | ((let ((start (python-info-dedenter-statement-p))) | 921 | ((let ((start (python-info-dedenter-statement-p))) |
| 931 | (when start | 922 | (when start |
| 932 | (cons :at-dedenter-block-start start)))) | 923 | (cons :at-dedenter-block-start start)))) |
| 933 | ;; After normal line. | 924 | ;; After normal line, comment or ender (default case). |
| 934 | ((let ((start (save-excursion | 925 | ((save-excursion |
| 935 | (back-to-indentation) | 926 | (back-to-indentation) |
| 936 | (skip-chars-backward " \t\n") | 927 | (skip-chars-backward " \t\n") |
| 937 | (python-nav-beginning-of-statement) | 928 | (python-nav-beginning-of-statement) |
| 938 | (point)))) | 929 | (cons |
| 939 | (when start | 930 | (cond ((python-info-current-line-comment-p) |
| 940 | (if (save-excursion | 931 | :after-comment) |
| 941 | (python-util-forward-comment -1) | 932 | ((save-excursion |
| 942 | (python-nav-beginning-of-statement) | 933 | (goto-char (line-end-position)) |
| 943 | (looking-at (python-rx block-ender))) | 934 | (python-util-forward-comment -1) |
| 944 | (cons :after-block-end start) | 935 | (python-nav-beginning-of-statement) |
| 945 | (cons :after-line start))))) | 936 | (looking-at (python-rx block-ender))) |
| 946 | ;; Default case: do not indent. | 937 | :after-block-end) |
| 947 | (t (cons :no-indent 0)))))) | 938 | (t :after-line)) |
| 939 | (point)))))))) | ||
| 948 | 940 | ||
| 949 | (defun python-indent--calculate-indentation () | 941 | (defun python-indent--calculate-indentation () |
| 950 | "Internal implementation of `python-indent-calculate-indentation'. | 942 | "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 @@ | |||
| 1 | 2015-01-30 Fabián Ezequiel Gallina <fgallina@gnu.org> | ||
| 2 | |||
| 3 | * automated/python-tests.el (python-indent-pep8-1) | ||
| 4 | (python-indent-pep8-2, python-indent-pep8-3) | ||
| 5 | (python-indent-after-comment-2): Fix tests. | ||
| 6 | (python-indent-after-comment-3): New test. | ||
| 7 | |||
| 1 | 2015-01-26 Fabián Ezequiel Gallina <fgallina@gnu.org> | 8 | 2015-01-26 Fabián Ezequiel Gallina <fgallina@gnu.org> |
| 2 | 9 | ||
| 3 | * automated/python-tests.el (python-indent-pep8-1) | 10 | * 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, | |||
| 177 | (should (eq (car (python-indent-context)) :no-indent)) | 177 | (should (eq (car (python-indent-context)) :no-indent)) |
| 178 | (should (= (python-indent-calculate-indentation) 0)) | 178 | (should (= (python-indent-calculate-indentation) 0)) |
| 179 | (python-tests-look-at "foo = long_function_name(var_one, var_two,") | 179 | (python-tests-look-at "foo = long_function_name(var_one, var_two,") |
| 180 | (should (eq (car (python-indent-context)) :after-line)) | 180 | (should (eq (car (python-indent-context)) :after-comment)) |
| 181 | (should (= (python-indent-calculate-indentation) 0)) | 181 | (should (= (python-indent-calculate-indentation) 0)) |
| 182 | (python-tests-look-at "var_three, var_four)") | 182 | (python-tests-look-at "var_three, var_four)") |
| 183 | (should (eq (car (python-indent-context)) :inside-paren)) | 183 | (should (eq (car (python-indent-context)) :inside-paren)) |
| @@ -195,7 +195,7 @@ def long_function_name( | |||
| 195 | (should (eq (car (python-indent-context)) :no-indent)) | 195 | (should (eq (car (python-indent-context)) :no-indent)) |
| 196 | (should (= (python-indent-calculate-indentation) 0)) | 196 | (should (= (python-indent-calculate-indentation) 0)) |
| 197 | (python-tests-look-at "def long_function_name(") | 197 | (python-tests-look-at "def long_function_name(") |
| 198 | (should (eq (car (python-indent-context)) :after-line)) | 198 | (should (eq (car (python-indent-context)) :after-comment)) |
| 199 | (should (= (python-indent-calculate-indentation) 0)) | 199 | (should (= (python-indent-calculate-indentation) 0)) |
| 200 | (python-tests-look-at "var_one, var_two, var_three,") | 200 | (python-tests-look-at "var_one, var_two, var_three,") |
| 201 | (should (eq (car (python-indent-context)) | 201 | (should (eq (car (python-indent-context)) |
| @@ -221,7 +221,7 @@ foo = long_function_name( | |||
| 221 | (should (eq (car (python-indent-context)) :no-indent)) | 221 | (should (eq (car (python-indent-context)) :no-indent)) |
| 222 | (should (= (python-indent-calculate-indentation) 0)) | 222 | (should (= (python-indent-calculate-indentation) 0)) |
| 223 | (python-tests-look-at "foo = long_function_name(") | 223 | (python-tests-look-at "foo = long_function_name(") |
| 224 | (should (eq (car (python-indent-context)) :after-line)) | 224 | (should (eq (car (python-indent-context)) :after-comment)) |
| 225 | (should (= (python-indent-calculate-indentation) 0)) | 225 | (should (= (python-indent-calculate-indentation) 0)) |
| 226 | (python-tests-look-at "var_one, var_two,") | 226 | (python-tests-look-at "var_one, var_two,") |
| 227 | (should (eq (car (python-indent-context)) :inside-paren-newline-start)) | 227 | (should (eq (car (python-indent-context)) :inside-paren-newline-start)) |
| @@ -286,10 +286,10 @@ class Blag(object): | |||
| 286 | def func(arg): | 286 | def func(arg): |
| 287 | # I don't do much | 287 | # I don't do much |
| 288 | return arg | 288 | return arg |
| 289 | # This comment is badly indented just because. | 289 | # This comment is badly indented because the user forced so. |
| 290 | # But we won't mess with the user in this line. | 290 | # At this line python.el wont dedent, user is always right. |
| 291 | 291 | ||
| 292 | now_we_do_mess_cause_this_is_not_a_comment = 1 | 292 | comment_wins_over_ender = True |
| 293 | 293 | ||
| 294 | # yeah, that. | 294 | # yeah, that. |
| 295 | " | 295 | " |
| @@ -301,28 +301,49 @@ now_we_do_mess_cause_this_is_not_a_comment = 1 | |||
| 301 | ;; the rules won't apply here. | 301 | ;; the rules won't apply here. |
| 302 | (should (eq (car (python-indent-context)) :after-block-start)) | 302 | (should (eq (car (python-indent-context)) :after-block-start)) |
| 303 | (should (= (python-indent-calculate-indentation) 4)) | 303 | (should (= (python-indent-calculate-indentation) 4)) |
| 304 | (python-tests-look-at "# This comment is badly") | 304 | (python-tests-look-at "# This comment is badly indented") |
| 305 | (should (eq (car (python-indent-context)) :after-block-end)) | 305 | (should (eq (car (python-indent-context)) :after-block-end)) |
| 306 | ;; The return keyword moves indentation backwards 4 spaces, but | 306 | ;; The return keyword do make indentation lose a level... |
| 307 | ;; let's assume this comment was placed there because the user | ||
| 308 | ;; wanted to (manually adding spaces or whatever). | ||
| 309 | (should (= (python-indent-calculate-indentation) 0)) | 307 | (should (= (python-indent-calculate-indentation) 0)) |
| 310 | (python-tests-look-at "# but we won't mess") | 308 | ;; ...but the current indentation was forced by the user. |
| 309 | (python-tests-look-at "# At this line python.el wont dedent") | ||
| 311 | (should (eq (car (python-indent-context)) :after-comment)) | 310 | (should (eq (car (python-indent-context)) :after-comment)) |
| 312 | (should (= (python-indent-calculate-indentation) 4)) | 311 | (should (= (python-indent-calculate-indentation) 4)) |
| 313 | ;; Behave the same for blank lines: potentially a comment. | 312 | ;; Should behave the same for blank lines: potentially a comment. |
| 314 | (forward-line 1) | 313 | (forward-line 1) |
| 315 | (should (eq (car (python-indent-context)) :after-comment)) | 314 | (should (eq (car (python-indent-context)) :after-comment)) |
| 316 | (should (= (python-indent-calculate-indentation) 4)) | 315 | (should (= (python-indent-calculate-indentation) 4)) |
| 317 | (python-tests-look-at "now_we_do_mess") | 316 | (python-tests-look-at "comment_wins_over_ender") |
| 318 | ;; Here is where comment indentation starts to get ignored and | 317 | ;; The comment won over the ender because the user said so. |
| 319 | ;; where the user can't freely indent anymore. | 318 | (should (eq (car (python-indent-context)) :after-comment)) |
| 320 | (should (eq (car (python-indent-context)) :after-block-end)) | 319 | (should (= (python-indent-calculate-indentation) 4)) |
| 321 | (should (= (python-indent-calculate-indentation) 0)) | 320 | ;; The indentation calculated fine for the assignment, but the user |
| 321 | ;; choose to force it back to the first column. Next line should | ||
| 322 | ;; be aware of that. | ||
| 322 | (python-tests-look-at "# yeah, that.") | 323 | (python-tests-look-at "# yeah, that.") |
| 323 | (should (eq (car (python-indent-context)) :after-line)) | 324 | (should (eq (car (python-indent-context)) :after-line)) |
| 324 | (should (= (python-indent-calculate-indentation) 0)))) | 325 | (should (= (python-indent-calculate-indentation) 0)))) |
| 325 | 326 | ||
| 327 | (ert-deftest python-indent-after-comment-3 () | ||
| 328 | "Test after-comment in buggy case." | ||
| 329 | (python-tests-with-temp-buffer | ||
| 330 | " | ||
| 331 | class A(object): | ||
| 332 | |||
| 333 | def something(self, arg): | ||
| 334 | if True: | ||
| 335 | return arg | ||
| 336 | |||
| 337 | # A comment | ||
| 338 | |||
| 339 | @adecorator | ||
| 340 | def method(self, a, b): | ||
| 341 | pass | ||
| 342 | " | ||
| 343 | (python-tests-look-at "@adecorator") | ||
| 344 | (should (eq (car (python-indent-context)) :after-comment)) | ||
| 345 | (should (= (python-indent-calculate-indentation) 4)))) | ||
| 346 | |||
| 326 | (ert-deftest python-indent-inside-paren-1 () | 347 | (ert-deftest python-indent-inside-paren-1 () |
| 327 | "The most simple inside-paren case that shouldn't fail." | 348 | "The most simple inside-paren case that shouldn't fail." |
| 328 | (python-tests-with-temp-buffer | 349 | (python-tests-with-temp-buffer |