Skip to content

Commit 8218789

Browse files
committed
Add Topiary to “Formatting Your Code”
1 parent b906ac2 commit 8218789

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

data/tutorials/platform/1_07_code_formatting.md

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

0 commit comments

Comments
 (0)