Tutorial: Auto-Editing

Index of All Documentation » Wing Pro Tutorial »


Let's revisit auto-editing, which was introduced before we tried out the debugger. So far we've seen the editor auto-enter invocation arguments and closing parentheses. There are a number of other auto-editing operations available as well:

Applying Characters to a Selection

If you select a range of text in the editor and press a quote, parenthesis, brace, bracket, or #, Wing applies that key stroke to the selection.

For example, try selecting a few lines of non-comment code and press #. Wing will comment out those lines using the comment style configured in the Editor > Block Comment Style preference. Pressing # a second time will remove the comment characters.

Also, selecting some text and pressing " (double quote) will surround it with double quotes, or pressing ( open parenthesis will surround it with parentheses. This also works when typing single quotes, triple quotes, back ticks, brackets, and braces.

Similarly, selecting a string and pressing a different quote character will convert that string to using the type of quote (either single or double quote). This also works if the caret is just after the closing quote of a string, within the opening or closing triple-quote, or one of the quotes is selected.

The : colon key can also be applied to a selection, in order to create a new block with one or more selected lines. The : is entered, the selected lines are indented following the : and the caret is positioned so that the block type can be entered. If try is entered, the corresponding except is also auto-entered and selected to make it easy to convert it to finally or enter the exception type.

These operations are on by default and may be disabled with the Editor > Auto-editing > Apply Quotes to Selection, Editor > Auto-editing > Apply Comment Key to Selection, Editor > Auto-editing > Apply [], (), and {} to Selection, and Editor > Auto-editing > Apply Colon to Selection preferences.

Auto-Entering Spacing

Wing can also auto-enter spaces as you type code, optionally enforcing PEP 8 style spacing. This auto-editing operation is off by default but can be turned on with the Editor > Auto-Editing > Auto-Enter Spaces preference. Try turning this on now and slowly typing the following into an editor:

import os
if os.environ['TEST'] == 'X' * 3:
    pass

Notice that Wing is auto-entering a space after the ], =, and other characters according to the context in the code. If you press the space anyway, it is ignored.

If you also enable the Editor > Auto-Editing > Enforce PEP 8 Style Spacing preference, Wing will try to enforce PEP 8 style spacing as you type. For example, typing the following disallows extra spaces around =:

x = 'test'

According to PEP 8, spaces should not be used in argument lists. This is also the default behavior for Wing, whether or not PEP 8 enforcement is on. To override this, enable the Editor > Auto-Editing > Spaces Around = in Argument Lists preference.

Similarly, enforcement of spacing around : in type annotations can be enabled or disabled with the Editor > Auto-Editing > Spaces Around : in Type Annotations preference.

Managing Blocks with the Colon Key

This operation automatically sets up new blocks and allows reindenting existing code under a newly added block. Try this now by typing the following into an editor:

if x == 1:

Notice that Wing auto-inserts a new line and indentation as soon as the colon is entered, so the contents of the block can be typed right away.

Now try typing the following before text = None on line 38 of example1.py, inside ReadPythonNews:

if force:

A new line and indent are added as before. Next, without moving the caret, press : again. Wing will move the first following line txt = None under the new block so it looks like this:

/images/doc/en/intro/colon1.png

Again without moving the caret press : a third time. Wing moves the entire following block, up until the next blank line or first line indented less than the current one, so it looks like this:

/images/doc/en/intro/colon2.png

Line Continuations

If you press Enter inside a comment or a string inside () and there is text after the caret, Wing auto-continues the line, placing the necessary comment or quote characters. For example, pressing Enter after the word code on the first line of example1.py results in the following:

/images/doc/en/intro/continuation.png

This is on by default and can be disabled with the Editor > Auto-Completion > Continue Comment or String on New Line preference.

Correcting Out-of-Order Typing

Wing also tries to correct out-of-order typing. For example, type the following in an editor:

def y(:)

Wing figures out that the colon is misplaced and auto-corrects this to read:

def y():

Similarly, if you type the following:

y()x

Wing figures out that a . is probably missing and auto-corrects this to read:

y().x

By relying on this, it is possible to save key strokes for caret movement when coding.

This auto-editing operation is on by default and can be disabled with the Editor > Auto-Completion > Correct Out-of-Order Typing preference.

See the Auto-editing documentation page for details.