(interactive)
(save-excursion
(save-restriction
- (widen)
+ (prog-widen)
(goto-char (point-min))
(let ((block-end))
(while (and (not block-end)
- Point is on a line starting a dedenter block.
- START is the position where the dedenter block starts."
(save-restriction
- (widen)
+ (prog-widen)
(let ((ppss (save-excursion
(beginning-of-line)
(syntax-ppss))))
((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))))))))
+ (if (bobp)
+ (cons :no-indent 0)
+ (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'.
happening for :at-dedenter-block-start context since the
possibilities can be narrowed to specific indentation points."
(save-restriction
- (widen)
+ (prog-widen)
(save-excursion
(pcase (python-indent-context)
- (`(:no-indent . ,_) 0)
+ (`(:no-indent . ,_) (prog-first-column)) ; usually 0
(`(,(or :after-line
:after-comment
:inside-string
(let ((opening-block-start-points
(python-info-dedenter-opening-block-positions)))
(if (not opening-block-start-points)
- 0 ; if not found default to first column
+ (prog-first-column) ; if not found default to first column
(mapcar (lambda (pos)
(save-excursion
(goto-char pos)
case INDENTATION is a list, this order is enforced."
(if (listp indentation)
(sort (copy-sequence indentation) #'<)
- (let* ((remainder (% indentation python-indent-offset))
- (steps (/ (- indentation remainder) python-indent-offset))
- (levels (mapcar (lambda (step)
- (* python-indent-offset step))
- (number-sequence steps 0 -1))))
- (reverse
- (if (not (zerop remainder))
- (cons indentation levels)
- levels)))))
+ (nconc (number-sequence (prog-first-column) (1- indentation)
+ python-indent-offset)
+ (list indentation))))
(defun python-indent--previous-level (levels indentation)
"Return previous level from LEVELS relative to INDENTATION."
(python-indent--previous-level levels (current-indentation))
(if levels
(apply #'max levels)
- 0))))
+ (prog-first-column)))))
(defun python-indent-line (&optional previous)
"Internal implementation of `python-indent-line-function'.
This function can be used as the value of `add-log-current-defun-function'
since it returns nil if point is not inside a defun."
(save-restriction
- (widen)
+ (prog-widen)
(save-excursion
(end-of-line 1)
(let ((names)
(let ((point (python-info-dedenter-opening-block-position)))
(when point
(save-restriction
- (widen)
+ (prog-widen)
(message "Closes %s" (save-excursion
(goto-char point)
(buffer-substring
With optional argument LINE-NUMBER, check that line instead."
(save-excursion
(save-restriction
- (widen)
+ (prog-widen)
(when line-number
(python-util-goto-line line-number))
(while (and (not (eobp))
Optional argument LINE-NUMBER forces the line number to check against."
(save-excursion
(save-restriction
- (widen)
+ (prog-widen)
(when line-number
(python-util-goto-line line-number))
(when (python-info-line-ends-backslash-p)
where the continued line ends."
(save-excursion
(save-restriction
- (widen)
+ (prog-widen)
(let* ((context-type (progn
(back-to-indentation)
(python-syntax-context-type)))
(should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(forward-line 1)
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(forward-line 1)
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))))
(ert-deftest python-indent-after-comment-1 ()
}
"
(python-tests-look-at "data = {")
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "'key':")
(should (eq (car (python-indent-context)) :inside-paren-newline-start))
}}
"
(python-tests-look-at "data = {")
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "'objlist': [")
(should (eq (car (python-indent-context)) :inside-paren-newline-start))
'tokens')
"
(python-tests-look-at "data = ('these',")
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(forward-line 1)
(should (eq (car (python-indent-context)) :inside-paren))
['the', 'tokens' ] ]
"
(python-tests-look-at "data = [ [ 'these', 'are'],")
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(forward-line 1)
(should (eq (car (python-indent-context)) :inside-paren))
with_some_arg)
"
(python-tests-look-at "while ((not some_condition) and")
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(forward-line 1)
(should (eq (car (python-indent-context)) :inside-paren))
('more', 'choices'))
"
(python-tests-look-at "CHOICES = (('some', 'choice'),")
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(forward-line 1)
(should (eq (car (python-indent-context)) :inside-paren))
something_4, something_5
"
(python-tests-look-at "from foo.bar.baz import something, something_1")
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "something_2 something_3,")
(should (eq (car (python-indent-context)) :after-backslash-first-line))
.values_list()
"
(python-tests-look-at "objects = Thing.objects.all()")
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at ".filter(")
(should (eq (car (python-indent-context))
"
(python-tests-look-at
"with open('/path/to/some/file/you/want/to/read') as file_1, \\\\")
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at
"open('/path/to/some/file/being/written', 'w') as file_2")
"
(python-tests-look-at
"super_awful_assignment = some_calculation() and \\\\")
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "another_calculation() and \\\\")
(should (eq (car (python-indent-context))
'''
"
(python-tests-look-at "multiline = '''")
- (should (eq (car (python-indent-context)) :after-line))
+ (should (eq (car (python-indent-context)) :no-indent))
(should (= (python-indent-calculate-indentation) 0))
(python-tests-look-at "bunch")
(should (eq (car (python-indent-context)) :inside-string))