diff options
| author | Fabián Ezequiel Gallina | 2013-12-12 02:37:09 -0300 |
|---|---|---|
| committer | Fabián Ezequiel Gallina | 2013-12-12 02:37:09 -0300 |
| commit | 09faee72dd0743a5b46444b5e917ee1259843788 (patch) | |
| tree | 13f65f15d978b036b9fca79cd3803220f1fcf1dd | |
| parent | 139f528442726be5160120fb13e68240982de92f (diff) | |
| download | emacs-09faee72dd0743a5b46444b5e917ee1259843788.tar.gz emacs-09faee72dd0743a5b46444b5e917ee1259843788.zip | |
* lisp/progmodes/python.el (python-indent-context)
(python-indent-calculate-indentation): Fix auto-identation
behavior for comment blocks.
* test/automated/python-tests.el (python-indent-after-comment-1)
(python-indent-after-comment-2): New tests.
Fixes: debbugs:15916
| -rw-r--r-- | lisp/ChangeLog | 6 | ||||
| -rw-r--r-- | lisp/progmodes/python.el | 16 | ||||
| -rw-r--r-- | test/ChangeLog | 5 | ||||
| -rw-r--r-- | test/automated/python-tests.el | 77 |
4 files changed, 104 insertions, 0 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index fc66ed607d4..52d4eff3eb6 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,9 @@ | |||
| 1 | 2013-12-12 Fabián Ezequiel Gallina <fgallina@gnu.org> | ||
| 2 | |||
| 3 | * progmodes/python.el (python-indent-context) | ||
| 4 | (python-indent-calculate-indentation): Fix auto-identation | ||
| 5 | behavior for comment blocks. (Bug#15916) | ||
| 6 | |||
| 1 | 2013-12-12 Nathan Trapuzzano <nbtrap@nbtrap.com> (tiny change) | 7 | 2013-12-12 Nathan Trapuzzano <nbtrap@nbtrap.com> (tiny change) |
| 2 | 8 | ||
| 3 | * progmodes/python.el (python-indent-calculate-indentation): When | 9 | * progmodes/python.el (python-indent-calculate-indentation): When |
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 669da135644..33039a4d087 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -686,6 +686,8 @@ Context information is returned with a cons with the form: | |||
| 686 | \(STATUS . START) | 686 | \(STATUS . START) |
| 687 | 687 | ||
| 688 | Where status can be any of the following symbols: | 688 | Where status can be any of the following symbols: |
| 689 | |||
| 690 | * after-comment: When current line might continue a comment block | ||
| 689 | * inside-paren: If point in between (), {} or [] | 691 | * inside-paren: If point in between (), {} or [] |
| 690 | * inside-string: If point is inside a string | 692 | * inside-string: If point is inside a string |
| 691 | * after-backslash: Previous line ends in a backslash | 693 | * after-backslash: Previous line ends in a backslash |
| @@ -704,6 +706,17 @@ START is the buffer position where the sexp starts." | |||
| 704 | (goto-char (line-beginning-position)) | 706 | (goto-char (line-beginning-position)) |
| 705 | (bobp)) | 707 | (bobp)) |
| 706 | 'no-indent) | 708 | 'no-indent) |
| 709 | ;; Comment continuation | ||
| 710 | ((save-excursion | ||
| 711 | (when (and | ||
| 712 | (or | ||
| 713 | (python-info-current-line-comment-p) | ||
| 714 | (python-info-current-line-empty-p)) | ||
| 715 | (progn | ||
| 716 | (forward-comment -1) | ||
| 717 | (python-info-current-line-comment-p))) | ||
| 718 | (setq start (point)) | ||
| 719 | 'after-comment))) | ||
| 707 | ;; Inside string | 720 | ;; Inside string |
| 708 | ((setq start (python-syntax-context 'string ppss)) | 721 | ((setq start (python-syntax-context 'string ppss)) |
| 709 | 'inside-string) | 722 | 'inside-string) |
| @@ -755,6 +768,9 @@ START is the buffer position where the sexp starts." | |||
| 755 | (save-excursion | 768 | (save-excursion |
| 756 | (pcase context-status | 769 | (pcase context-status |
| 757 | (`no-indent 0) | 770 | (`no-indent 0) |
| 771 | (`after-comment | ||
| 772 | (goto-char context-start) | ||
| 773 | (current-indentation)) | ||
| 758 | ;; When point is after beginning of block just add one level | 774 | ;; When point is after beginning of block just add one level |
| 759 | ;; of indentation relative to the context-start | 775 | ;; of indentation relative to the context-start |
| 760 | (`after-beginning-of-block | 776 | (`after-beginning-of-block |
diff --git a/test/ChangeLog b/test/ChangeLog index ce7208db229..790725b6342 100644 --- a/test/ChangeLog +++ b/test/ChangeLog | |||
| @@ -1,3 +1,8 @@ | |||
| 1 | 2013-12-12 Fabián Ezequiel Gallina <fgallina@gnu.org> | ||
| 2 | |||
| 3 | * automated/python-tests.el (python-indent-after-comment-1) | ||
| 4 | (python-indent-after-comment-2): New tests. | ||
| 5 | |||
| 1 | 2013-12-12 Nathan Trapuzzano <nbtrap@nbtrap.com> | 6 | 2013-12-12 Nathan Trapuzzano <nbtrap@nbtrap.com> |
| 2 | 7 | ||
| 3 | * automated/python-test.el (python-indent-block-enders-1): Rename | 8 | * automated/python-test.el (python-indent-block-enders-1): Rename |
diff --git a/test/automated/python-tests.el b/test/automated/python-tests.el index 58a839a5500..84be598b6dd 100644 --- a/test/automated/python-tests.el +++ b/test/automated/python-tests.el | |||
| @@ -199,6 +199,83 @@ foo = long_function_name( | |||
| 199 | (should (eq (car (python-indent-context)) 'inside-paren)) | 199 | (should (eq (car (python-indent-context)) 'inside-paren)) |
| 200 | (should (= (python-indent-calculate-indentation) 4)))) | 200 | (should (= (python-indent-calculate-indentation) 4)))) |
| 201 | 201 | ||
| 202 | (ert-deftest python-indent-after-comment-1 () | ||
| 203 | "The most simple after-comment case that shouldn't fail." | ||
| 204 | (python-tests-with-temp-buffer | ||
| 205 | "# Contents will be modified to correct indentation | ||
| 206 | class Blag(object): | ||
| 207 | def _on_child_complete(self, child_future): | ||
| 208 | if self.in_terminal_state(): | ||
| 209 | pass | ||
| 210 | # We only complete when all our async children have entered a | ||
| 211 | # terminal state. At that point, if any child failed, we fail | ||
| 212 | # with the exception with which the first child failed. | ||
| 213 | " | ||
| 214 | (python-tests-look-at "# We only complete") | ||
| 215 | (should (eq (car (python-indent-context)) 'after-line)) | ||
| 216 | (should (= (python-indent-calculate-indentation) 8)) | ||
| 217 | (python-tests-look-at "# terminal state") | ||
| 218 | (should (eq (car (python-indent-context)) 'after-comment)) | ||
| 219 | (should (= (python-indent-calculate-indentation) 8)) | ||
| 220 | (python-tests-look-at "# with the exception") | ||
| 221 | (should (eq (car (python-indent-context)) 'after-comment)) | ||
| 222 | ;; This one indents relative to previous block, even given the fact | ||
| 223 | ;; that it was under-indented. | ||
| 224 | (should (= (python-indent-calculate-indentation) 4)) | ||
| 225 | (python-tests-look-at "# terminal state" -1) | ||
| 226 | ;; It doesn't hurt to check again. | ||
| 227 | (should (eq (car (python-indent-context)) 'after-comment)) | ||
| 228 | (python-indent-line) | ||
| 229 | (should (= (current-indentation) 8)) | ||
| 230 | (python-tests-look-at "# with the exception") | ||
| 231 | (should (eq (car (python-indent-context)) 'after-comment)) | ||
| 232 | ;; Now everything should be lined up. | ||
| 233 | (should (= (python-indent-calculate-indentation) 8)))) | ||
| 234 | |||
| 235 | (ert-deftest python-indent-after-comment-2 () | ||
| 236 | "Test after-comment in weird cases." | ||
| 237 | (python-tests-with-temp-buffer | ||
| 238 | "# Contents will be modified to correct indentation | ||
| 239 | def func(arg): | ||
| 240 | # I don't do much | ||
| 241 | return arg | ||
| 242 | # This comment is badly indented just because. | ||
| 243 | # But we won't mess with the user in this line. | ||
| 244 | |||
| 245 | now_we_do_mess_cause_this_is_not_a_comment = 1 | ||
| 246 | |||
| 247 | # yeah, that. | ||
| 248 | " | ||
| 249 | (python-tests-look-at "# I don't do much") | ||
| 250 | (should (eq (car (python-indent-context)) 'after-beginning-of-block)) | ||
| 251 | (should (= (python-indent-calculate-indentation) 4)) | ||
| 252 | (python-tests-look-at "return arg") | ||
| 253 | ;; Comment here just gets ignored, this line is not a comment so | ||
| 254 | ;; the rules won't apply here. | ||
| 255 | (should (eq (car (python-indent-context)) 'after-beginning-of-block)) | ||
| 256 | (should (= (python-indent-calculate-indentation) 4)) | ||
| 257 | (python-tests-look-at "# This comment is badly") | ||
| 258 | (should (eq (car (python-indent-context)) 'after-line)) | ||
| 259 | ;; The return keyword moves indentation backwards 4 spaces, but | ||
| 260 | ;; let's assume this comment was placed there because the user | ||
| 261 | ;; wanted to (manually adding spaces or whatever). | ||
| 262 | (should (= (python-indent-calculate-indentation) 0)) | ||
| 263 | (python-tests-look-at "# but we won't mess") | ||
| 264 | (should (eq (car (python-indent-context)) 'after-comment)) | ||
| 265 | (should (= (python-indent-calculate-indentation) 4)) | ||
| 266 | ;; Behave the same for blank lines: potentially a comment. | ||
| 267 | (forward-line 1) | ||
| 268 | (should (eq (car (python-indent-context)) 'after-comment)) | ||
| 269 | (should (= (python-indent-calculate-indentation) 4)) | ||
| 270 | (python-tests-look-at "now_we_do_mess") | ||
| 271 | ;; Here is where comment indentation starts to get ignored and | ||
| 272 | ;; where the user can't freely indent anymore. | ||
| 273 | (should (eq (car (python-indent-context)) 'after-line)) | ||
| 274 | (should (= (python-indent-calculate-indentation) 0)) | ||
| 275 | (python-tests-look-at "# yeah, that.") | ||
| 276 | (should (eq (car (python-indent-context)) 'after-line)) | ||
| 277 | (should (= (python-indent-calculate-indentation) 0)))) | ||
| 278 | |||
| 202 | (ert-deftest python-indent-inside-paren-1 () | 279 | (ert-deftest python-indent-inside-paren-1 () |
| 203 | "The most simple inside-paren case that shouldn't fail." | 280 | "The most simple inside-paren case that shouldn't fail." |
| 204 | (python-tests-with-temp-buffer | 281 | (python-tests-with-temp-buffer |