jaredgrubb / fit-clang-format

Helper tool to generate a best-fit "clang-format style" for your project
MIT License
9 stars 1 forks source link

fit-clang-format

Find the best-fit clang-format rules file for a git repo.

Overview

Clang-format is an awesome tool that automatically reformats your source code according to a style file that you build (spaces anyone? or tabs?).

However, creating that style file is tedious and annoying. This Python tool iteratively tries all the options until it finds the style file that gives you the closest fit:

  1. Pick tabs vs spaces (and how much one identation level is)
  2. Pick the closest predefined top-level style (LLVM, Google, WebKit)
  3. For each of the other style options, iteratively try every possible option and pick the one that fits "closest"

Usage

$ cd YourRepo
$ fit-clang-format
   [ ... ]
$ git diff   # Like it? Or reset and re-run?

You may need to specify the path to the clang-format tool if it is not in your path:

$ fit-clang-format --clang-format-path /path/to/clang-format

The tool automatically grabs every source file (by extension) and performs reformatting and calculates the size of the delta. It tries to find the combination that is the closest fit.

After the tool completes, your repo will have a .clang-format file in the root of your repository and the files will have that format applied so you can run git-diff to see how it looks.

Trouble-Shooting and Fine-Tuning

How It Works

The scripts works by:

  1. Picking the best "base" style. There are five pre-defined styles that are tuned to match the style preferences of a few large OSS projects (Chromium, Google, LLVM, Mozilla, WebKit). The tool tries each one and picks the "best" one.
  2. Tabs or Spaces? The most basic question of style. The tool first figures out the usual indent and whether indentation is correct (space) or not (tabs).
  3. Retry the base styles. Now that indentation is decided, rerun the base check to double-check.
  4. Try all the others. Then, for each of the ~60 options, it tries each one, in series. Note that the tool caches results, so it can skip one option of each set (the default in that base).

Figuring out which style is "better" is tricky, and there are a few different algorithms implemented that you can try. Each algorithm reads the output of git-diff and calculates a score. These scores are a triplet of three values (in different orders):

License

This software is dual-licensed under MIT and LLVM licenses.