Skip to main content

whatstyle

whatstyle finds a code format style that fits given source files.

Code formatters like clang-format or uncrustify usually need a specific style definition how to reformat the code. This program looks at your source code and generates a style definition that the reformatted source fits its original formatting as closely as possible.

It should help programmers to begin using a formatting tool right away without the need to invest hours of reading the formatting tool documentation.

The currently supported formatters are clang-format, YAPF, Tidy, indent, Artistic Style, Uncrustify, scalariform, scalafmt, rfmt and rustfmt.

How to run

First you choose one or more of your source files whose style you find representative of the style you want to keep. The source files should cover a wide range of language constructs and to keep runtime down not exceed a few thousand lines if possible.

You specify the formatter and source files as follows:

$ whatstyle.py -f clang-format tests/examples/gumbo-parser/utf8.c

whatstyle will then try different options while reporting intermediate results. After a while you get back a result like this:

### This style was chosen for your .clang-format - it perfectly matches your sources.
BasedOnStyle: Google
AlignAfterOpenBracket: DontAlign
SpaceAfterCStyleCast: true

Adding the option --mode resilient will usually add more options to your style so that a heavily out of shape version of your sources can be better retransformed into your style.

Style difference effects vs options documentation

Reading the documention of the invidual options of a formatter takes time and does not necessarily make clear how an option influences the formatting. You can try something like this instead:

$ whatstyle.py --variants tests/examples/xv6/printf.c

First the best matching style is chosen and then every option is replaced or augmented by every possible value. All combinations that actually made a difference are grouped and displayed side by side. The variant on the left is the original from the best style, on the right is another option setting that usually makes things worse. Below the option values, differing code fragments are shown, you can use --numhunks to show more diff hunks per variation. Use --ansi to display the variants table in an ANSI terminal, --html to open it in a browser or --ansihtml for a darker look in a browser.

Format Style Variants

Usage scenarios

For information about some useful scenarios run:

$ whatstyle.py --usage-examples

or read the text at the beginning of whatstyle.py.

Requirements

whatstyle needs at least Python 2.7 and it works as well with Python 3.2, 3.3, 3.4 and 3.5. Jython and pypy are supported.

Also whatstyle needs at least one code formatter in the current PATH.

The presence of either diff or git is optional but useful because the diff quality of both of them may be better than Python’s difflib and this results in a different and usually better matching style.

This program should work on OS X, Windows, Linux, FreeBSD and OpenBSD.

How it works

The program basically works by reformatting the source with many combinations of options and running a diff between the original and the reformatted source code.

First the major standard styles (e.g. WebKit, GNU, LLVM) are evaluated and the closest one chosen as a baseline.

Successively every choice of every style option is added to test if the additional option further reduces the differences. When no more option settings can improve the result the most appropriate style has been found.

Among a number of candidate styles with the same diff quality the one with the least number of explicit options is chosen to keep the style definition brief.

Author

whatstyle was written by Michael Krause.

License

whatstyle is available under the MIT license. See the LICENSE file for more info.

The project specific Open Source licenses of the source codes in tests/examples are present in their respective directories.

See also

Unformat: Python3 utility to generate a .clang-format file from example code-base.

Metadata

Release files for whatstyle 0.1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for whatstyle 0.1.9
File Size Uploaded Provenance Actions
whatstyle-0.1.9.tar.gz 183.9 kB Without provenance Details for whatstyle-0.1.9.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for whatstyle 0.1.9
File Interpreter ABI Platform Actions
whatstyle-0.1.9-py2.py3-none-any.whl Python 3, Python 2 none any Details for whatstyle-0.1.9-py2.py3-none-any.whl

Total release size: 252.8 kB

Release files / whatstyle-0.1.9.tar.gz

Download URL whatstyle-0.1.9.tar.gz
Size 183.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9866e896f59c0f8181330a8ad762bfb376a5709eb11fc0ec24d156c72d7764fa
BLAKE2b-256 checksum
How to use checksums
051bcc74177cfdd21a78fda1b6d9b60e93f2e46df26aaab989aa6e7be5bd799d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.1

Release files / whatstyle-0.1.9-py2.py3-none-any.whl

Download URL whatstyle-0.1.9-py2.py3-none-any.whl
Size 68.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b5c35464ddfbc3961274392bff93a8a899fc47abcfe0db17bb4c48e0ee690dff
BLAKE2b-256 checksum
How to use checksums
30885bdfe65fe1565448502579fd6e1456314da7437b94e9441385672d06e4e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.22.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.1

Security for whatstyle 0.1.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.9 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page