Sublime Forum

Indententation (bug?) after lines with a 'return' statement

#1

I’m having an issue with indentation. I’ve noticed that if my caret is at the end of this blob of code…

def foo(index):
    if index < 0:
        return 'something'
    return 'other thing'

…and I press return, the indentation levels stays the same at the next line, at the level of if, instead of moving back to the level of def.

Are there cases where this behavior is “desirable” or “correct”? Or is this some kind of bug/oversight?

It’s a bit annoying because the python linter wants me to put two line breaks before each new python function, and I can’t just do “return return” after finishing the previous function, I have to do “return backspace return”. (And more likely “return-return-oh-crap-I-still-have-whitespace-here-backspace-backspace-backspace-return”.)

A similar issue arises with the break statement: the next line stays at the same indendentation level, instead of going back (at least one) indentation level which, it seems to me, would be the smart behavior.

0 Likes

#2

this is a limitation of the TextMate indentation engine that ST uses - it is not possible to decrease indentation after a line, one can only decrease indentation as the line is being typed, which is less than helpful here for Python.

it can be solved with a simple macro assigned to the Enter key though, when the filetype is Python and the text preceding the caret is break or return [something].

0 Likes

#3

@kingkeith Thanks.

I imagine that writing such a context-sensitive macro is a bit above my ability level… I wouldn’t know how to program the “context” part of the macro, in particular.

Could you give us a tutorial? Would be much obliged…

0 Likes