Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dedent nested else blocks correctly #4820

Open
kimadeline opened this issue Jul 17, 2019 · 2 comments
Open

Dedent nested else blocks correctly #4820

kimadeline opened this issue Jul 17, 2019 · 2 comments
Assignees
Labels
auto-indent Bugs in the auto indent feature bug Something isn't working P2

Comments

@kimadeline
Copy link

Is it a bug or an enhancement to the regexes added when working on microsoft/vscode-python#6333 (PR microsoft/vscode-python#6497)?

Expected behaviour

if foo:
   if bar:
      print('bar')
   else:
      print('foo')
else:
      print('baz')

Actual behaviour

dedent-else

@kimadeline kimadeline added the bug Something isn't working label Jul 17, 2019
@jkyeung
Copy link

jkyeung commented Aug 12, 2019

I have to say I find all this focus on indentation aids a little disconcerting. If I had found microsoft/vscode-python#6333 (or microsoft/vscode-python#6497) earlier, I would have commented there.

I had kind of thought that once the fancy continuation-line indentation was finished, we were really done with automatic indentation.

Note that in Python, this is perfectly legal:

if foo:
    if bar:
        print('foobar')
else:
    print('not foo')

One of the consequences of not having suite-ending delimiters is that it is impossible to infer, even in principle, what the "proper" indentation level is for else when there are multiple outstanding antecedent if statements. It's entirely up to the programmer to specify it. (In microsoft/vscode-python#6333, Brett cites the auto-dedent for else in Ruby. But Ruby has the end keyword, so the situation is different.)

I would appeal to the Python sensibility of refusing the temptation to guess.

Please tell me that whatever is being done with auto-dedentation of else and friends, it only happens when the keyword in question appears at the same indentation level as the previous line.

For example, let's say I've already typed the following, and the cursor is on the next line, aligned with print:

if foo:
    if bar:
        print('foobar')
        #<--cursor is here

And let's say I want an else on that 4th line. Because I am a conscientious programmer (which I believe most are, by the way), I am not going to just type else right there; I'm going to dedent first, manually. Let's say what I want to achieve is this:

if foo:
    if bar:
        print('foobar')
    else:
        print('foo')

Now, recall I said I dedented manually. Please tell me that the plan is not for automatic dedent to kick in when I type the else and move it another level to the left.

Or, let's say I want to achieve the following:

if foo:
    if bar:
        print('foobar')
else:
    print('not foo')

Again, after typing print('foobar') and Enter, I'm going to manually dedent two levels. Please reassure me that my carefully positioned else is not going to be automatically shifted to the right because some overly guessy algorithm thinks it is meant to correspond to the most recent if.

I am sort of OK with the editor automatically dedenting else one level if it isn't already dedented one or more levels. Because then the editor would at least be converting definitely-invalid code into valid-and-possibly-even-intended code. It's definitely not OK to convert already valid code into code that means something else (which is what is happening in microsoft/vscode-python#6886, and why that issue has so much attention; indeed, that is how I found this one).

@brettcannon
Copy link
Member

Note that solving this specific issue will require context-aware whitespace formatting.

@luabud luabud removed the bug Something isn't working label Sep 8, 2023
@luabud luabud transferred this issue from microsoft/vscode-python Sep 8, 2023
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Sep 8, 2023
@judej judej added bug Something isn't working and removed needs repro Issue has not been reproduced yet labels Sep 18, 2023
@debonte debonte added the P2 label Jan 3, 2024
@debonte debonte added the auto-indent Bugs in the auto indent feature label Mar 1, 2024
@debonte debonte assigned KacieKK and unassigned StellaHuang95 Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-indent Bugs in the auto indent feature bug Something isn't working P2
Projects
None yet
Development

No branches or pull requests

8 participants