Coder Social home page Coder Social logo

dbsplayground / whatstyle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mikr/whatstyle

0.0 1.0 0.0 189 KB

whatstyle finds a code format style that fits given source files

License: MIT License

Python 45.23% Objective-C 10.48% HTML 1.97% C 22.46% JavaScript 1.04% C++ 9.01% Java 5.01% Scala 1.07% R 0.18% Rust 3.47% Shell 0.07%

whatstyle's Introduction

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.

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.

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

For information about some useful scenarios run:

$ whatstyle.py --usage-examples

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

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.

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.

whatstyle was written by Michael Krause.

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.

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.