|
| 1 | +--- |
| 2 | +outline: deep |
| 3 | +--- |
| 4 | + |
| 5 | +# Configure Treefmt |
| 6 | + |
| 7 | +The `treefmt.toml` configuration file consists of a mixture of global options and formatter sections: |
| 8 | + |
| 9 | +```toml |
| 10 | +excludes = ["*.md", "*.dat"] |
| 11 | + |
| 12 | +[formatter.elm] |
| 13 | +command = "elm-format" |
| 14 | +options = ["--yes"] |
| 15 | +includes = ["*.elm"] |
| 16 | + |
| 17 | +[formatter.go] |
| 18 | +command = "gofmt" |
| 19 | +options = ["-w"] |
| 20 | +includes = ["*.go"] |
| 21 | + |
| 22 | +[formatter.python] |
| 23 | +command = "black" |
| 24 | +includes = ["*.py"] |
| 25 | + |
| 26 | +# run shellcheck first |
| 27 | +[formatter.shellcheck] |
| 28 | +command = "shellcheck" |
| 29 | +includes = ["*.sh"] |
| 30 | +pipeline = "sh" |
| 31 | +priority = 0 |
| 32 | + |
| 33 | +# shfmt second |
| 34 | +[formatter.shfmt] |
| 35 | +command = "shfmt" |
| 36 | +options = ["-s", "-w"] |
| 37 | +includes = ["*.sh"] |
| 38 | +pipeline = "sh" |
| 39 | +priority = 1 |
| 40 | +``` |
| 41 | + |
| 42 | +## Global Options |
| 43 | + |
| 44 | +- `excludes` - an optional list of glob patters used to exclude certain files from all formatters. |
| 45 | + |
| 46 | +## Formatter Options |
| 47 | + |
| 48 | +- `command` - the command to invoke when applying the formatter. |
| 49 | +- `options` - an optional list of args to be passed to `command`. |
| 50 | +- `includes` - a list of glob patterns used to determine whether the formatter should be applied against a given path. |
| 51 | +- `excludes` - an optional list of glob patterns used to exclude certain files from this formatter. |
| 52 | +- `pipeline` - an optional key used to group related formatters together, ensuring they are executed sequentially |
| 53 | + against a given path. |
| 54 | +- `priority` - indicates the order of execution when this formatter is operating as part of a pipeline. Greater |
| 55 | + precedence is given to lower numbers, with the default being `0`. |
| 56 | + |
| 57 | +> When two or more formatters in a pipeline have the same priority, they are executed in lexicographical order to |
| 58 | +> ensure deterministic behaviour over multiple executions. |
| 59 | +
|
| 60 | +## Supported Formatters |
| 61 | + |
| 62 | +Here is a list of all the formatters we tested. Feel free to send a PR to add other ones! |
| 63 | + |
| 64 | +### [prettier](https://prettier.io/) |
| 65 | + |
| 66 | +An opinionated code formatter that supports many languages. |
| 67 | + |
| 68 | +```toml |
| 69 | +command = "prettier" |
| 70 | +options = ["--write"] |
| 71 | +includes = [ |
| 72 | + "*.css", |
| 73 | + "*.html", |
| 74 | + "*.js", |
| 75 | + "*.json", |
| 76 | + "*.jsx", |
| 77 | + "*.md", |
| 78 | + "*.mdx", |
| 79 | + "*.scss", |
| 80 | + "*.ts", |
| 81 | + "*.yaml", |
| 82 | +] |
| 83 | +``` |
| 84 | + |
| 85 | +### [Black](https://github.com/psf/black) |
| 86 | + |
| 87 | +A python formatter. |
| 88 | + |
| 89 | +```toml |
| 90 | +command = "black" |
| 91 | +includes = ["*.py"] |
| 92 | +``` |
| 93 | + |
| 94 | +### [clang-format](https://clang.llvm.org/docs/ClangFormat.html) |
| 95 | + |
| 96 | +A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code. |
| 97 | + |
| 98 | +```toml |
| 99 | +command = "clang-format" |
| 100 | +options = [ "-i" ] |
| 101 | +includes = [ "*.c", "*.cpp", "*.cc", "*.h", "*.hpp" ] |
| 102 | +``` |
| 103 | + |
| 104 | +Note: This example focuses on C/C++ but can be modified to use with other languages. |
| 105 | + |
| 106 | +### Elm |
| 107 | + |
| 108 | +```toml |
| 109 | +command = "elm-format" |
| 110 | +options = ["--yes"] |
| 111 | +includes = ["*.elm"] |
| 112 | +``` |
| 113 | + |
| 114 | +### Go |
| 115 | + |
| 116 | +```toml |
| 117 | +command = "gofmt" |
| 118 | +options = ["-w"] |
| 119 | +includes = ["*.go"] |
| 120 | +``` |
| 121 | + |
| 122 | +### [Ormolu](https://github.com/tweag/ormolu) |
| 123 | + |
| 124 | +Haskell formatter. Make sure to use ormolu 0.1.4.0+ as older versions don't |
| 125 | +adhere to the spec. |
| 126 | + |
| 127 | +```toml |
| 128 | +command = "ormolu" |
| 129 | +options = [ |
| 130 | + "--ghc-opt", "-XBangPatterns", |
| 131 | + "--ghc-opt", "-XPatternSynonyms", |
| 132 | + "--ghc-opt", "-XTypeApplications", |
| 133 | + "--mode", "inplace", |
| 134 | + "--check-idempotence", |
| 135 | +] |
| 136 | +includes = ["*.hs"] |
| 137 | +``` |
| 138 | + |
| 139 | +### [stylish-haskell](https://github.com/jaspervdj/stylish-haskell) |
| 140 | + |
| 141 | +Another Haskell formatter. |
| 142 | + |
| 143 | +```toml |
| 144 | +command = "stylish-haskell" |
| 145 | +options = [ "--inplace" ] |
| 146 | +includes = [ "*.hs" ] |
| 147 | +``` |
| 148 | + |
| 149 | +### [nixpkgs-fmt](https://github.com/nix-community/nixpkgs-fmt) |
| 150 | + |
| 151 | +Nix code formatter. |
| 152 | + |
| 153 | +```toml |
| 154 | +command = "nixpkgs-fmt" |
| 155 | +includes = ["*.nix"] |
| 156 | +``` |
| 157 | + |
| 158 | +### rustfmt |
| 159 | + |
| 160 | +```toml |
| 161 | +command = "rustfmt" |
| 162 | +options = ["--edition", "2018"] |
| 163 | +includes = ["*.rs"] |
| 164 | +``` |
| 165 | + |
| 166 | +### [rufo](https://github.com/ruby-formatter/rufo) |
| 167 | + |
| 168 | +Rufo is an opinionated ruby formatter. By default it exits with status 3 on |
| 169 | +file change so we have to pass the `-x` option. |
| 170 | + |
| 171 | +```toml |
| 172 | +command = "rufo" |
| 173 | +options = ["-x"] |
| 174 | +includes = ["*.rb"] |
| 175 | +``` |
| 176 | + |
| 177 | +### cargo fmt |
| 178 | + |
| 179 | +`cargo fmt` is not supported as it doesn't follow the spec. It doesn't allow |
| 180 | +to pass arbitrary files to be formatted, which treefmt relies on. Use `rustfmt` |
| 181 | +instead (which is what `cargo fmt` uses under the hood). |
| 182 | + |
| 183 | +### [shfmt](https://github.com/mvdan/sh) |
| 184 | + |
| 185 | +A shell code formatter. |
| 186 | + |
| 187 | +```toml |
| 188 | +command = "shfmt" |
| 189 | +options = [ |
| 190 | + "-i", |
| 191 | + "2", # indent 2 |
| 192 | + "-s", # simplify the code |
| 193 | + "-w", # write back to the file |
| 194 | +] |
| 195 | +includes = ["*.sh"] |
| 196 | +``` |
| 197 | + |
| 198 | +### terraform |
| 199 | + |
| 200 | +terraform fmt only supports formatting one file at the time. See |
| 201 | +https://github.com/hashicorp/terraform/pull/28191 |
0 commit comments