Subject: Re: bug#20742: 24.5; [PATCH] python.el: fix close paren indentation to match pep8

From: Noam Postavsky <[email-masked]>
Date: Sat, 20 Apr 2019 16:51:23 -0400

tags 20742 + patch
quit

>> When opening paren is followed by newline the closing paren should follow
>> the current indentation. Otherwise the closing paren should be aligned
>> with the opening paren. This fixes the latter case. #20742
>
> I'm having trouble applying this.
>
>     error: corrupt patch at line 11
>     Applying: python.el: fix close paren indentation to match pep8
>     Patch failed at 0001 python.el: fix close paren indentation to match pep8

I managed to get this applied; I've replaced the python-test.el
modifications, partly because they're written as updates to the existing
tests which makes them a bit harder to follow, and partly to make more
clear that the patch is below the 15 line copyright limit.

I'll push to master in a week or so if there are no further comments.

>From e0acfb7254f97f623286e65483535c1b6312f4ba Mon Sep 17 00:00:00 2001
From: Tommi Komulainen <[email-masked]>
Date: Fri, 19 Jun 2015 18:53:52 +0200
Subject: [PATCH 1/2] python.el: Fix close paren indentation to match pep8
 (Bug#20742)

* lisp/progmodes/python.el (python-indent--calculate-indentation):
When opening paren is followed by newline the closing paren should
follow the current indentation.  Otherwise the closing paren should be
aligned with the opening paren.  This fixes the latter case.

Copyright-paperwork-exempt: yes
---
 lisp/progmodes/python.el | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index eb3e31c4b7..188bc973d9 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1070,12 +1070,18 @@ (defun python-indent--calculate-indentation ()
         (`(,(or :after-line
                 :after-comment
                 :inside-string
-                :after-backslash
-                :inside-paren-at-closing-paren
-                :inside-paren-at-closing-nested-paren) . ,start)
+                :after-backslash) . ,start)
          ;; Copy previous indentation.
          (goto-char start)
          (current-indentation))
+        (`(,(or :inside-paren-at-closing-paren
+                :inside-paren-at-closing-nested-paren) . ,start)
+         (goto-char (+ 1 start))
+         (if (looking-at "[ \t]*\\(?:#\\|$\\)")
+             ;; Copy previous indentation.
+             (current-indentation)
+           ;; Align with opening paren.
+           (current-column)))
         (`(:inside-docstring . ,start)
          (let* ((line-indentation (current-indentation))
                 (base-indent (progn
-- 
2.11.0

>From 8649d874d5158d91b0b6056bc66b9a99f4c1c31a Mon Sep 17 00:00:00 2001
From: Noam Postavsky <[email-masked]>
Date: Sat, 20 Apr 2019 15:31:51 -0400
Subject: [PATCH 2/2] ; Add test for previous change

* test/lisp/progmodes/python-tests.el (python-indent-hanging-close-paren):
New test.
---
 test/lisp/progmodes/python-tests.el | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index b940f45bb9..a517909717 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -260,6 +260,19 @@ (ert-deftest python-indent-pep8-3 ()
    (should (eq (car (python-indent-context)) :inside-paren-newline-start))
    (should (= (python-indent-calculate-indentation) 4))))
 
+(ert-deftest python-indent-hanging-close-paren ()
+  "Like first pep8 case, but with hanging close paren." ;; See Bug#20742.
+  (python-tests-with-temp-buffer
+   "\
+foo = long_function_name(var_one, var_two,
+                         var_three, var_four
+                         )
+"
+   (should (= (python-indent-calculate-indentation) 0))
+   (python-tests-look-at ")")
+   (should (eq (car (python-indent-context)) :inside-paren-at-closing-paren))
+   (should (= (python-indent-calculate-indentation) 25))))
+
 (ert-deftest python-indent-base-case ()
   "Check base case does not trigger errors."
   (python-tests-with-temp-buffer
-- 
2.11.0