all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#29305: 26.0.90; Wrong electrified indentation with Python multiline string
@ 2017-11-15  9:33 Lele Gaifax
  2017-11-19 14:27 ` Noam Postavsky
  0 siblings, 1 reply; 6+ messages in thread
From: Lele Gaifax @ 2017-11-15  9:33 UTC (permalink / raw)
  To: 29305

[-- Attachment #1: Type: text/plain, Size: 359 bytes --]

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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0002-Add-failing-tests-on-electric-indentation-and-Python.patch --]
[-- Type: text/x-diff, Size: 1820 bytes --]

From 21cd61ab71a16490a5c0c569a2715b7e5f2d7b98 Mon Sep 17 00:00:00 2001
From: Lele Gaifax <lele@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


[-- Attachment #3: Type: text/plain, Size: 211 bytes --]


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

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* bug#29305: 26.0.90; Wrong electrified indentation with Python multiline string
  2017-11-15  9:33 bug#29305: 26.0.90; Wrong electrified indentation with Python multiline string Lele Gaifax
@ 2017-11-19 14:27 ` Noam Postavsky
  2017-11-21  9:15   ` Lele Gaifax
  0 siblings, 1 reply; 6+ messages in thread
From: Noam Postavsky @ 2017-11-19 14:27 UTC (permalink / raw)
  To: Lele Gaifax; +Cc: 29305

[-- Attachment #1: Type: text/plain, Size: 419 bytes --]

severity 29305 minor
tags 29305 + patch
quit

Lele Gaifax <lele@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:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-diff, Size: 1116 bytes --]

From d48c6f6d25911b2d128c72eec9ff5b3f457a3545 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs@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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* bug#29305: 26.0.90; Wrong electrified indentation with Python multiline string
  2017-11-19 14:27 ` Noam Postavsky
@ 2017-11-21  9:15   ` Lele Gaifax
  2017-11-21 13:52     ` Noam Postavsky
  0 siblings, 1 reply; 6+ messages in thread
From: Lele Gaifax @ 2017-11-21  9:15 UTC (permalink / raw)
  To: Noam Postavsky; +Cc: 29305

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@metapensiero.it  |                 -- Fortunato Depero, 1929.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#29305: 26.0.90; Wrong electrified indentation with Python multiline string
  2017-11-21  9:15   ` Lele Gaifax
@ 2017-11-21 13:52     ` Noam Postavsky
  2017-11-21 14:22       ` Lele Gaifax
  0 siblings, 1 reply; 6+ messages in thread
From: Noam Postavsky @ 2017-11-21 13:52 UTC (permalink / raw)
  To: Lele Gaifax; +Cc: 29305

[-- Attachment #1: Type: text/plain, Size: 1211 bytes --]

Lele Gaifax <lele@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:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: patch --]
[-- Type: text/x-diff, Size: 1192 bytes --]

From a965850b20109218713583743873952a395b7e1c Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs@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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* bug#29305: 26.0.90; Wrong electrified indentation with Python multiline string
  2017-11-21 13:52     ` Noam Postavsky
@ 2017-11-21 14:22       ` Lele Gaifax
  2017-12-02 14:54         ` Noam Postavsky
  0 siblings, 1 reply; 6+ messages in thread
From: Lele Gaifax @ 2017-11-21 14:22 UTC (permalink / raw)
  To: Noam Postavsky; +Cc: 29305

Noam Postavsky <npostavs@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@metapensiero.it  |                 -- Fortunato Depero, 1929.





^ permalink raw reply	[flat|nested] 6+ messages in thread

* bug#29305: 26.0.90; Wrong electrified indentation with Python multiline string
  2017-11-21 14:22       ` Lele Gaifax
@ 2017-12-02 14:54         ` Noam Postavsky
  0 siblings, 0 replies; 6+ messages in thread
From: Noam Postavsky @ 2017-12-02 14:54 UTC (permalink / raw)
  To: Lele Gaifax; +Cc: 29305

tags 29305 fixed
close 29305 26.1
quit

Lele Gaifax <lele@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





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-12-02 14:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-11-15  9:33 bug#29305: 26.0.90; Wrong electrified indentation with Python multiline string Lele Gaifax
2017-11-19 14:27 ` Noam Postavsky
2017-11-21  9:15   ` Lele Gaifax
2017-11-21 13:52     ` Noam Postavsky
2017-11-21 14:22       ` Lele Gaifax
2017-12-02 14:54         ` Noam Postavsky

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.