unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Andreas Röhler" <andreas.roehler@easy-emacs.de>
To: 63959@debbugs.gnu.org
Subject: bug#63959: python-mode does not keep indentation in square brackets []
Date: Mon, 19 Jun 2023 10:46:48 +0200	[thread overview]
Message-ID: <93804360-aafe-fe2f-46fa-f2fca67d9add@easy-emacs.de> (raw)
In-Reply-To: <45d2d886ba9bd52f33c588c457cfc0338f504eca.camel@yandex.ru>


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@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	other threads:[~2023-06-19  8:46 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-08  9:39 bug#63959: python-mode does not keep indentation in square brackets [] Konstantin Kharlamov
2023-06-09 11:21 ` Eli Zaretskii
2023-06-09 14:35   ` kobarity
2023-06-18 14:56     ` kobarity
2023-06-18 15:20       ` Konstantin Kharlamov
2023-06-19  8:46         ` Andreas Röhler [this message]
2023-06-24 12:37           ` kobarity
2023-06-28  6:58             ` Andreas Röhler
2023-07-01 13:42               ` kobarity
2023-07-01 13:57                 ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-06-24 12:14         ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=93804360-aafe-fe2f-46fa-f2fca67d9add@easy-emacs.de \
    --to=andreas.roehler@easy-emacs.de \
    --cc=63959@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).