Skip to content

ClangFormat

Oren edited this page Dec 21, 2015 · 1 revision

ClangFormat

Code must be formatted with ClangFormat before a pull request is accepted.

There are several options to do this, and you must choose at least one:

pre-push script

Run this script before sending a pull request.

Usage:

From the WinObjC repo root:

For Powershell:

.\tools\scripts\pre-push.ps1 [upstream]

For Git bash:

./tools/scripts/pre-push [upstream]

upstream - specify the upstream branch to diff against. The changed files in the diff will be formatted with clang format.

For example,

.\tools\scripts\pre-push.ps1 master

If no upstream is specified by argument, the script will try to auto-detect which branch is set as the upstream in git and use that.

If files were formatted, you will need to restage and recommit the files after running this script.

pre-commit hook

This is a hook that will run clang-format on all staged files before each commit.

Install:

Copy this file: \tools\scripts\pre-commit

To the directory: .git\hooks\

Usage:

Stage files to commit and run "git commit." If there are files that were formatted, you will need to review, restage, and recommit the files.

Visual Studio plugin

This is a plugin for Visual Studio that allows running clang format on specific sections of code.

Install:

  1. Close Visual Studio

  2. Install Visual Studio plugin from this page http://llvm.org/builds/

  3. Open Visual Studio

Usage

  1. Open an objective-c file

  2. Select the code to be formatted and either go to tools > ClangFormat, or Hit CTRL-R, then CTRL-F

Sublime

Sublime can be set up to clang format on each file save.

See this page for more details: https://packagecontrol.io/packages/Clang%20Format

Settings for this package look like: Preferences > Package Settings > Clang Format > User specific

{
    "binary": "C:\\Path\\to\\WinObjC\\msvc\\LLVM-3.6.0\\bin\\clang-format.exe",
    "format_on_save": true,
    "style": "File"
}