Skip to content

Commit 37591b4

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

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

data/tutorials/platform/1_07_code_formatting.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,64 @@ 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+
55+
<!-- markdownlint-disable commands-show-output -->
56+
```shell-session
57+
$ touch .topiary.ncl
58+
$ $EDITOR .topiary.ncl
59+
```
60+
<!-- markdownlint-restore -->
61+
62+
<!-- markdownlint-disable no-hard-tabs -->
63+
```nickel
64+
{
65+
languages = {
66+
nickel.indent | priority 1 = "\t",
67+
ocaml.indent | priority 1 = "\t",
68+
ocaml_interface.indent | priority 1 = "\t",
69+
ocamllex.indent | priority 1 = "\t",
70+
},
71+
}
72+
```
73+
<!-- markdownlint-restore -->
74+
75+
Then this file needs to be exported to the environment such as `export
76+
TOPIARY_CONFIG_FILE=".topiary.ncl"` in Bash/ZSH or `set -x TOPIARY_CONFIG_FILE
77+
".topiary.ncl"` in Fish.
78+
79+
TIP: If using Direnv, the environment variable can also be added to the user’s
80+
personal `.envrc` so it is exported on switching to the project directory by
81+
appending with `echo 'export TOPIARY_CONFIG_FILE=".topiary.ncl"' >> .envrc`.
82+
83+
Afterwards, `--merge-configuration` will always merge in the example
84+
configuration. Invoke Topiary’s formatting with
85+
86+
87+
<!-- markdownlint-disable commands-show-output -->
88+
```shell-session
89+
$ topiary format --merge-configuration source.ml
90+
```
91+
<!-- markdownlint-restore -->
92+
93+
and/or configure your editor to run this command on saving the file.

0 commit comments

Comments
 (0)