python-indent
python-indent copied to clipboard
Incorrect indentation when closing hanging indent
If a hanging indent is closed after some values have been entered, it incorrectly keeps the indentation level.
Example:
x = [
0,1,2,3]
pressing enter before the ]
character results in
x = [
0,1,2,3
]
instead of producing
x = [
0,1,2,3
]
as expected. If the hanging indent were created to begin with, then the bracket would be correctly placed, this only occurs if a previously non-hanging-indented set of lines were converted to a hanging indent.
Note, however, that it is not enough simply to look for a comma. Consider the case of the (admittedly rarely used) multi-line string created via parentheses:
x = (
'here is a multi-line string'
' that spans several lines without any commas'
' how will we detect this correctly? If this were'
' inside brackets, then we would want to see if there'
' is a comma, but since it is parentheses, there does'
' not need to be a comma...'
'It appears as if there is no certain way to tell what the'
' indentation should be here, probably best to default to'
' current behavior, which is to set indentation to current indentation.'
)
x = [
'compare the above to this',
'where now we should close the hanging'
'indent because there is no comma'
]
Need to think more about what the desired behavior actually is here.
This case comes up about once every 2 weeks for me. Definitely not a pressing issue but it is on my mind. I do intend to update the parser to handle this... someday.
Sorry...closed the wrong issue.
Hello and thank you for the great package! I'm not completely sure this is the same exact issue, but it seems like it. For me even if nothing is yet entered in any type of brackets, when I hit enter this happens:
a = [<- pressed enter here
|<- cursor is here
]
expected behaviour would be:
a = [
|
]
As far as I remember it started to happen after one of the updates not that long ago (unfortunately can't tell after which exactly, about maybe a month ago)
@igorzamyslov I get your expected behavior running version 1.1.2 of this package, Atom version 1.21.0. Is the package up to date and all of that? Do you have another package that could be interfering with the indentation?
@kbrose, Atom itself and all the packages are up-to-date. And I suppose I've found a problem.
I've got a following setup:
- MagicPython (https://github.com/MagicStack/MagicPython) for python syntax
- language-python (default Atom package) disabled - as it is proposed in MagicPython installation instructions.
As soon as I enable language-python package - indentations are working as expected. I guess I will try to work this way, hope there won't be any compatibility issues. Thank you for the help!
Hmm. I suspect both this package and magic python try to do the same thing on a hanging indent so you end up indenting it twice (which ends up with your demonstrated behavior). Not sure of an easy work-around there...
I think it's worth noting that PEP 8 explicitly allows the closing bracket to line up in either of the following ways:
my_list = [
1, 2, 3,
4, 5, 6,
]
or
my_list = [
1, 2, 3,
4, 5, 6,
]
Those examples are taken verbatim from PEP 8 itself. So the original premise (that maintaining the indentation level on the closing bracket is somehow incorrect) is wrong.
The most proper way to handle this is with a setting that allows the user to choose either of these styles.
?