Home Download Buy Blog Forum Support

Emacs-style indentation of Python line continuations

Emacs-style indentation of Python line continuations

Postby kstrauser on Thu Oct 06, 2011 4:24 pm

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:

Code: Select all
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.

Code: Select all
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?
kstrauser
 
Posts: 9
Joined: Tue Sep 27, 2011 8:07 pm

Re: Emacs-style indentation of Python line continuations

Postby santagada on Fri Oct 07, 2011 2:01 pm

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.
santagada
 
Posts: 5
Joined: Fri Oct 07, 2011 1:58 pm

Re: Emacs-style indentation of Python line continuations

Postby dsarch on Fri Oct 07, 2011 6:32 pm

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
dsarch
 
Posts: 62
Joined: Tue May 03, 2011 2:31 am

Re: Emacs-style indentation of Python line continuations

Postby santagada on Mon Oct 10, 2011 10:19 pm

so no one knows how to implement better automatic indentation on sublime text 2?
santagada
 
Posts: 5
Joined: Fri Oct 07, 2011 1:58 pm

Re: Emacs-style indentation of Python line continuations

Postby kstrauser on Tue Oct 11, 2011 2:57 pm

I'm willing to hack on this if someone can point me in the right direction (and confirm whether it's even possible).
kstrauser
 
Posts: 9
Joined: Tue Sep 27, 2011 8:07 pm

Re: Emacs-style indentation of Python line continuations

Postby guillermooo on Tue Oct 11, 2011 3:45 pm

Check out the .tmPreferences files inside the Python package:

Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>name</key>
   <string>Miscellaneous</string>
   <key>scope</key>
   <string>source.python</string>
   <key>settings</key>
   <dict>
      <key>decreaseIndentPattern</key>
      <string>^\s*(elif|else|except|finally)\b.*:</string>
      <key>increaseIndentPattern</key>
      <string>^\s*(class|def|elif|else|except|finally|for|if|try|with|while)\b.*:\s*$</string>
      <key>shellVariables</key>
      <array>
         <dict>
            <key>name</key>
            <string>TM_COMMENT_START</string>
            <key>value</key>
            <string># </string>
         </dict>
         <dict>
            <key>name</key>
            <string>TM_LINE_TERMINATOR</string>
            <key>value</key>
            <string>:</string>
         </dict>
      </array>
   </dict>
   <key>uuid</key>
   <string>33877934-69D3-4773-8786-9B5211012A9A</string>
</dict>
</plist>


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.
guillermooo
 
Posts: 728
Joined: Thu Jul 23, 2009 9:06 am

Re: Emacs-style indentation of Python line continuations

Postby joshourisman on Thu Dec 22, 2011 5:04 pm

So has anything ever come of this? This is actually one of the things that keeps me from using Sublime on a regular basis.
joshourisman
 
Posts: 2
Joined: Thu Dec 22, 2011 5:02 pm

Re: Emacs-style indentation of Python line continuations

Postby aparajita on Fri Dec 23, 2011 2:23 am

It could be done, but it would require a plugin.
aparajita
 
Posts: 177
Joined: Sun Jul 24, 2011 10:42 pm

Re: Emacs-style indentation of Python line continuations

Postby joshourisman on Fri Dec 23, 2011 4:56 pm

Do you know if anyone's working on one?
joshourisman
 
Posts: 2
Joined: Thu Dec 22, 2011 5:02 pm


Return to Ideas and Feature Requests

Who is online

Users browsing this forum: emjay, Google [Bot], Yahoo [Bot] and 7 guests