Sublime Forum

Emacs-style indentation of Python line continuations

#1

I love ST2, but really miss the way Emacs handles the formatting of line continuations in Python. It adheres very closely to PEP 8 (see the section on “Code lay-out”). For example, here’s the result of entering code with line continuations in Emacs. I’ve done no formatting at all - this is just how Emacs indents stuff:

class Foo(object):
    def foo(self,
            arg1, arg2,
            arg3,
            arg4):
        if self.method() \
                .method2() \
                .method3():
            pass

Here’s the same code entered in ST2. Note how in each line continuation, the second and following lines are indented at the same level as their parent lines and aren’t visually distinguishable.

class Foo(object):
    def foo(self,
    arg1, arg2,
    arg3,
    arg4):
    if self.method() \
    .method2() \
    .method3():
    pass

Much more important than devotion to a style guide is that code following those continued lines - such as the “if self.method …]” and “pass” lines in my example - should be indented to form new blocks, but ST2 doesn’t indent them at all. The result is syntactically invalid Python.

I haven’t worked up the bravery to investigate the syntax definitions myself. Is it possible to implement this idea in ST2 as it stands today?

0 Likes

Sublime Text 3 Beta
#2

I also would love to have emacs indentation on sublimetext2. Not only continuations, but on other occasions emacs do the right thing were sublime does not, eg. inside brackets/parenthesis.

This is the number one thing I keep going back to emacs for.

0 Likes

#3

Hello @jps,

How could we implement this on Sublime ? Does it have to be implemented on editor (jps) level, or can it be implemented in plugin level ? If so (plugin), do you have a tip where we could look for ?

Thanks a lot

0 Likes

#4

so no one knows how to implement better automatic indentation on sublime text 2?

0 Likes

#5

I’m willing to hack on this if someone can point me in the right direction (and confirm whether it’s even possible).

0 Likes

#6

Check out the .tmPreferences files inside the Python package:

[code]<?xml version="1.0" encoding="UTF-8"?>

name Miscellaneous scope source.python settings decreaseIndentPattern ^\s*(elif|else|except|finally)\b.*: increaseIndentPattern ^\s*(class|def|elif|else|except|finally|for|if|try|with|while)\b.*:\s*$ shellVariables name TM_COMMENT_START value # name TM_LINE_TERMINATOR value : uuid 33877934-69D3-4773-8786-9B5211012A9A [/code]

I’ve never created indentation rules myself, but I doubt they’ll let you do anything too fancy. Alternatively, you can try creating a plugin.

0 Likes

#7

So has anything ever come of this? This is actually one of the things that keeps me from using Sublime on a regular basis.

0 Likes

#8

It could be done, but it would require a plugin.

0 Likes

#9

Do you know if anyone’s working on one?

0 Likes