2016-10-07 21:03:03 +00:00
|
|
|
# Using clang-format on C++ Code
|
2016-10-04 21:27:22 +00:00
|
|
|
|
|
|
|
[`clang-format`](http://clang.llvm.org/docs/ClangFormat.html) is a tool to
|
|
|
|
automatically format C/C++/Objective-C code, so that developers don't need to
|
2016-10-07 21:03:03 +00:00
|
|
|
worry about style issues during code reviews.
|
2016-10-04 21:27:22 +00:00
|
|
|
|
2016-10-07 21:03:03 +00:00
|
|
|
It is highly recommended to format your changed C++ code before opening pull
|
|
|
|
requests, which will save you and the reviewers' time.
|
|
|
|
|
|
|
|
You can install `clang-format` and `git-clang-format` via
|
|
|
|
`npm install -g clang-format`.
|
2016-10-04 21:27:22 +00:00
|
|
|
|
|
|
|
To automatically format a file according to Electron C++ code style, simply run
|
2016-10-07 21:03:03 +00:00
|
|
|
`clang-format -i path/to/electron/file.cc`. It should work on macOS/Linux/Windows.
|
2016-10-04 21:27:22 +00:00
|
|
|
|
|
|
|
The workflow to format your changed code:
|
|
|
|
|
2016-10-07 21:03:03 +00:00
|
|
|
1. Make codes changes in Electron repository.
|
2016-10-04 21:27:22 +00:00
|
|
|
2. Run `git add your_changed_file.cc`.
|
|
|
|
3. Run `git-clang-format`, and you will probably see modifications in
|
2016-10-07 21:03:03 +00:00
|
|
|
`your_changed_file.cc`, these modifications are generated from `clang-format`.
|
2016-10-04 21:27:22 +00:00
|
|
|
4. Run `git add your_changed_file.cc`, and commit your change.
|
2016-10-07 21:03:03 +00:00
|
|
|
5. Now the branch is ready to be opened as a pull request.
|
2016-10-04 21:27:22 +00:00
|
|
|
|
|
|
|
If you want to format the changed code on your latest git commit (HEAD), you can
|
|
|
|
run `git-clang-format HEAD~1`. See `git-clang-format -h` for more details.
|
|
|
|
|
2016-10-07 21:03:03 +00:00
|
|
|
## Editor Integration
|
2016-10-04 21:27:22 +00:00
|
|
|
|
2016-10-07 21:03:03 +00:00
|
|
|
You can also integrate `clang-format` directly into your favorite editors.
|
|
|
|
For further guidance on setting up editor integration, see these pages:
|
2016-10-04 21:27:22 +00:00
|
|
|
|
|
|
|
* [Atom](https://atom.io/packages/clang-format)
|
|
|
|
* [Vim & Emacs](http://clang.llvm.org/docs/ClangFormat.html#vim-integration)
|
2017-05-12 16:40:46 +00:00
|
|
|
* [Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=xaver.clang-format)
|