diff options
| -rw-r--r-- | lisp/progmodes/python.el | 60 | ||||
| -rw-r--r-- | test/automated/python-tests.el | 26 |
2 files changed, 41 insertions, 45 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index d45d082c40a..f641880428c 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -734,7 +734,7 @@ work on `python-indent-calculate-indentation' instead." | |||
| 734 | (interactive) | 734 | (interactive) |
| 735 | (save-excursion | 735 | (save-excursion |
| 736 | (save-restriction | 736 | (save-restriction |
| 737 | (widen) | 737 | (prog-widen) |
| 738 | (goto-char (point-min)) | 738 | (goto-char (point-min)) |
| 739 | (let ((block-end)) | 739 | (let ((block-end)) |
| 740 | (while (and (not block-end) | 740 | (while (and (not block-end) |
| @@ -833,7 +833,7 @@ keyword | |||
| 833 | - Point is on a line starting a dedenter block. | 833 | - Point is on a line starting a dedenter block. |
| 834 | - START is the position where the dedenter block starts." | 834 | - START is the position where the dedenter block starts." |
| 835 | (save-restriction | 835 | (save-restriction |
| 836 | (widen) | 836 | (prog-widen) |
| 837 | (let ((ppss (save-excursion | 837 | (let ((ppss (save-excursion |
| 838 | (beginning-of-line) | 838 | (beginning-of-line) |
| 839 | (syntax-ppss)))) | 839 | (syntax-ppss)))) |
| @@ -958,18 +958,20 @@ keyword | |||
| 958 | ((save-excursion | 958 | ((save-excursion |
| 959 | (back-to-indentation) | 959 | (back-to-indentation) |
| 960 | (skip-chars-backward " \t\n") | 960 | (skip-chars-backward " \t\n") |
| 961 | (python-nav-beginning-of-statement) | 961 | (if (bobp) |
| 962 | (cons | 962 | (cons :no-indent 0) |
| 963 | (cond ((python-info-current-line-comment-p) | 963 | (python-nav-beginning-of-statement) |
| 964 | :after-comment) | 964 | (cons |
| 965 | ((save-excursion | 965 | (cond ((python-info-current-line-comment-p) |
| 966 | (goto-char (line-end-position)) | 966 | :after-comment) |
| 967 | (python-util-forward-comment -1) | 967 | ((save-excursion |
| 968 | (python-nav-beginning-of-statement) | 968 | (goto-char (line-end-position)) |
| 969 | (looking-at (python-rx block-ender))) | 969 | (python-util-forward-comment -1) |
| 970 | :after-block-end) | 970 | (python-nav-beginning-of-statement) |
| 971 | (t :after-line)) | 971 | (looking-at (python-rx block-ender))) |
| 972 | (point)))))))) | 972 | :after-block-end) |
| 973 | (t :after-line)) | ||
| 974 | (point))))))))) | ||
| 973 | 975 | ||
| 974 | (defun python-indent--calculate-indentation () | 976 | (defun python-indent--calculate-indentation () |
| 975 | "Internal implementation of `python-indent-calculate-indentation'. | 977 | "Internal implementation of `python-indent-calculate-indentation'. |
| @@ -978,10 +980,10 @@ current context or a list of integers. The latter case is only | |||
| 978 | happening for :at-dedenter-block-start context since the | 980 | happening for :at-dedenter-block-start context since the |
| 979 | possibilities can be narrowed to specific indentation points." | 981 | possibilities can be narrowed to specific indentation points." |
| 980 | (save-restriction | 982 | (save-restriction |
| 981 | (widen) | 983 | (prog-widen) |
| 982 | (save-excursion | 984 | (save-excursion |
| 983 | (pcase (python-indent-context) | 985 | (pcase (python-indent-context) |
| 984 | (`(:no-indent . ,_) 0) | 986 | (`(:no-indent . ,_) (prog-first-column)) ; usually 0 |
| 985 | (`(,(or :after-line | 987 | (`(,(or :after-line |
| 986 | :after-comment | 988 | :after-comment |
| 987 | :inside-string | 989 | :inside-string |
| @@ -1019,7 +1021,7 @@ possibilities can be narrowed to specific indentation points." | |||
| 1019 | (let ((opening-block-start-points | 1021 | (let ((opening-block-start-points |
| 1020 | (python-info-dedenter-opening-block-positions))) | 1022 | (python-info-dedenter-opening-block-positions))) |
| 1021 | (if (not opening-block-start-points) | 1023 | (if (not opening-block-start-points) |
| 1022 | 0 ; if not found default to first column | 1024 | (prog-first-column) ; if not found default to first column |
| 1023 | (mapcar (lambda (pos) | 1025 | (mapcar (lambda (pos) |
| 1024 | (save-excursion | 1026 | (save-excursion |
| 1025 | (goto-char pos) | 1027 | (goto-char pos) |
| @@ -1037,15 +1039,9 @@ integers. Levels are returned in ascending order, and in the | |||
| 1037 | case INDENTATION is a list, this order is enforced." | 1039 | case INDENTATION is a list, this order is enforced." |
| 1038 | (if (listp indentation) | 1040 | (if (listp indentation) |
| 1039 | (sort (copy-sequence indentation) #'<) | 1041 | (sort (copy-sequence indentation) #'<) |
| 1040 | (let* ((remainder (% indentation python-indent-offset)) | 1042 | (nconc (number-sequence (prog-first-column) (1- indentation) |
| 1041 | (steps (/ (- indentation remainder) python-indent-offset)) | 1043 | python-indent-offset) |
| 1042 | (levels (mapcar (lambda (step) | 1044 | (list indentation)))) |
| 1043 | (* python-indent-offset step)) | ||
| 1044 | (number-sequence steps 0 -1)))) | ||
| 1045 | (reverse | ||
| 1046 | (if (not (zerop remainder)) | ||
| 1047 | (cons indentation levels) | ||
| 1048 | levels))))) | ||
| 1049 | 1045 | ||
| 1050 | (defun python-indent--previous-level (levels indentation) | 1046 | (defun python-indent--previous-level (levels indentation) |
| 1051 | "Return previous level from LEVELS relative to INDENTATION." | 1047 | "Return previous level from LEVELS relative to INDENTATION." |
| @@ -1068,7 +1064,7 @@ minimum." | |||
| 1068 | (python-indent--previous-level levels (current-indentation)) | 1064 | (python-indent--previous-level levels (current-indentation)) |
| 1069 | (if levels | 1065 | (if levels |
| 1070 | (apply #'max levels) | 1066 | (apply #'max levels) |
| 1071 | 0)))) | 1067 | (prog-first-column))))) |
| 1072 | 1068 | ||
| 1073 | (defun python-indent-line (&optional previous) | 1069 | (defun python-indent-line (&optional previous) |
| 1074 | "Internal implementation of `python-indent-line-function'. | 1070 | "Internal implementation of `python-indent-line-function'. |
| @@ -4230,7 +4226,7 @@ Optional argument INCLUDE-TYPE indicates to include the type of the defun. | |||
| 4230 | This function can be used as the value of `add-log-current-defun-function' | 4226 | This function can be used as the value of `add-log-current-defun-function' |
| 4231 | since it returns nil if point is not inside a defun." | 4227 | since it returns nil if point is not inside a defun." |
| 4232 | (save-restriction | 4228 | (save-restriction |
| 4233 | (widen) | 4229 | (prog-widen) |
| 4234 | (save-excursion | 4230 | (save-excursion |
| 4235 | (end-of-line 1) | 4231 | (end-of-line 1) |
| 4236 | (let ((names) | 4232 | (let ((names) |
| @@ -4413,7 +4409,7 @@ likely an invalid python file." | |||
| 4413 | (let ((point (python-info-dedenter-opening-block-position))) | 4409 | (let ((point (python-info-dedenter-opening-block-position))) |
| 4414 | (when point | 4410 | (when point |
| 4415 | (save-restriction | 4411 | (save-restriction |
| 4416 | (widen) | 4412 | (prog-widen) |
| 4417 | (message "Closes %s" (save-excursion | 4413 | (message "Closes %s" (save-excursion |
| 4418 | (goto-char point) | 4414 | (goto-char point) |
| 4419 | (buffer-substring | 4415 | (buffer-substring |
| @@ -4434,7 +4430,7 @@ statement." | |||
| 4434 | With optional argument LINE-NUMBER, check that line instead." | 4430 | With optional argument LINE-NUMBER, check that line instead." |
| 4435 | (save-excursion | 4431 | (save-excursion |
| 4436 | (save-restriction | 4432 | (save-restriction |
| 4437 | (widen) | 4433 | (prog-widen) |
| 4438 | (when line-number | 4434 | (when line-number |
| 4439 | (python-util-goto-line line-number)) | 4435 | (python-util-goto-line line-number)) |
| 4440 | (while (and (not (eobp)) | 4436 | (while (and (not (eobp)) |
| @@ -4450,7 +4446,7 @@ With optional argument LINE-NUMBER, check that line instead." | |||
| 4450 | Optional argument LINE-NUMBER forces the line number to check against." | 4446 | Optional argument LINE-NUMBER forces the line number to check against." |
| 4451 | (save-excursion | 4447 | (save-excursion |
| 4452 | (save-restriction | 4448 | (save-restriction |
| 4453 | (widen) | 4449 | (prog-widen) |
| 4454 | (when line-number | 4450 | (when line-number |
| 4455 | (python-util-goto-line line-number)) | 4451 | (python-util-goto-line line-number)) |
| 4456 | (when (python-info-line-ends-backslash-p) | 4452 | (when (python-info-line-ends-backslash-p) |
| @@ -4467,7 +4463,7 @@ When current line is continuation of another return the point | |||
| 4467 | where the continued line ends." | 4463 | where the continued line ends." |
| 4468 | (save-excursion | 4464 | (save-excursion |
| 4469 | (save-restriction | 4465 | (save-restriction |
| 4470 | (widen) | 4466 | (prog-widen) |
| 4471 | (let* ((context-type (progn | 4467 | (let* ((context-type (progn |
| 4472 | (back-to-indentation) | 4468 | (back-to-indentation) |
| 4473 | (python-syntax-context-type))) | 4469 | (python-syntax-context-type))) |
diff --git a/test/automated/python-tests.el b/test/automated/python-tests.el index ae4323ba8af..4585e7f9614 100644 --- a/test/automated/python-tests.el +++ b/test/automated/python-tests.el | |||
| @@ -267,10 +267,10 @@ foo = long_function_name( | |||
| 267 | (should (eq (car (python-indent-context)) :no-indent)) | 267 | (should (eq (car (python-indent-context)) :no-indent)) |
| 268 | (should (= (python-indent-calculate-indentation) 0)) | 268 | (should (= (python-indent-calculate-indentation) 0)) |
| 269 | (forward-line 1) | 269 | (forward-line 1) |
| 270 | (should (eq (car (python-indent-context)) :after-line)) | 270 | (should (eq (car (python-indent-context)) :no-indent)) |
| 271 | (should (= (python-indent-calculate-indentation) 0)) | 271 | (should (= (python-indent-calculate-indentation) 0)) |
| 272 | (forward-line 1) | 272 | (forward-line 1) |
| 273 | (should (eq (car (python-indent-context)) :after-line)) | 273 | (should (eq (car (python-indent-context)) :no-indent)) |
| 274 | (should (= (python-indent-calculate-indentation) 0)))) | 274 | (should (= (python-indent-calculate-indentation) 0)))) |
| 275 | 275 | ||
| 276 | (ert-deftest python-indent-after-comment-1 () | 276 | (ert-deftest python-indent-after-comment-1 () |
| @@ -392,7 +392,7 @@ data = { | |||
| 392 | } | 392 | } |
| 393 | " | 393 | " |
| 394 | (python-tests-look-at "data = {") | 394 | (python-tests-look-at "data = {") |
| 395 | (should (eq (car (python-indent-context)) :after-line)) | 395 | (should (eq (car (python-indent-context)) :no-indent)) |
| 396 | (should (= (python-indent-calculate-indentation) 0)) | 396 | (should (= (python-indent-calculate-indentation) 0)) |
| 397 | (python-tests-look-at "'key':") | 397 | (python-tests-look-at "'key':") |
| 398 | (should (eq (car (python-indent-context)) :inside-paren-newline-start)) | 398 | (should (eq (car (python-indent-context)) :inside-paren-newline-start)) |
| @@ -455,7 +455,7 @@ data = {'key': { | |||
| 455 | }} | 455 | }} |
| 456 | " | 456 | " |
| 457 | (python-tests-look-at "data = {") | 457 | (python-tests-look-at "data = {") |
| 458 | (should (eq (car (python-indent-context)) :after-line)) | 458 | (should (eq (car (python-indent-context)) :no-indent)) |
| 459 | (should (= (python-indent-calculate-indentation) 0)) | 459 | (should (= (python-indent-calculate-indentation) 0)) |
| 460 | (python-tests-look-at "'objlist': [") | 460 | (python-tests-look-at "'objlist': [") |
| 461 | (should (eq (car (python-indent-context)) :inside-paren-newline-start)) | 461 | (should (eq (car (python-indent-context)) :inside-paren-newline-start)) |
| @@ -494,7 +494,7 @@ data = ('these', | |||
| 494 | 'tokens') | 494 | 'tokens') |
| 495 | " | 495 | " |
| 496 | (python-tests-look-at "data = ('these',") | 496 | (python-tests-look-at "data = ('these',") |
| 497 | (should (eq (car (python-indent-context)) :after-line)) | 497 | (should (eq (car (python-indent-context)) :no-indent)) |
| 498 | (should (= (python-indent-calculate-indentation) 0)) | 498 | (should (= (python-indent-calculate-indentation) 0)) |
| 499 | (forward-line 1) | 499 | (forward-line 1) |
| 500 | (should (eq (car (python-indent-context)) :inside-paren)) | 500 | (should (eq (car (python-indent-context)) :inside-paren)) |
| @@ -514,7 +514,7 @@ data = [ [ 'these', 'are'], | |||
| 514 | ['the', 'tokens' ] ] | 514 | ['the', 'tokens' ] ] |
| 515 | " | 515 | " |
| 516 | (python-tests-look-at "data = [ [ 'these', 'are'],") | 516 | (python-tests-look-at "data = [ [ 'these', 'are'],") |
| 517 | (should (eq (car (python-indent-context)) :after-line)) | 517 | (should (eq (car (python-indent-context)) :no-indent)) |
| 518 | (should (= (python-indent-calculate-indentation) 0)) | 518 | (should (= (python-indent-calculate-indentation) 0)) |
| 519 | (forward-line 1) | 519 | (forward-line 1) |
| 520 | (should (eq (car (python-indent-context)) :inside-paren)) | 520 | (should (eq (car (python-indent-context)) :inside-paren)) |
| @@ -530,7 +530,7 @@ while ((not some_condition) and | |||
| 530 | with_some_arg) | 530 | with_some_arg) |
| 531 | " | 531 | " |
| 532 | (python-tests-look-at "while ((not some_condition) and") | 532 | (python-tests-look-at "while ((not some_condition) and") |
| 533 | (should (eq (car (python-indent-context)) :after-line)) | 533 | (should (eq (car (python-indent-context)) :no-indent)) |
| 534 | (should (= (python-indent-calculate-indentation) 0)) | 534 | (should (= (python-indent-calculate-indentation) 0)) |
| 535 | (forward-line 1) | 535 | (forward-line 1) |
| 536 | (should (eq (car (python-indent-context)) :inside-paren)) | 536 | (should (eq (car (python-indent-context)) :inside-paren)) |
| @@ -551,7 +551,7 @@ CHOICES = (('some', 'choice'), | |||
| 551 | ('more', 'choices')) | 551 | ('more', 'choices')) |
| 552 | " | 552 | " |
| 553 | (python-tests-look-at "CHOICES = (('some', 'choice'),") | 553 | (python-tests-look-at "CHOICES = (('some', 'choice'),") |
| 554 | (should (eq (car (python-indent-context)) :after-line)) | 554 | (should (eq (car (python-indent-context)) :no-indent)) |
| 555 | (should (= (python-indent-calculate-indentation) 0)) | 555 | (should (= (python-indent-calculate-indentation) 0)) |
| 556 | (forward-line 1) | 556 | (forward-line 1) |
| 557 | (should (eq (car (python-indent-context)) :inside-paren)) | 557 | (should (eq (car (python-indent-context)) :inside-paren)) |
| @@ -612,7 +612,7 @@ from foo.bar.baz import something, something_1 \\\\ | |||
| 612 | something_4, something_5 | 612 | something_4, something_5 |
| 613 | " | 613 | " |
| 614 | (python-tests-look-at "from foo.bar.baz import something, something_1") | 614 | (python-tests-look-at "from foo.bar.baz import something, something_1") |
| 615 | (should (eq (car (python-indent-context)) :after-line)) | 615 | (should (eq (car (python-indent-context)) :no-indent)) |
| 616 | (should (= (python-indent-calculate-indentation) 0)) | 616 | (should (= (python-indent-calculate-indentation) 0)) |
| 617 | (python-tests-look-at "something_2 something_3,") | 617 | (python-tests-look-at "something_2 something_3,") |
| 618 | (should (eq (car (python-indent-context)) :after-backslash-first-line)) | 618 | (should (eq (car (python-indent-context)) :after-backslash-first-line)) |
| @@ -639,7 +639,7 @@ objects = Thing.objects.all() \\\\ | |||
| 639 | .values_list() | 639 | .values_list() |
| 640 | " | 640 | " |
| 641 | (python-tests-look-at "objects = Thing.objects.all()") | 641 | (python-tests-look-at "objects = Thing.objects.all()") |
| 642 | (should (eq (car (python-indent-context)) :after-line)) | 642 | (should (eq (car (python-indent-context)) :no-indent)) |
| 643 | (should (= (python-indent-calculate-indentation) 0)) | 643 | (should (= (python-indent-calculate-indentation) 0)) |
| 644 | (python-tests-look-at ".filter(") | 644 | (python-tests-look-at ".filter(") |
| 645 | (should (eq (car (python-indent-context)) | 645 | (should (eq (car (python-indent-context)) |
| @@ -682,7 +682,7 @@ with open('/path/to/some/file/you/want/to/read') as file_1, \\\\ | |||
| 682 | " | 682 | " |
| 683 | (python-tests-look-at | 683 | (python-tests-look-at |
| 684 | "with open('/path/to/some/file/you/want/to/read') as file_1, \\\\") | 684 | "with open('/path/to/some/file/you/want/to/read') as file_1, \\\\") |
| 685 | (should (eq (car (python-indent-context)) :after-line)) | 685 | (should (eq (car (python-indent-context)) :no-indent)) |
| 686 | (should (= (python-indent-calculate-indentation) 0)) | 686 | (should (= (python-indent-calculate-indentation) 0)) |
| 687 | (python-tests-look-at | 687 | (python-tests-look-at |
| 688 | "open('/path/to/some/file/being/written', 'w') as file_2") | 688 | "open('/path/to/some/file/being/written', 'w') as file_2") |
| @@ -703,7 +703,7 @@ super_awful_assignment = some_calculation() and \\\\ | |||
| 703 | " | 703 | " |
| 704 | (python-tests-look-at | 704 | (python-tests-look-at |
| 705 | "super_awful_assignment = some_calculation() and \\\\") | 705 | "super_awful_assignment = some_calculation() and \\\\") |
| 706 | (should (eq (car (python-indent-context)) :after-line)) | 706 | (should (eq (car (python-indent-context)) :no-indent)) |
| 707 | (should (= (python-indent-calculate-indentation) 0)) | 707 | (should (= (python-indent-calculate-indentation) 0)) |
| 708 | (python-tests-look-at "another_calculation() and \\\\") | 708 | (python-tests-look-at "another_calculation() and \\\\") |
| 709 | (should (eq (car (python-indent-context)) | 709 | (should (eq (car (python-indent-context)) |
| @@ -992,7 +992,7 @@ lines | |||
| 992 | ''' | 992 | ''' |
| 993 | " | 993 | " |
| 994 | (python-tests-look-at "multiline = '''") | 994 | (python-tests-look-at "multiline = '''") |
| 995 | (should (eq (car (python-indent-context)) :after-line)) | 995 | (should (eq (car (python-indent-context)) :no-indent)) |
| 996 | (should (= (python-indent-calculate-indentation) 0)) | 996 | (should (= (python-indent-calculate-indentation) 0)) |
| 997 | (python-tests-look-at "bunch") | 997 | (python-tests-look-at "bunch") |
| 998 | (should (eq (car (python-indent-context)) :inside-string)) | 998 | (should (eq (car (python-indent-context)) :inside-string)) |