PythonFormat is a python formatting plugin for Sublime Text 2.
- Turns
"
double quotes"
into'
single quotes'
where possible - Adds spacing around mathematical operators.
print 2+-4/3
will becomeprint 2 + -4 / 3
- Adds spacing around syntactically valid statements.
do_method(arg1,arg2,arg3)
will becomedo_method(arg1, arg2, arg3)
if(True):
will becomeif (True):
print'Hello, World'
will becomeprint 'Hello, World'
- Adds parentheses around
if
,elif
, andwhile
statements - Intelligently wraps lines enclosed in parentheses/braces/brackets
- Intelligently wraps comments
- Enforces a maximum of 2 new lines in a row
- Trims excess white space
Clone this repository in to the Sublime Text 2 "Packages" directory, which is located wherever the "Preferences" -> "Browse Packages" option in sublime takes you. A restart of Sublime may be necessary.
The default key binding is super+shift+f
. Alternatively, you can do open the command pallette via super+shift+p
and enter "Format: Python". This will format your entire opened file. Currently, there is no support for formatting of a highlighted selection.
I have another Sublime Text 2 plugin, called SublimePythonTidy. It is based on the formatting provided by the excellent PythonTidy script.
SublimePythonTidy will be more reliable and less likely to screw up your code, as it uses a more mature, better tested formatter. SublimePythonTidy works by parsing the abstract syntax tree of your Python code. As such, it requires the file being formatted to be valid Python syntax. If you forgot to insert a colon or some other symbol, SublimePythonTidy will not be able to run. PythonFormat, on the other hand, will still make an attempt at formatting your code, even if it is not valid Python syntax.
Here is a sample of how the formatting style compares (I personally happen to prefer the style used by PythonFormat).
if self.opts.max_preserve_newlines == 0 or self.opts.max_preserve_newlines > self.n_newlines:
if (self.opts.max_preserve_newlines == 0 or
self.opts.max_preserve_newlines > self.n_newlines):
if self.opts.max_preserve_newlines == 0 \
or self.opts.max_preserve_newlines \
> self.n_newlines:
self.operators = ['!=', '%', '&', '*', '**', '+', '+=', '-=', '-', '/','//', '<', '<<', '<=', '~', '==', '=', '>', '>=', '>>','^', '|', '<>', '*=', '/=', '%=', '**=', '//=', '|=','&=', '^=']
self.operators = ['!=', '%', '&', '*', '**', '+', '+=', '-=', '-', '/',
'//', '<', '<<', '<=', '~', '==', '=', '>', '>=', '>>',
'^', '|', '<>', '*=', '/=', '%=', '**=', '//=', '|=',
'&=', '^=']
self.operators = [
'!=',
'%',
'&',
'*',
'**',
'+',
'+=',
'-=',
'-',
'/',
'//',
'<',
'<<',
'<=',
'~',
'==',
'=',
'>',
'>=',
'>>',
'^',
'|',
'<>',
'*=',
'/=',
'%=',
'**=',
'//=',
'|=',
'&=',
'^=',
]
PythonFormat has been tested, but it is still very new! There is a real chance it could screw up your files, so be careful, test, and save a backup.