by dmitriid · 3y ago · 0 comments
Just because you paste a bunch of code after `then` in Python doesn't mean all of it goes into `then`.
It's not rocket science, just following the language indentation rules for defining blocks. If the language parser can do it, why not the text editor?
Will it? Are you sure?
> Just copy it to the new position, and change the indentation level to that of the target position, maintaining the same block definition.
"Just" copy. "Just" manually re-indent it.
Remember, we're talking about an IDE automatically figuring out proper indentation for something.
> If the language parser can do it, why not the text editor?
Because constructs that are valid from the compiler's point of view may be ambiguous fro the program's point of view: https://news.ycombinator.com/item?id=34235121
> Will it? Are you sure?
Yes, it's defined as part of Python specification. https://docs.python.org/3/reference/lexical_analysis.html#in...
Indentation increasing or decreasing generates INDENT and DEDENT tokens, which are used as block delimiters.
> Remember, we're talking about an IDE automatically figuring out proper indentation for something.
The IDE doesn't need to figure out whether the program is correct. Only has to treat code blocks as defined by the language syntax. In your linked example:
if x > 0:
function1()
function2()
function3()
if x > 0:
function1()
function2()
function3()
The IDE should treat the blocks as if defined with delimiters this way: if x > 0: {
function1()
function2()
function3()
}
if x > 0: {
function1()
function2()
}
function3()
Because that's how Python will interpret them. So, the IDE would do exactly the same behavior with curly bracket delimiters and with changes in indentation, because in both cases there is a precise rule to define where blocks start and end.Exactly. It treats these blocks as such. And it will inevitably format them incorrectly from the programmer's point of view.
> because in both cases there is a precise rule to define where blocks start and end.
And this precise rule inevitably formats code incorrectly from the programmer's point of view in a significant amount of cases.