You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 15, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 15, 2026. It is now read-only.
Python indentation in nested blocks dedents too much, does not dedent on def meth(): #1370
I think this is the worst example, where the user intends to end the block, leave a blank line and start a new block of code but the indentation is forced into alignment with the body:
However, I think that dedent on enter should only move you one block back, not two blocks back, see:
Because there is no dedent when typing method definition (indent on input is not implemented for Python for this case):
the current implementation is de facto fighting the user who tries to add a method to a class definition, they end up with wrong indentation regardless of whether they decide to:
(a) add a new line after the first indented line (as per convention in Python there is an empty line between method definitions), or
(b) start writing in the first new line
In (a) case they end up with too little indent, in (b) they end up with too much indent (and lack of new line).
I am not sure how difficult this is to implement, but if it is too difficult, could there be a setting to allow the user to opt-in to always continue block indentation, or to say continue block indentation until at least 2 blank lines were entered and only dedent on the third one?
Dedenting two units seems pretty arbitrary as well. It's fundamentally impossible to know what the user is trying to do with an indentation-based language like this. I don't think it's a productive road to go down to add increasingly complex heuristics for this.
What VS Code seems to do is just crudely repeating the indentation of the line before without any kind of syntactic awareness. I could add a command that does that, if that's what you want here.
It's fundamentally impossible to know what the user is trying to do with an indentation-based language like this.
Right, I now see that my example with def meth() - while useful at demonstrating the struggle of a user - is ambiguous itself because another user may have wanted to define a closure instead in which case this is the correct behaviour.
What VS Code seems to do is just crudely repeating the indentation of the line before without any kind of syntactic awareness. I could add a command that does that, if that's what you want here.
I think this is what a number of Python users might prefer. A new command (or argument to existing insertNewlineAndIndent command?) sounds good to me.
I tried insertNewlineKeepIndent and it indeed does what the name suggests. It keeps the indent constant always, and does not increase the indent when language rules would encourage it:
What VS Code seems to do is just crudely repeating the indentation of the line before without any kind of syntactic awareness.
I misunderstood this initially. I thought that you were saying that it repeats the indentation of the line before and never deindents it when in a block - which is correct. However, it does have a very good syntactic awareness, compare the previous GIF with:
It seems that there's both the out-of-the-box VS Code Python behavior, which is what I was looking at, and then a slightly smarter auto-indentation when you install a Python-specific package. It still seems to work with simple regexps (indent on colon, dedent on pass), but indeed doesn't simply preserve previous indent.
I don't want to make this configurable. But attached patch might make it work more like what you're looking for.
Activity
marijnh commentedon Apr 19, 2024
Dedenting two units seems pretty arbitrary as well. It's fundamentally impossible to know what the user is trying to do with an indentation-based language like this. I don't think it's a productive road to go down to add increasingly complex heuristics for this.
What VS Code seems to do is just crudely repeating the indentation of the line before without any kind of syntactic awareness. I could add a command that does that, if that's what you want here.
krassowski commentedon Apr 19, 2024
Right, I now see that my example with
def meth()- while useful at demonstrating the struggle of a user - is ambiguous itself because another user may have wanted to define a closure instead in which case this is the correct behaviour.I think this is what a number of Python users might prefer. A new command (or argument to existing
insertNewlineAndIndentcommand?) sounds good to me.Add insertNewlineKeepIndent command
marijnh commentedon Apr 19, 2024
Attached patch adds an
insertNewlineKeepIndentcommand that you can bind to enter to get the behavior where it simply preserves indentation.krassowski commentedon Apr 26, 2024
I tried
insertNewlineKeepIndentand it indeed does what the name suggests. It keeps the indent constant always, and does not increase the indent when language rules would encourage it:I misunderstood this initially. I thought that you were saying that it repeats the indentation of the line before and never deindents it when in a block - which is correct. However, it does have a very good syntactic awareness, compare the previous GIF with:
Base continued body in indentation on the indentation of the line before
marijnh commentedon Apr 29, 2024
It seems that there's both the out-of-the-box VS Code Python behavior, which is what I was looking at, and then a slightly smarter auto-indentation when you install a Python-specific package. It still seems to work with simple regexps (indent on colon, dedent on
pass), but indeed doesn't simply preserve previous indent.I don't want to make this configurable. But attached patch might make it work more like what you're looking for.