Skip to content

Commit bed1873

Browse files
committed
Add Topiary to “Formatting Your Code”
1 parent 3685937 commit bed1873

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

data/tutorials/platform/1_07_code_formatting.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,62 @@ format all files from your codebase:
3030
```shell
3131
opam exec -- dune fmt
3232
```
33+
34+
## Using Topiary
35+
36+
[Topiary](https://topiary.tweag.io) is a Tree-sitter-based code formatter
37+
supporting multiple languages, including OCaml & OCamllex. It can be invoked
38+
with
39+
40+
```shell-session
41+
$ topiary format source.ml
42+
```
43+
44+
Topiary does not require an empty configuration file to operate & has its own
45+
set of defaults, however, it can be
46+
[configured](https://topiary.tweag.io/book/cli/configuration.html).
47+
48+
### Example configuration setup
49+
50+
This example configuration will override the default configuration to use 1 tab
51+
character for indentation by creating a `.topiary.ncl` Nickel configuration
52+
file.
53+
54+
<!-- markdownlint-disable commands-show-output -->
55+
```shell-session
56+
$ touch .topiary.ncl
57+
$ $EDITOR .topiary.ncl
58+
```
59+
<!-- markdownlint-restore -->
60+
61+
<!-- markdownlint-disable no-hard-tabs -->
62+
```nickel
63+
{
64+
languages = {
65+
nickel.indent | priority 1 = "\t",
66+
ocaml.indent | priority 1 = "\t",
67+
ocaml_interface.indent | priority 1 = "\t",
68+
ocamllex.indent | priority 1 = "\t",
69+
},
70+
}
71+
```
72+
<!-- markdownlint-restore -->
73+
74+
Then this file needs to be exported to the environment such as `export
75+
TOPIARY_CONFIG_FILE=".topiary.ncl"` in Bash/ZSH or `set -x TOPIARY_CONFIG_FILE
76+
".topiary.ncl"` in Fish.
77+
78+
TIP: If using Direnv, the environment variable can also be added to the user’s
79+
personal `.envrc` so it is exported on switching to the project directory by
80+
appending with `echo 'export TOPIARY_CONFIG_FILE=".topiary.ncl"' >> .envrc`.
81+
82+
Afterwards, `--merge-configuration` will always merge in the example
83+
configuration. Invoke Topiary’s formatting with
84+
85+
<!-- markdownlint-disable commands-show-output -->
86+
```shell-session
87+
$ topiary format --merge-configuration source.ml
88+
```
89+
<!-- markdownlint-restore -->
90+
91+
and/or configure your editor to run this command on saving the file.

0 commit comments

Comments
 (0)