Source code formatter
clang-format
is a source code formatter – it changes source files according to a config file.
In NeoMutt we use it to:
{}
s in the right place#include
s (see the weighting strategy)*
to the variable, not the typeUnlike many similar tools, it really understands the code it’s transforming. It uses clang to create an AST for the code.
NeoMutt’s config file ships with the code.
The config file looks like this:
Language: Cpp
TabWidth: 8
UseTab: Never
IndentWidth: 2
ColumnLimit: 80
BreakBeforeBraces: Allman
Clang has documentation for all of the options.
Running it is as simple as:
clang-format -i source.c
// clang-format off
code here will not be formatted
// clang-format on
To run clang-format
on all the code that you’ve changed, before committing, you could run:
git diff HEAD --name-only -- '*.c' | xargs --no-run-if-empty clang-format -i