GNU bug report logs -
#63959
python-mode does not keep indentation in square brackets []
Previous Next
To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 63959 in the body.
You can then email your comments to 63959 AT debbugs.gnu.org in the normal way.
Toggle the display of automated, internal messages from the tracker.
Report forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#63959
; Package
emacs
.
(Thu, 08 Jun 2023 09:40:02 GMT)
Full text and
rfc822 format available.
Acknowledgement sent
to
Konstantin Kharlamov <hi-angel <at> yandex.ru>
:
New bug report received and forwarded. Copy sent to
bug-gnu-emacs <at> gnu.org
.
(Thu, 08 Jun 2023 09:40:02 GMT)
Full text and
rfc822 format available.
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
Usually in programming modes, when previous indentation is kind of "special",
the new lines should keep the indentation from the previous line. However, it
doesn't work in this case.
# Steps to reproduce
1. Create file `test.py` with following content:
for infix in [ # some description
"_cdata", "_cmeta", "_corig", "_cpool", "_cvol", "_wcorig",
"indentation is broken here", "bar"]:
print(infix)
2. Open it as `emacs -Q test.py`
3. Put a caret on the 3rd line (which says "indentation is broken"
4. Press TAB
## Expected
Indentation won't change
## Actual
The line goes back by 4 spaces or so
# Additional information
emacs version: compiled from latest git a week ago, commit 5cace109d2b
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#63959
; Package
emacs
.
(Fri, 09 Jun 2023 11:22:01 GMT)
Full text and
rfc822 format available.
Message #8 received at 63959 <at> debbugs.gnu.org (full text, mbox):
> From: Konstantin Kharlamov <hi-angel <at> yandex.ru>
> Date: Thu, 08 Jun 2023 12:39:23 +0300
>
> Usually in programming modes, when previous indentation is kind of "special",
> the new lines should keep the indentation from the previous line. However, it
> doesn't work in this case.
>
>
> # Steps to reproduce
>
> 1. Create file `test.py` with following content:
>
> for infix in [ # some description
> "_cdata", "_cmeta", "_corig", "_cpool", "_cvol", "_wcorig",
> "indentation is broken here", "bar"]:
> print(infix)
>
> 2. Open it as `emacs -Q test.py`
> 3. Put a caret on the 3rd line (which says "indentation is broken"
> 4. Press TAB
>
>
> ## Expected
>
> Indentation won't change
>
> ## Actual
>
> The line goes back by 4 spaces or so
>
> # Additional information
>
> emacs version: compiled from latest git a week ago, commit 5cace109d2b
kobarity, any comments?
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#63959
; Package
emacs
.
(Fri, 09 Jun 2023 14:36:01 GMT)
Full text and
rfc822 format available.
Message #11 received at 63959 <at> debbugs.gnu.org (full text, mbox):
Eli Zaretskii wrote:
> > From: Konstantin Kharlamov <hi-angel <at> yandex.ru>
> > Date: Thu, 08 Jun 2023 12:39:23 +0300
> >
> > Usually in programming modes, when previous indentation is kind of "special",
> > the new lines should keep the indentation from the previous line. However, it
> > doesn't work in this case.
> >
> >
> > # Steps to reproduce
> >
> > 1. Create file `test.py` with following content:
> >
> > for infix in [ # some description
> > "_cdata", "_cmeta", "_corig", "_cpool", "_cvol", "_wcorig",
> > "indentation is broken here", "bar"]:
> > print(infix)
> >
> > 2. Open it as `emacs -Q test.py`
> > 3. Put a caret on the 3rd line (which says "indentation is broken"
> > 4. Press TAB
> >
> >
> > ## Expected
> >
> > Indentation won't change
> >
> > ## Actual
> >
> > The line goes back by 4 spaces or so
> >
> > # Additional information
> >
> > emacs version: compiled from latest git a week ago, commit 5cace109d2b
>
> kobarity, any comments?
I think the current Python mode tries to indent based on parens,
regardless of the indentation of the previous line. However, it would
also be reasonable to maintain the indentation of the previous line.
I will see if I can implement it.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#63959
; Package
emacs
.
(Sun, 18 Jun 2023 14:58:02 GMT)
Full text and
rfc822 format available.
Message #14 received at 63959 <at> debbugs.gnu.org (full text, mbox):
[Message part 1 (text/plain, inline)]
I wrote:
> Eli Zaretskii wrote:
> > > From: Konstantin Kharlamov <hi-angel <at> yandex.ru>
> > > Date: Thu, 08 Jun 2023 12:39:23 +0300
> > >
> > > Usually in programming modes, when previous indentation is kind of "special",
> > > the new lines should keep the indentation from the previous line. However, it
> > > doesn't work in this case.
> > >
> > >
> > > # Steps to reproduce
> > >
> > > 1. Create file `test.py` with following content:
> > >
> > > for infix in [ # some description
> > > "_cdata", "_cmeta", "_corig", "_cpool", "_cvol", "_wcorig",
> > > "indentation is broken here", "bar"]:
> > > print(infix)
> > >
> > > 2. Open it as `emacs -Q test.py`
> > > 3. Put a caret on the 3rd line (which says "indentation is broken"
> > > 4. Press TAB
> > >
> > >
> > > ## Expected
> > >
> > > Indentation won't change
> > >
> > > ## Actual
> > >
> > > The line goes back by 4 spaces or so
> > >
> > > # Additional information
> > >
> > > emacs version: compiled from latest git a week ago, commit 5cace109d2b
> >
> > kobarity, any comments?
>
> I think the current Python mode tries to indent based on parens,
> regardless of the indentation of the previous line. However, it would
> also be reasonable to maintain the indentation of the previous line.
> I will see if I can implement it.
Attached is a patch to implement it. I introduced a new indent
context `:inside-paren-continuation-line' for the continuation lines
within paren. The indent context respects the indentation of the
previous line.
However, it may happen that the previous line is indented for the
inner paren as in the ERT `python-indent-inside-paren-2':
data = {'key': {
'objlist': [
{'pk': 1,
'name': 'first'},
{'pk': 2,
'name': 'second'}
]
}}
The line "{'pk': 2," is considered as the continuation line, but it
should not respect the indentation of the PREVIOUS line "'name':
'first'},". So skipping such lines with inner parens were needed.
It searches backward for the line which starts with the item of the
same opening paren as the target line.
In the case of the above example, if the target line is "{'pk': 2,",
its opening paren is "[" in the line "'objlist': [". It first checks
the previous line "'name': 'first'},", but its opening paren is "{" in
"{'pk': 1,". So this line is skipped. Next, it checks the line
"{'pk': 1," and its opening paren is "[" in the line "'objlist': [",
which is same as the target line. So the target line's indentation
will be as same as the line "{'pk': 1,".
It would be helpful if you could try this patch.
Does anyone think we should have a customize variable that switches
between the traditional behavior of ignoring the indentation of the
previous line and this new behavior?
[0001-Fix-Python-indentation-of-continuation-lines-within-.patch (application/octet-stream, attachment)]
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#63959
; Package
emacs
.
(Sun, 18 Jun 2023 15:21:01 GMT)
Full text and
rfc822 format available.
Message #17 received at 63959 <at> debbugs.gnu.org (full text, mbox):
On Sun, 2023-06-18 at 23:56 +0900, kobarity wrote:
>
> I wrote:
> > Eli Zaretskii wrote:
> > > > From: Konstantin Kharlamov <hi-angel <at> yandex.ru>
> > > > Date: Thu, 08 Jun 2023 12:39:23 +0300
> > > >
> > > > Usually in programming modes, when previous indentation is kind of
> > > > "special",
> > > > the new lines should keep the indentation from the previous line.
> > > > However, it
> > > > doesn't work in this case.
> > > >
> > > >
> > > > # Steps to reproduce
> > > >
> > > > 1. Create file `test.py` with following content:
> > > >
> > > > for infix in [ # some description
> > > > "_cdata", "_cmeta", "_corig", "_cpool", "_cvol",
> > > > "_wcorig",
> > > > "indentation is broken here", "bar"]:
> > > > print(infix)
> > > >
> > > > 2. Open it as `emacs -Q test.py`
> > > > 3. Put a caret on the 3rd line (which says "indentation is broken"
> > > > 4. Press TAB
> > > >
> > > >
> > > > ## Expected
> > > >
> > > > Indentation won't change
> > > >
> > > > ## Actual
> > > >
> > > > The line goes back by 4 spaces or so
> > > >
> > > > # Additional information
> > > >
> > > > emacs version: compiled from latest git a week ago, commit 5cace109d2b
> > >
> > > kobarity, any comments?
> >
> > I think the current Python mode tries to indent based on parens,
> > regardless of the indentation of the previous line. However, it would
> > also be reasonable to maintain the indentation of the previous line.
> > I will see if I can implement it.
>
> Attached is a patch to implement it. I introduced a new indent
> context `:inside-paren-continuation-line' for the continuation lines
> within paren. The indent context respects the indentation of the
> previous line.
>
> However, it may happen that the previous line is indented for the
> inner paren as in the ERT `python-indent-inside-paren-2':
>
> data = {'key': {
> 'objlist': [
> {'pk': 1,
> 'name': 'first'},
> {'pk': 2,
> 'name': 'second'}
> ]
> }}
>
> The line "{'pk': 2," is considered as the continuation line, but it
> should not respect the indentation of the PREVIOUS line "'name':
> 'first'},". So skipping such lines with inner parens were needed.
> It searches backward for the line which starts with the item of the
> same opening paren as the target line.
>
> In the case of the above example, if the target line is "{'pk': 2,",
> its opening paren is "[" in the line "'objlist': [". It first checks
> the previous line "'name': 'first'},", but its opening paren is "{" in
> "{'pk': 1,". So this line is skipped. Next, it checks the line
> "{'pk': 1," and its opening paren is "[" in the line "'objlist': [",
> which is same as the target line. So the target line's indentation
> will be as same as the line "{'pk': 1,".
>
> It would be helpful if you could try this patch.
Thank you, tested, works for me!
> Does anyone think we should have a customize variable that switches
> between the traditional behavior of ignoring the indentation of the
> previous line and this new behavior?
I doubt it's useful. It is in general how indentation works in many modes, and I
don't think there ever been a variable to disable that. Stefan Monnier in
particular has a paper called "SMIE: weakness is power", where such indentation
is shortly discussed.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#63959
; Package
emacs
.
(Mon, 19 Jun 2023 08:47:02 GMT)
Full text and
rfc822 format available.
Message #20 received at submit <at> debbugs.gnu.org (full text, mbox):
Am 18.06.23 um 17:20 schrieb Konstantin Kharlamov:
> On Sun, 2023-06-18 at 23:56 +0900, kobarity wrote:
>> I wrote:
>>> Eli Zaretskii wrote:
>>>>> From: Konstantin Kharlamov <hi-angel <at> yandex.ru>
>>>>> Date: Thu, 08 Jun 2023 12:39:23 +0300
>>>>>
>>>>> Usually in programming modes, when previous indentation is kind of
>>>>> "special",
>>>>> the new lines should keep the indentation from the previous line.
>>>>> However, it
>>>>> doesn't work in this case.
>>>>>
>>>>>
>>>>> # Steps to reproduce
>>>>>
>>>>> 1. Create file `test.py` with following content:
>>>>>
>>>>> for infix in [ # some description
>>>>> "_cdata", "_cmeta", "_corig", "_cpool", "_cvol",
>>>>> "_wcorig",
>>>>> "indentation is broken here", "bar"]:
>>>>> print(infix)
>>>>>
>>>>> 2. Open it as `emacs -Q test.py`
>>>>> 3. Put a caret on the 3rd line (which says "indentation is broken"
>>>>> 4. Press TAB
>>>>>
>>>>>
>>>>> ## Expected
>>>>>
>>>>> Indentation won't change
>>>>>
>>>>> ## Actual
>>>>>
>>>>> The line goes back by 4 spaces or so
>>>>>
>>>>> # Additional information
>>>>>
>>>>> emacs version: compiled from latest git a week ago, commit 5cace109d2b
>>>> kobarity, any comments?
>>> I think the current Python mode tries to indent based on parens,
>>> regardless of the indentation of the previous line. However, it would
>>> also be reasonable to maintain the indentation of the previous line.
>>> I will see if I can implement it.
>> Attached is a patch to implement it. I introduced a new indent
>> context `:inside-paren-continuation-line' for the continuation lines
>> within paren. The indent context respects the indentation of the
>> previous line.
>>
>> However, it may happen that the previous line is indented for the
>> inner paren as in the ERT `python-indent-inside-paren-2':
>>
>> data = {'key': {
>> 'objlist': [
>> {'pk': 1,
>> 'name': 'first'},
>> {'pk': 2,
>> 'name': 'second'}
>> ]
>> }}
>>
>> The line "{'pk': 2," is considered as the continuation line, but it
>> should not respect the indentation of the PREVIOUS line "'name':
>> 'first'},". So skipping such lines with inner parens were needed.
>> It searches backward for the line which starts with the item of the
>> same opening paren as the target line.
>>
>> In the case of the above example, if the target line is "{'pk': 2,",
>> its opening paren is "[" in the line "'objlist': [". It first checks
>> the previous line "'name': 'first'},", but its opening paren is "{" in
>> "{'pk': 1,". So this line is skipped. Next, it checks the line
>> "{'pk': 1," and its opening paren is "[" in the line "'objlist': [",
>> which is same as the target line. So the target line's indentation
>> will be as same as the line "{'pk': 1,".
>>
>> It would be helpful if you could try this patch.
> Thank you, tested, works for me!
>
>> Does anyone think we should have a customize variable that switches
>> between the traditional behavior of ignoring the indentation of the
>> previous line and this new behavior?
> I doubt it's useful. It is in general how indentation works in many modes, and I
> don't think there ever been a variable to disable that. Stefan Monnier in
> particular has a paper called "SMIE: weakness is power", where such indentation
> is shortly discussed.
>
>
> IMO the question of previous lines indentation isn't raised here, as a more specific rule applies: inside a non-empty list
> indent with its first element. Whereas if list starts empty, as with "[" here, next indents as that line plus offset.
Reply sent
to
Eli Zaretskii <eliz <at> gnu.org>
:
You have taken responsibility.
(Sat, 24 Jun 2023 12:15:02 GMT)
Full text and
rfc822 format available.
Notification sent
to
Konstantin Kharlamov <hi-angel <at> yandex.ru>
:
bug acknowledged by developer.
(Sat, 24 Jun 2023 12:15:02 GMT)
Full text and
rfc822 format available.
Message #25 received at 63959-done <at> debbugs.gnu.org (full text, mbox):
> From: Konstantin Kharlamov <hi-angel <at> yandex.ru>
> Cc: 63959 <at> debbugs.gnu.org, Stefan Monnier <monnier <at> iro.umontreal.ca>
> Date: Sun, 18 Jun 2023 18:20:25 +0300
>
> > It would be helpful if you could try this patch.
>
> Thank you, tested, works for me!
Thanks for testing. I've now installed this on the master branch, and
I'm marking the bug done.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#63959
; Package
emacs
.
(Sat, 24 Jun 2023 12:38:02 GMT)
Full text and
rfc822 format available.
Message #28 received at 63959 <at> debbugs.gnu.org (full text, mbox):
Andreas Röhler wrote:
> >> However, it may happen that the previous line is indented for the
> >> inner paren as in the ERT `python-indent-inside-paren-2':
> >>
> >> data = {'key': {
> >> 'objlist': [
> >> {'pk': 1,
> >> 'name': 'first'},
> >> {'pk': 2,
> >> 'name': 'second'}
> >> ]
> >> }}
> >>
> >> The line "{'pk': 2," is considered as the continuation line, but it
> >> should not respect the indentation of the PREVIOUS line "'name':
> >> 'first'},". So skipping such lines with inner parens were needed.
> >> It searches backward for the line which starts with the item of the
> >> same opening paren as the target line.
> >>
> >> In the case of the above example, if the target line is "{'pk': 2,",
> >> its opening paren is "[" in the line "'objlist': [". It first checks
> >> the previous line "'name': 'first'},", but its opening paren is "{" in
> >> "{'pk': 1,". So this line is skipped. Next, it checks the line
> >> "{'pk': 1," and its opening paren is "[" in the line "'objlist': [",
> >> which is same as the target line. So the target line's indentation
> >> will be as same as the line "{'pk': 1,".
> >>
> >> It would be helpful if you could try this patch.
> > Thank you, tested, works for me!
> >
> >> Does anyone think we should have a customize variable that switches
> >> between the traditional behavior of ignoring the indentation of the
> >> previous line and this new behavior?
> > I doubt it's useful. It is in general how indentation works in many modes, and I
> > don't think there ever been a variable to disable that. Stefan Monnier in
> > particular has a paper called "SMIE: weakness is power", where such indentation
> > is shortly discussed.
> >
> >
> > IMO the question of previous lines indentation isn't raised here, as a more specific rule applies: inside a non-empty list
> > indent with its first element. Whereas if list starts empty, as with "[" here, next indents as that line plus offset.
Let me explain it again. I added the line numbers.
1: data = {'key': {
2: 'objlist': [
3: {'pk': 1,
4: 'name': 'first'},
5: {'pk': 2,
6: 'name': 'second'}
7: ]
8: }}
I think you are saying that the third line will be indented with an
additional offset to the second line, am I correct? If so, I totally
agree with you. This behavior has not changed with my patch.
What I wanted to say in the previous mail is that the fifth line
should align with the third line, not the fourth line.
As for such standard indentation, my patch does not change the
behavior.
However, if the user intentionally changed the indentation of the
third line, the current Python mode would indent the fourth and
subsequent lines as follows:
1: data = {'key': {
2: 'objlist': [
3: {'pk': 1, # Intentionally changed
4: 'name': 'first'},
5: {'pk': 2,
6: 'name': 'second'}
7: ]
8: }}
After applying my patch, it is indented as follows:
1: data = {'key': {
2: 'objlist': [
3: {'pk': 1, # Intentionally changed
4: 'name': 'first'},
5: {'pk': 2,
6: 'name': 'second'}
7: ]
8: }}
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#63959
; Package
emacs
.
(Wed, 28 Jun 2023 06:59:02 GMT)
Full text and
rfc822 format available.
Message #31 received at 63959 <at> debbugs.gnu.org (full text, mbox):
Il 24.06.23 14:37, kobarity ha scritto:
> Andreas Röhler wrote:
>>>> However, it may happen that the previous line is indented for the
>>>> inner paren as in the ERT `python-indent-inside-paren-2':
>>>>
>>>> data = {'key': {
>>>> 'objlist': [
>>>> {'pk': 1,
>>>> 'name': 'first'},
>>>> {'pk': 2,
>>>> 'name': 'second'}
>>>> ]
>>>> }}
>>>>
>>>> The line "{'pk': 2," is considered as the continuation line, but it
>>>> should not respect the indentation of the PREVIOUS line "'name':
>>>> 'first'},". So skipping such lines with inner parens were needed.
>>>> It searches backward for the line which starts with the item of the
>>>> same opening paren as the target line.
>>>>
>>>> In the case of the above example, if the target line is "{'pk': 2,",
>>>> its opening paren is "[" in the line "'objlist': [". It first checks
>>>> the previous line "'name': 'first'},", but its opening paren is "{" in
>>>> "{'pk': 1,". So this line is skipped. Next, it checks the line
>>>> "{'pk': 1," and its opening paren is "[" in the line "'objlist': [",
>>>> which is same as the target line. So the target line's indentation
>>>> will be as same as the line "{'pk': 1,".
>>>>
>>>> It would be helpful if you could try this patch.
>>> Thank you, tested, works for me!
>>>
>>>> Does anyone think we should have a customize variable that switches
>>>> between the traditional behavior of ignoring the indentation of the
>>>> previous line and this new behavior?
>>> I doubt it's useful. It is in general how indentation works in many modes, and I
>>> don't think there ever been a variable to disable that. Stefan Monnier in
>>> particular has a paper called "SMIE: weakness is power", where such indentation
>>> is shortly discussed.
>>>
>>>
>>> IMO the question of previous lines indentation isn't raised here, as a more specific rule applies: inside a non-empty list
>>> indent with its first element. Whereas if list starts empty, as with "[" here, next indents as that line plus offset.
> Let me explain it again. I added the line numbers.
>
> 1: data = {'key': {
> 2: 'objlist': [
> 3: {'pk': 1,
> 4: 'name': 'first'},
> 5: {'pk': 2,
> 6: 'name': 'second'}
> 7: ]
> 8: }}
>
> I think you are saying that the third line will be indented with an
> additional offset to the second line, am I correct? If so, I totally
> agree with you. This behavior has not changed with my patch.
>
> What I wanted to say in the previous mail is that the fifth line
> should align with the third line, not the fourth line.
>
> As for such standard indentation, my patch does not change the
> behavior.
>
> However, if the user intentionally changed the indentation of the
> third line, the current Python mode would indent the fourth and
> subsequent lines as follows:
>
> 1: data = {'key': {
> 2: 'objlist': [
> 3: {'pk': 1, # Intentionally changed
> 4: 'name': 'first'},
> 5: {'pk': 2,
> 6: 'name': 'second'}
> 7: ]
> 8: }}
>
> After applying my patch, it is indented as follows:
>
> 1: data = {'key': {
> 2: 'objlist': [
> 3: {'pk': 1, # Intentionally changed
> 4: 'name': 'first'},
> 5: {'pk': 2,
> 6: 'name': 'second'}
> 7: ]
> 8: }}
Thanks for your explanation, which makes me better understand your
endeavour. A question remains: is this new feature worth that possibly
raise of complexity? Your patch provides a higher degree of freedom
while keeping regularity - which is a pro.
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#63959
; Package
emacs
.
(Sat, 01 Jul 2023 13:44:02 GMT)
Full text and
rfc822 format available.
Message #34 received at 63959 <at> debbugs.gnu.org (full text, mbox):
Andreas Röhler wrote:
> Thanks for your explanation, which makes me better understand your
> endeavour. A question remains: is this new feature worth that possibly
> raise of complexity? Your patch provides a higher degree of freedom
> while keeping regularity - which is a pro.
I think it is natural for many people to indent the same as the
previous line. In fact, it is standard behavior outside the parens.
For example, if you intentionally change the indentation of the first
line of a block, the following lines will have the same indentation:
def func():
a = 1 # Intentionally changed.
b = 2 # Same indent as previous line
You can even do the following, although it will result in an
IndentationError when executed:
def func():
a = 1
b = 2 # Intentionally changed.
c = 3 # Same indent as previous line
If inside the parens, the following will not result in an
IndentationError:
a = (
1,
2, # Intentionally changed.
3) # Same indent as previous line
So I rather think the rule of indenting the same as the previous line
is more useful inside parens.
I would consider making the indentation the same as the previous line
inside the parens a fix rather than a new feature. This is why I
named the commit "Fix Python indentation of continuation lines within
parens."
Information forwarded
to
bug-gnu-emacs <at> gnu.org
:
bug#63959
; Package
emacs
.
(Sat, 01 Jul 2023 13:59:02 GMT)
Full text and
rfc822 format available.
Message #37 received at 63959 <at> debbugs.gnu.org (full text, mbox):
> I would consider making the indentation the same as the previous line
> inside the parens a fix rather than a new feature.
Definitely. It might also fix a performance bug (when re-indenting
line by line a long parenthesized list of things, always going back to
the beginning can make the overall indentation O(N²) rather than O(N),
where N is the number of lines).
Stefan
bug archived.
Request was from
Debbugs Internal Request <help-debbugs <at> gnu.org>
to
internal_control <at> debbugs.gnu.org
.
(Sun, 30 Jul 2023 11:24:10 GMT)
Full text and
rfc822 format available.
This bug report was last modified 2 years and 11 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.