Wednesday, January 22, 2020

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

Wing version 7.2 has been released, so in the next couple Wing Tips we'll take a look at some of its new features.

Wing 7.2 expands the options for automatic code reformatting to include also Black and YAPF, in addition to the previously supported autopep8. Using one of these 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 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 yapf

Or if you are using Anaconda:

conda install black
conda install yapf

After this is done, running Python on the command line with arguments -m black 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 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.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...