[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#63959: python-mode does not keep indentation in square brackets []
From: |
Konstantin Kharlamov |
Subject: |
bug#63959: python-mode does not keep indentation in square brackets [] |
Date: |
Sun, 18 Jun 2023 18:20:25 +0300 |
User-agent: |
Evolution 3.48.3 |
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.