|
| 1 | +--- |
| 2 | +outline: deep |
| 3 | +--- |
| 4 | + |
| 5 | +# format-plugin |
| 6 | + |
| 7 | +[format-plugin](https://github.com/xmake-addons/format-plugin) provides the `xmake format` |
| 8 | +command, which formats the sources of your project with clang-format. |
| 9 | + |
| 10 | +| Payload | What it provides | |
| 11 | +| --- | --- | |
| 12 | +| `plugins/format` | the `xmake format` command | |
| 13 | + |
| 14 | +It formats exactly the files your targets own, so generated code and third-party sources |
| 15 | +outside of the targets are never touched. `clang-format` is installed from xmake-repo when it |
| 16 | +is not on the host. |
| 17 | + |
| 18 | +::: tip NOTE |
| 19 | +This command used to be built into xmake and now ships as an addon. The builtin one still |
| 20 | +works but is deprecated: it prints a notice pointing at the addon, and an installed addon |
| 21 | +takes over the command. |
| 22 | +::: |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +```sh |
| 27 | +$ xmake addon --install format-plugin |
| 28 | +``` |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +```sh |
| 33 | +$ xmake format # all default targets |
| 34 | +$ xmake format target1 target2 # only the given targets |
| 35 | +$ xmake format -a # all targets |
| 36 | +$ xmake format -g test # a target group, `test_*` patterns work too |
| 37 | +$ xmake format --files='src/**.c|excluded.c' |
| 38 | +$ xmake format --create --style=Google # write a .clang-format |
| 39 | +``` |
| 40 | + |
| 41 | +| Option | Default | Description | |
| 42 | +| --- | --- | --- | |
| 43 | +| `-s, --style` | | the path of a `.clang-format` file, or a builtin style: `LLVM`, `Google`, `Chromium`, `Mozilla`, `WebKit` | |
| 44 | +| `--create` | | create a `.clang-format` from the given style | |
| 45 | +| `-n, --dry-run` | | do not change anything, just list the files which would be formatted | |
| 46 | +| `-e, --error` | | turn the formatting warnings into errors | |
| 47 | +| `-j, --jobs` | cpu cores | the number of parallel format jobs | |
| 48 | +| `-a, --all` | | format all targets | |
| 49 | +| `-g, --group` | | format the targets of the given group | |
| 50 | +| `-f, --files` | | the given source files, `**` and `|` exclusion are supported | |
| 51 | + |
| 52 | +## Checking the format in the ci |
| 53 | + |
| 54 | +`--dry-run` changes nothing, and with `--error` the command fails when a file does not match |
| 55 | +the style: |
| 56 | + |
| 57 | +```sh |
| 58 | +$ xmake format --dry-run --error |
| 59 | +``` |
0 commit comments