Wing Tips: Using Black, Ruff, YAPF, and Autopep8 Code Reformatting in Wing Python IDE

Jan 22, 2020


Wing supports automatic code reformatting with Black, Ruff, YAPF, and autopep8. Using an auto-reformatter allows you to develop nicely formatted uniform-looking code without spending time manually adjusting the layout of code.

There are two ways to go about reformatting code in Wing with any of these: (1) You can reformat whole files or the current selection on request at any time, or (2) you can reformat automatically as you edit lines of code or save edited files to disk.

Installing Reformatters

Wing uses its own copy of autopep8 for PEP 8 style formatting. If you plan to use Black, Ruff, or YAPF formatting instead, then you must first install the selected formatter into the Python that you are using with your code. For example:

pip install black
pip install ruff
pip install yapf

If you want to install with a package manager other than pip, please refer to the reformatter's documentation.

After this is done, running Python on the command line with arguments -m black or -m ruff or -m yapf should invoke the reformatter.

Manual Reformatting

The Source > Reformatting menu contains items for reformatting the current file or selection for PEP 8, Black, Ruff, or YAPF:

/images/blog/reformatting/reformat-menu.png

The result of the above operation (reformatting the selection with Black) looks like this:

/images/blog/reformatting/reformat-menu-result.png

A single Undo will undo the reformatting operation.

Automatic Reformatting

Wing can also auto-format edited lines after the caret leaves the line, or whole files as they are saved to disk. This is enabled with the Auto-Reformat property under the Options tab in Project Properties, or with the Editor > Auto-formatting > Auto-Reformat preference:

/images/blog/reformatting/reformat-automatic.png

When this is set to Lines After Edit, Wing only reformats lines that you have edited, as the editor caret leaves that line or before the file is saved. For example, using yapf as the formatter:

/images/blog/reformatting/reformat-auto-line.gif

Notice that reformatting applies to whole logical lines which, as in this case, may span more than one physical line.

If Whole Files Before Save auto-reformatting is used instead, then the whole file is reformatted before saving it to disk. For example, using Black as the formatter:

/images/blog/reformatting/reformat-auto-file.gif

Note that Wing implements some timeouts for reformatting, so that very large files do not hang up saving or other operations, and there are some options available to control the details of formatting. See Auto-Reformatting for more information.



That's it for now! We'll be back soon with more Wing Tips for Wing Python IDE.

As always, please don't hesitate to email support@wingware.com if you run into problems or have any questions.



Share this article: