May 05, 2014One way in which an IDE speeds up software development is through auto-editing operations. A common example found in almost every text editor is auto-entering ')' after the caret when '(' is pressed.
Wing IDE Professional takes this quite a bit further with a suite of operations that speed up the process of editing Python code. Some of these are disabled by default because they can be disconcerting to new users, but they are well worth exploring. This article explains how to use some of the more advanced auto-editing operations that Wing IDE Pro provides for Python.
If you want to try out each auto-editing operation while reading this, install Wing IDE Pro and Python and go into the Edit > Keyboard Personality menu and Configure Auto-Editing to turn on all the auto-editing operations.
Auto-Enter Invocation Args
This operation causes Wing to automatically insert function or method arguments when an invocation is started, so that you can more easily enter your invocation. For example, when you type getattr( Wing looks up the type of getattr and automatically places the arguments for it. You would end up with getattr(object, name, default=None) in the editor, with the arguments selected.
At this point you can either over-type the selected arguments or tab through them to enter each one. For keyword arguments, tab order visits first the whole argument and then just the value. When you exit the invocation, Wing automatically removes any unaltered default values.
This operation also works if you are entering a def for a method that overrides an inherited method, so that the same arguments that exist in the inherited method are placed automatically.
If you tend to use uniform local variable and function/method argument names then the auto-entered arguments are often all you need. For example, typing self.MyMethod( might get you an invocation like self.MyMethod(path, filename, extend) that works as-is if the locals path, filename, and extend are defined. This can save a lot on typing and reduces opportunities for mistakes.
Here is an example of this feature in action:
Configuring the Python PathAuto-invocation and many of Wing's other features rely on the IDE's analysis of your source code and the code that you import. If Wing cannot find some of the imported modules, it cannot offer editing assistance for them. In most cases, modules are installed into standard locations or Wing is able to read modifications to sys.path from your code, so this is not an issue. However, in some cases you may need add to the Python Path in Wing's Project Properties, accessed from the Project menu.
Manage Blocks with Repeated Colon Presses
A Few Words on IndentationWhile I'm on this topic, let me say a few things about indentation. Wing's default behavior for the Tab key is context-dependent. It does different things at the beginning, middle, and end of a line, when there is a non-empty selection, when the auto-completer is active, and if you're entering a snippet or auto-invocation. This lets you do things like selecting a block of code and toggling its indentation between syntactically valid levels by pressing Tab repeatedly. The behavior of the tab key can be changed with the User Interface > Keyboard > Tab Key Action preference.
Wing also adjusts the indentation of pasted lines. If it gets it wrong, just Undo once to remove the indentation change.
You can convert indentation style in files with the Indentation tool in the Tools menu, there are other indentation operations in the Source > Indentation menu, and there are preferences for adjusting how Wing approaches indentation in the Editor > Indentation preferences group.
When this operation is enabled, placing the caret above a block of existing code and typing if x: causes Wing to automatically place a newline and indent to prepare for input of a new line of code.
Pressing ':' a second time in this context, reformats the code so that the first line of code following the new block is placed under the conditional.
Pressing ':' a third time instead places the whole following block of contiguous lines under the conditional. Reformatting stops at any blank line or indentation decrease.
Here is an example of this in action:
This auto-editing operation also works with for, try, except, else, while, with, and so forth -- anything that starts a new block.
Applying Keys to Selections
Another way to create new blocks of code is to select a range of lines and press the ':' key. This indents the existing lines and starts a new block. Now you can type if, for, or whatever other block type you want to create. Wing auto-inserts except when try is typed and selects except so it can be edited easily. You can press Tab to move into the except (or finally) block.
You can also apply other keys to selections, such as '(', '[', or '[' to surround text with '()', '', or '', press quotes to create strings, or '#' to toggle whether something is commented out. These can be enable or disabled independently from the Editor > Auto-Editing preferences group.
Here this an example of this:
This auto-editing operation is also off by default since it can take some getting used to. When enabled with the Editor > Auto-Editing > Auto-Enter Spaces preference, Wing automatically inserts spacing in a way that conforms to PEP8 style spacing. For example, if you type x=1, Wing will enter x = 1.
With the Enforce PEP8 Style Spacing sub-option enabled, Wing protects against adding extra spaces when you press the space bar.
The Spaces in Argument Lists sub-option lets you override whether to use PEP8 style spaces for arguments. That's probably the most contentious part of PEP8, so we provided an separate option for it.
Notice that the auto-completer also places spacing when this auto-editing operation is enabled. Together with Wing's auto-indent feature and the fact that Wing matches indentation style in existing files, the user is freed most of the time from having to worry about spacing.
Here is an example of this feature in action:
Correct Out-of-Order Typing
When this auto-editing operation is enabled, Wing recognizes some types of out-of-order typing and adjusts them automatically. For example, typing def x(a,b:) enters def x(a, b): plus a new line and indentation. Similarly x(,) is converted to x(),
Here is an example of these operations:
That's it for now. Next time we'll take a detailed look at Wing's autocompleter and Source Assistant.
We're continuously adding to Wing's auto-editing capabilities. If you have auto-editing features you would like to see, or have any questions about the existing features, please email us at email@example.com.
Share this article: