Skip to content

Python indentation in nested blocks dedents too much, does not dedent on def meth(): #1370

@krassowski

Description

@krassowski

Describe the issue

lang-python does not continue indentation at the end of a block, which was intentional as per this code comment and the reasoning was given here: codemirror/lang-python#4 (comment)

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:

gif

However, I think that dedent on enter should only move you one block back, not two blocks back, see:

should-have-dedented-once

Because there is no dedent when typing method definition (indent on input is not implemented for Python for this case):

no-dedent-on-method

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?

Of note VS Code always continues indentation.

Browser and platform

No response

Reproduction link

https://codemirror.net/try/?c=aW1wb3J0IHtiYXNpY1NldHVwLCBFZGl0b3JWaWV3fSBmcm9tICJjb2RlbWlycm9yIgppbXBvcnQge3B5dGhvbn0gZnJvbSAiQGNvZGVtaXJyb3IvbGFuZy1weXRob24iCmltcG9ydCB7IGluZGVudFVuaXQsIGluZGVudE9uSW5wdXQgfSBmcm9tICJAY29kZW1pcnJvci9sYW5ndWFnZSI7CgpuZXcgRWRpdG9yVmlldyh7CiAgZG9jOiAiZGVmIGZ1bmMoKTpcbiAgICBwYXNzXG5cbmNsYXNzIENsYXNzOlxuICAgIGRlZiBtZXRob2QoKTpcbiAgICAgICAgcGFzc1xuIiwKICBleHRlbnNpb25zOiBbYmFzaWNTZXR1cCwgcHl0aG9uKCksIGluZGVudFVuaXQub2YoJyAgICAnKSwgaW5kZW50T25JbnB1dCgpXSwKICBwYXJlbnQ6IGRvY3VtZW50LmJvZHkKfSkK

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions