From 9c2cbfa49db96eae95bb40c5fc3ce7f09781a97d Mon Sep 17 00:00:00 2001 From: kobarity Date: Sun, 18 Jun 2023 23:47:25 +0900 Subject: Fix Python indentation of continuation lines within parens * lisp/progmodes/python.el (python-indent-context): Add a new indent context `:inside-paren-continuation-line'. (python-indent--calculate-indentation): Use the new indent context. * test/lisp/progmodes/python-tests.el (python-indent-pep8-2) (python-indent-pep8-3) (python-indent-inside-paren-1) (python-indent-inside-paren-2) (python-indent-inside-paren-3) (python-indent-inside-paren-6) (python-indent-after-backslash-2): Change to use the new indent context. (python-indent-inside-paren-8) (python-indent-inside-paren-9): New tests. (Bug#63959) --- lisp/progmodes/python.el | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'lisp/progmodes/python.el') diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 26dafde7591..50d712ebb0c 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -1406,6 +1406,10 @@ keyword - Point is inside a paren from a block start followed by some items on the same line. - START is the first non space char position *after* the open paren. +:inside-paren-continuation-line + - Point is on a continuation line inside a paren. + - START is the position where the previous line (excluding lines + for inner parens) starts. :after-backslash - Fallback case when point is after backslash. @@ -1460,7 +1464,21 @@ keyword (= (line-number-at-pos) (progn (python-util-forward-comment) - (line-number-at-pos)))))))) + (line-number-at-pos))))))) + (continuation-start + (when start + (save-excursion + (forward-line -1) + (back-to-indentation) + ;; Skip inner parens. + (cl-loop with prev-start = (python-syntax-context 'paren) + while (and prev-start (>= prev-start start)) + if (= prev-start start) + return (point) + else do (goto-char prev-start) + (back-to-indentation) + (setq prev-start + (python-syntax-context 'paren))))))) (when start (cond ;; Current line only holds the closing paren. @@ -1476,6 +1494,9 @@ keyword (back-to-indentation) (python-syntax-closing-paren-p)) (cons :inside-paren-at-closing-nested-paren start)) + ;; This line is a continuation of the previous line. + (continuation-start + (cons :inside-paren-continuation-line continuation-start)) ;; This line starts from an opening block in its own line. ((save-excursion (goto-char start) @@ -1591,7 +1612,8 @@ possibilities can be narrowed to specific indentation points." (`(,(or :after-line :after-comment :inside-string - :after-backslash) . ,start) + :after-backslash + :inside-paren-continuation-line) . ,start) ;; Copy previous indentation. (goto-char start) (current-indentation)) -- cgit v1.2.1