Skip to content

Commit d7fe9ec

Browse files
committed
add Topiary to “Formatting Your Code”
1 parent 5889ed5 commit d7fe9ec

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

data/tutorials/platform/1_07_code_formatting.md

+46
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,49 @@ In addition to editor plugins that use OCamlFormat for automatic code formatting
2525
```shell-session
2626
$ opam exec -- dune fmt
2727
```
28+
29+
30+
## Using Topiary
31+
32+
[Topiary](https://topiary.tweag.io) is a Tree-sitter-based code formatter supporting multiple languages, including OCaml & OCamllex.
33+
It can be invoked with
34+
35+
```shell-session
36+
$ topiary format source.ml
37+
```
38+
39+
Topiary does not require an empty configuration file to operate & has its own set of defaults, however, it can be [configured](https://topiary.tweag.io/book/cli/configuration.html).
40+
41+
### Example configuration setup
42+
43+
This example configuration will override the default configuration to use 1 tab character for indentation by creating a `.topiary.ncl` Nickel configuration file.
44+
45+
46+
```shell-session
47+
$ touch .topiary.ncl
48+
$ $EDITOR .topiary.ncl
49+
```
50+
51+
```nickel
52+
{
53+
languages = {
54+
nickel.indent | priority 1 = " ",
55+
ocaml.indent | priority 1 = " ",
56+
ocaml_interface.indent | priority 1 = " ",
57+
ocamllex.indent | priority 1 = " ",
58+
},
59+
}
60+
```
61+
62+
Then this file needs to be exported to the environment such as `export TOPIARY_CONFIG_FILE=".topiary.ncl"` in Bash/ZSH or `set -x TOPIARY_CONFIG_FILE ".topiary.ncl"` in Fish.
63+
64+
TIP: If using Direnv, the environment variable can also be added to the user’s personal `.envrc` so it is exported on switching to the project directory by appending with `echo 'export TOPIARY_CONFIG_FILE=".topiary.ncl"' >> .envrc`.
65+
66+
Afterwards, `--merge-configuration` will always merge in the example configuration.
67+
Invoke Topiary’s formatting with
68+
69+
```shell-session
70+
$ topiary format --merge-configuration source.ml
71+
```
72+
73+
and/or configure your editor to run this command on saving the file.

0 commit comments

Comments
 (0)