Code Monkey home page Code Monkey logo

Comments (2)

jkyeung avatar jkyeung commented on August 27, 2024

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).

from pylance-release.

brettcannon avatar brettcannon commented on August 27, 2024

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

from pylance-release.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.