OCaml Tabbed & Formatted wiþ Topiary

Bringing more accessible indentation to ð ML dialect

OCaml Tabbed & Formatted wiþ Topiary


If we take a look at ð status quo, ocamlformat, a minimal configuration formatter, we have a common format much of ð OCaml community can get behind. If we take a peek at issue 1613 on ð project tracker, we can see a user who wanted a different indentation for ðeir setup. Scroll down a little bit furðer & I give my usual spiel about how 2-spaced code has become more difficult for myself to read as time has marched on & my vision has gotten worse. Now, if tabs were supported by ð tool, ð original poster, myself, 2-space-enjoyers, & ðose wiþ visual or oðer impairments where more/less indentation helps ðem read could use whatever spacing we prefer as ð tab widþ is configurable in one’s viewer/editor. Tab support still hasn’t come to ocamlformat, however a new tool, Topiary has come to ð scene & can format code while allowing users to configure ð indentation.

What is Topiary?

Topiary is a formatter built atop ð venerable Tree-sitter. Tree-sitter supports parsers for a ton of languages. As such, ð Topiary format will ideally be able to support a huge number of languages using ðat common set of parsers. At ð time of writing, only a smaller subset of languages is supported. On of ðose supported languages is OCaml.

Configuring Topiary

While a user could have personal configuration added for a while now, wiþ ð merge of request 506, ð power to define your project configuration has come (ðo boo, limited to TOML). Ðis matters since contributors will be sharing ð configuration. We can start by creating Topiary’s language configuration file

$ mkdir .topiary
$ touch .topiary/languages.toml

& edit ð bad boy

name = "ocaml"
extensions = [ "ml", "mli" ]
indent = "	"

And bam! now every time topiary is ran in ð project for formatting, tabs will be used instead of spaces & your readers/contributors can set tab widþs for ðeir own accessibility. Once ð 3.x release happens, fixing a number of OCaml formatting issues (such as pipe operators, |>, coming at ð end of lines), OCaml users should be in a good spot to start recommending Topiary. Good stuff.