GNU bug report logs - #29305
26.0.90; Wrong electrified indentation with Python multiline string

Previous Next

Package: emacs;

Reported by: Lele Gaifax <lele <at> metapensiero.it>

Date: Wed, 15 Nov 2017 09:35:02 UTC

Severity: minor

Tags: fixed, patch

Found in version 26.0.90

Fixed in version 26.1

Done: Noam Postavsky <npostavs <at> users.sourceforge.net>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 29305 in the body.
You can then email your comments to 29305 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#29305; Package emacs. (Wed, 15 Nov 2017 09:35:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Lele Gaifax <lele <at> metapensiero.it>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Wed, 15 Nov 2017 09:35:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Lele Gaifax <lele <at> metapensiero.it>
To: bug-gnu-emacs <at> gnu.org
Subject: 26.0.90; Wrong electrified indentation with Python multiline string
Date: Wed, 15 Nov 2017 10:33:39 +0100
[Message part 1 (text/plain, inline)]
In python-mode, when I'm inside a triple-quoted string or immediately after
it and insert (say) a comma, current line indentation should not change.

I tried to play with python-indent-post-self-insert-function, but could not
figure out how to recognize when I'm within or close to a multiline string.

The attached patch adds two (currently) failing tests.

[0002-Add-failing-tests-on-electric-indentation-and-Python.patch (text/x-diff, inline)]
From 21cd61ab71a16490a5c0c569a2715b7e5f2d7b98 Mon Sep 17 00:00:00 2001
From: Lele Gaifax <lele <at> metapensiero.it>
Date: Wed, 15 Nov 2017 10:10:19 +0100
Subject: [PATCH 2/2] Add failing tests on electric-indentation and Python
 multiline strings

* test/lisp/progmodes/python-tests.el
  (python-indent-electric-comma-inside-multiline-string,
  python-indent-electric-comma-after-multiline-string): New tests.
---
 test/lisp/progmodes/python-tests.el | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index a59885637e..010eb67160 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -1109,6 +1109,37 @@ python-tests-visible-string
    (should (eq (car (python-indent-context)) :inside-string))
    (should (= (python-indent-calculate-indentation) 4))))
 
+(ert-deftest python-indent-electric-comma-inside-multiline-string ()
+  "Test indentation ...."
+  (python-tests-with-temp-buffer
+   "
+a = (
+    '''\
+- foo,
+- bar
+'''
+"
+   (python-tests-look-at "- bar")
+   (should (eq (car (python-indent-context)) :inside-string))
+   (goto-char (line-end-position))
+   (python-tests-self-insert ",")
+   (should (= (current-indentation) 0))))
+
+(ert-deftest python-indent-electric-comma-after-multiline-string ()
+  "Test indentation ...."
+  (python-tests-with-temp-buffer
+   "
+a = (
+    '''\
+- foo,
+- bar'''
+"
+   (python-tests-look-at "- bar'''")
+   (should (eq (car (python-indent-context)) :inside-string))
+   (goto-char (line-end-position))
+   (python-tests-self-insert ",")
+   (should (= (current-indentation) 0))))
+
 (ert-deftest python-indent-electric-colon-1 ()
   "Test indentation case from Bug#18228."
   (python-tests-with-temp-buffer
-- 
2.15.0

[Message part 3 (text/plain, inline)]
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele <at> metapensiero.it  |                 -- Fortunato Depero, 1929.

Severity set to 'minor' from 'normal' Request was from Noam Postavsky <npostavs <at> users.sourceforge.net> to control <at> debbugs.gnu.org. (Sun, 19 Nov 2017 14:28:02 GMT) Full text and rfc822 format available.

Added tag(s) patch. Request was from Noam Postavsky <npostavs <at> users.sourceforge.net> to control <at> debbugs.gnu.org. (Sun, 19 Nov 2017 14:28:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#29305; Package emacs. (Sun, 19 Nov 2017 14:29:01 GMT) Full text and rfc822 format available.

Message #12 received at 29305 <at> debbugs.gnu.org (full text, mbox):

From: Noam Postavsky <npostavs <at> users.sourceforge.net>
To: Lele Gaifax <lele <at> metapensiero.it>
Cc: 29305 <at> debbugs.gnu.org
Subject: Re: bug#29305: 26.0.90;
 Wrong electrified indentation with Python multiline string
Date: Sun, 19 Nov 2017 09:27:46 -0500
[Message part 1 (text/plain, inline)]
severity 29305 minor
tags 29305 + patch
quit

Lele Gaifax <lele <at> metapensiero.it> writes:

> In python-mode, when I'm inside a triple-quoted string or immediately after
> it and insert (say) a comma, current line indentation should not change.
>
> I tried to play with python-indent-post-self-insert-function, but could not
> figure out how to recognize when I'm within or close to a multiline string.

How about this:

[0001-Disable-eletric-indent-for-python-strings-Bug-29305.patch (text/x-diff, inline)]
From d48c6f6d25911b2d128c72eec9ff5b3f457a3545 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs <at> gmail.com>
Date: Sun, 19 Nov 2017 09:00:43 -0500
Subject: [PATCH] Disable eletric indent for python strings (Bug#29305)

* lisp/progmodes/python.el (python-indent-post-self-insert-function):
Do nothing when in string syntax or indent context.
---
 lisp/progmodes/python.el | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index d4226e5ce7..093dd0ae65 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1257,7 +1257,9 @@ python-indent-post-self-insert-function
 If a line renders a paren alone, after adding a char before it,
 the line will be re-indented automatically if needed."
   (when (and electric-indent-mode
-             (eq (char-before) last-command-event))
+             (eq (char-before) last-command-event)
+             (not (python-syntax-context 'string))
+             (not (eq (car (python-indent-context)) :inside-string)))
     (cond
      ;; Electric indent inside parens
      ((and
-- 
2.11.0


Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#29305; Package emacs. (Tue, 21 Nov 2017 09:16:01 GMT) Full text and rfc822 format available.

Message #15 received at 29305 <at> debbugs.gnu.org (full text, mbox):

From: Lele Gaifax <lele <at> metapensiero.it>
To: Noam Postavsky <npostavs <at> users.sourceforge.net>
Cc: 29305 <at> debbugs.gnu.org
Subject: Re: bug#29305: 26.0.90;
 Wrong electrified indentation with Python multiline string
Date: Tue, 21 Nov 2017 10:15:12 +0100
Thanks Noam, I tried your suggestion and it seems doing the right thing, I
will keep exercising this for a while.

> @@ -1257,7 +1257,9 @@ python-indent-post-self-insert-function
>  If a line renders a paren alone, after adding a char before it,
>  the line will be re-indented automatically if needed."
>    (when (and electric-indent-mode
> -             (eq (char-before) last-command-event))
> +             (eq (char-before) last-command-event)
> +             (not (python-syntax-context 'string))
> +             (not (eq (car (python-indent-context)) :inside-string)))
>      (cond
>       ;; Electric indent inside parens
>       ((and

I wish to better understand *why* it works though: in particular, I fail to
see how it can handle the situation illustrated by my second test case
(python-indent-electric-comma-after-multiline-string):

    a = (
        '''\
    - foo,
    - bar'''

where I'm going to add a comma *after* the multiline string: when I tried to
find a solution, I thought I'd need to consider the state *at
(beginning-of-line)*, in other words, morphing my experiment on top of
your change to something like:

    (when (and electric-indent-mode
               (eq (char-before) last-command-event)
               (not (python-syntax-context 'string))
               (save-excursion
                 (beginning-of-line)
                 (not (eq (car (python-indent-context)) :inside-string))))

but your simpler code tells that it is not needed...

I will try harder ;-)

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele <at> metapensiero.it  |                 -- Fortunato Depero, 1929.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#29305; Package emacs. (Tue, 21 Nov 2017 13:53:01 GMT) Full text and rfc822 format available.

Message #18 received at 29305 <at> debbugs.gnu.org (full text, mbox):

From: Noam Postavsky <npostavs <at> users.sourceforge.net>
To: Lele Gaifax <lele <at> metapensiero.it>
Cc: 29305 <at> debbugs.gnu.org
Subject: Re: bug#29305: 26.0.90;
 Wrong electrified indentation with Python multiline string
Date: Tue, 21 Nov 2017 08:52:31 -0500
[Message part 1 (text/plain, inline)]
Lele Gaifax <lele <at> metapensiero.it> writes:

> where I'm going to add a comma *after* the multiline string: when I tried to
> find a solution, I thought I'd need to consider the state *at
> (beginning-of-line)*, in other words, morphing my experiment on top of
> your change to something like:
>
>     (when (and electric-indent-mode
>                (eq (char-before) last-command-event)
>                (not (python-syntax-context 'string))
>                (save-excursion
>                  (beginning-of-line)
>                  (not (eq (car (python-indent-context)) :inside-string))))
>
> but your simpler code tells that it is not needed...

Well, there is no big mystery I think.  It's simply that
`python-indent-context' goes to the beginning of line itself:

    (defun python-indent-context ()
      ...
      (save-restriction
        (prog-widen)
        (let ((ppss (save-excursion
                      (beginning-of-line)
                      (syntax-ppss))))

Actually, I forgot to check for an indent context of :inside-docstring
though.  I'm not sure if there is valid python code which could trigger
electric indent next to a docstring, but it's probably more correct to
do this instead:

[v2-0001-Disable-eletric-indent-for-python-strings-Bug-293.patch (text/x-diff, inline)]
From a965850b20109218713583743873952a395b7e1c Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs <at> gmail.com>
Date: Sun, 19 Nov 2017 09:00:43 -0500
Subject: [PATCH v2] Disable eletric indent for python strings (Bug#29305)

* lisp/progmodes/python.el (python-indent-post-self-insert-function):
Do nothing when point or beginning of line is in string.
---
 lisp/progmodes/python.el | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index d4226e5ce7..9e09bfc594 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1257,7 +1257,11 @@ python-indent-post-self-insert-function
 If a line renders a paren alone, after adding a char before it,
 the line will be re-indented automatically if needed."
   (when (and electric-indent-mode
-             (eq (char-before) last-command-event))
+             (eq (char-before) last-command-event)
+             (not (python-syntax-context 'string))
+             (save-excursion
+               (beginning-of-line)
+               (not (python-syntax-context 'string (syntax-ppss)))))
     (cond
      ;; Electric indent inside parens
      ((and
-- 
2.11.0


Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#29305; Package emacs. (Tue, 21 Nov 2017 14:23:02 GMT) Full text and rfc822 format available.

Message #21 received at 29305 <at> debbugs.gnu.org (full text, mbox):

From: Lele Gaifax <lele <at> metapensiero.it>
To: Noam Postavsky <npostavs <at> users.sourceforge.net>
Cc: 29305 <at> debbugs.gnu.org
Subject: Re: bug#29305: 26.0.90;
 Wrong electrified indentation with Python multiline string
Date: Tue, 21 Nov 2017 15:22:06 +0100
Noam Postavsky <npostavs <at> users.sourceforge.net> writes:

> Well, there is no big mystery I think.  It's simply that
> `python-indent-context' goes to the beginning of line itself:
>
>     (defun python-indent-context ()
>       ...
>       (save-restriction
>         (prog-widen)
>         (let ((ppss (save-excursion
>                       (beginning-of-line)
>                       (syntax-ppss))))

Doh, you are right, I just skimmed through its docstring :-|

> Actually, I forgot to check for an indent context of :inside-docstring
> though.  I'm not sure if there is valid python code which could trigger
> electric indent next to a docstring, but it's probably more correct to
> do this instead:

Will try your latest in my next Emacs sessions.

> Subject: [PATCH v2] Disable eletric indent for python strings (Bug#29305)

ele*ctric

Thank you again,
ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele <at> metapensiero.it  |                 -- Fortunato Depero, 1929.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#29305; Package emacs. (Sat, 02 Dec 2017 14:55:02 GMT) Full text and rfc822 format available.

Message #24 received at 29305 <at> debbugs.gnu.org (full text, mbox):

From: Noam Postavsky <npostavs <at> users.sourceforge.net>
To: Lele Gaifax <lele <at> metapensiero.it>
Cc: 29305 <at> debbugs.gnu.org
Subject: Re: bug#29305: 26.0.90;
 Wrong electrified indentation with Python multiline string
Date: Sat, 02 Dec 2017 09:54:08 -0500
tags 29305 fixed
close 29305 26.1
quit

Lele Gaifax <lele <at> metapensiero.it> writes:

>> Subject: [PATCH v2] Disable eletric indent for python strings (Bug#29305)
>
> ele*ctric

Thanks, pushed to emacs-26.

[1: 946bb6d225]: 2017-12-02 09:35:40 -0500
  Disable electric indent for python strings (Bug#29305)
  https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=946bb6d2250ce81fc68ec0bca4e1bb79815f4a59

[2: c02c1f6be7]: 2017-12-02 09:35:44 -0500
  Add tests on electric-indentation and Python multiline strings (Bug#29305)
  https://git.savannah.gnu.org/cgit/emacs.git/commit/?id=c02c1f6be7befdaf4f3987148db18121e1081dae




Added tag(s) fixed. Request was from Noam Postavsky <npostavs <at> users.sourceforge.net> to control <at> debbugs.gnu.org. (Sat, 02 Dec 2017 14:55:03 GMT) Full text and rfc822 format available.

bug marked as fixed in version 26.1, send any further explanations to 29305 <at> debbugs.gnu.org and Lele Gaifax <lele <at> metapensiero.it> Request was from Noam Postavsky <npostavs <at> users.sourceforge.net> to control <at> debbugs.gnu.org. (Sat, 02 Dec 2017 14:55:03 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 31 Dec 2017 12:24:03 GMT) Full text and rfc822 format available.

This bug report was last modified 7 years and 268 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.