-
Notifications
You must be signed in to change notification settings - Fork 678
Code Formatting
New in PTVS 2.0
The code formatting feature enables you to quickly reformat either a document of Python code or just a selection to match your preconfigured formatting options. By default the code formatting options are set to match a superset of the PEP 8 style guide.
You can access the code formatting feature from Edit -> Advanced -> Format Document or Edit -> Advanced -> Format Selection. They are also typically bound to a keyboard shortcut - typically this is bound to Ctrl-K, Ctrl-D for reformat document and Ctrl-K, Ctrl-F for reformat selection.
You can configure formatting options under Tools -> Options-> Text Editor -> Python -> Formatting.
Currently there is one general options page and three high level categories of options for automatic reformatting. The general options page controls when formatting is applied, while the other pages control what formatting is performed.
The various formatting options are broken down into Spacing, Statements, and Wrapping. Spacing controls where spaces are inserted or removed around various language constructs, Statements controls automatic re-writing of various statements into more Pythonic forms, and Wrapping controls automatic wrapping of comments to limit text width.
Spacing options have 3 possible values: On, which ensures that spacing is present. Off, which removes any spacing, and Indeterminate, which leaves the original formatting in place.
If checked, a space is added between the name and opening parenthesis of the bases list. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
class X (object): pass
Example off:
class X(object): pass
If checked, a space is added after the open parenthesis and before the close parenthesis of a bases list. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
class X( object ): pass
Example off:
class X(object): pass
If checked, a space is added after the open parenthesis and before the close parenthesis of an empty bases list. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
class X( ): pass
Example off:
class X(): pass
If checked, a space is added between the name and opening parenthesis of the parameter list. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
def X (): pass
Example off:
def X(): pass
If checked, a space is added after the open parenthesis and before the close parenthesis of a parameter list. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
def X( a, b ): pass
Example off:
def X(a, b): pass
If checked, a space is added after the open parenthesis and before the close parenthesis of an empty parameter list. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
def X( ): pass
Example off:
def X(): pass
If checked, a space is added before and after '=' operators in function definitions. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
def X(a = 42): pass
Example off:
def X(a=42): pass
If checked, a space is added before and after '->' operators in function definitions. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
def X() -> 42: pass
Example off:
def X()->42: pass
If checked, a space is added before and after binary operators. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
a + b
Example off:
a+b
If checked, a space is added before and after '=' operators in assignments. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
a = b
Example off:
a=b
If checked, a space is added between the name and opening parenthesis of the argument list. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
X ()
Example off:
X()
If checked, a space is added after the open parenthesis and before the close parenthesis of an empty argument list. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
X( )
Example off:
X()
If checked, a space is added after the open parenthesis and before the close parenthesis of an argument list. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
X( a, b )
Example off:
X(a, b)
If checked, a space is added after the open parenthesis and before the close parenthesis of an expression. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
( a )
Example off:
(a)
If checked, a space is added after the open parenthesis and before the close parenthesis of an empty tuple. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
( )
Example off:
()
If checked, a space is added after the open parenthesis and before the close parenthesis of the tuple. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
( a, b )
Example off:
(a, b)
If checked, a space is added between the open square bracket and the close square bracket. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
[ ]
Example off:
[]
If checked, a space is added after the open square bracket and before the close square bracket of the list. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
[ a, b ]
Example off:
[a, b]
If checked, a space is added before an open square bracket. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
x [i]
Example off:
x[i]
If checked, a space is added after the open square bracket and before the close square bracket. If unchecked, spaces are removed. Otherwise, spaces are not modified.
Example on:
x[ i ]
Example off:
x[i]
If checked, import statements with multiple modules are separated onto individual lines. If unchecked, import statements with multiple modules are not modified.
Before:
import sys, pickle
After:
import sys
import pickle
If checked, semicolons at the end of lines will be removed. If unchecked, unnecessary semicolons are not modified.
Before:
x = 42;
After:
x = 42
If checked, statements separated by semicolons are moved onto individual lines. If unchecked, lines with multiple statements are not modified.
Before:
x = 42; y = 100
After:
x = 42
y = 100
If checked, comments are wrapped to the specified width. If unchecked, comments are not modified.
Wrapped to 40 columns:
# There should be one-- and preferably
# only one --obvious way to do it.
Not wrapped:
# There should be one-- and preferably only one --obvious way to do it.
The number of the last column that should include comment text. Words after this column are moved to the following line.
This command will reflow comment text and format it:
# foo
# bar
# baz
changes to:
# foo bar baz
and turns:
# This is a very long long long long long long long long long long long long long long long long long long long comment
into:
# This is a very long long long long long long long long long long long long
# long long long long long long long comment
- Documentation on docs.microsoft.com
- PTVS Project
- Development topics
- Additional resources
- wfastcgi (on PyPI)
- Video index; note that some videos are outdated.