Find file
Fetching contributors…
Cannot retrieve contributors at this time
43 lines (33 sloc) 1.17 KB
from sublime_plugin import TextCommand
from sublime import Region
from subprocess import call
from os.path import abspath, expanduser, exists, join
from StringIO import StringIO
from sys import path
# load the git submodule
extra = abspath('PythonTidy')
if not exists(join(extra, '.git')):
call(['git', 'submodule', 'init'])
call(['git', 'submodule', 'update'])
# tweak path to allow importing PythonTidy from the git submodule
path.insert(0, extra)
import PythonTidy
import PythonTidyWrapper
def setup():
xml = expanduser('~/.pythontidy.xml')
if exists(xml):
config = PythonTidyWrapper.Config(file=xml)
class python_tidy(TextCommand):
def run(self, edit):
view = self.view
region = Region(0L, view.size())
encoding = view.encoding()
if not encoding or encoding == u'Undefined':
encoding = view.settings().get('default_encoding')
source = StringIO(view.substr(region).encode(encoding))
output = StringIO()
PythonTidy.tidy_up(source, output)
view.replace(edit, region, output.getvalue().decode(encoding))