Skip to content

add Topiary to “Formatting Your Code” #3097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions data/tutorials/platform/1_07_code_formatting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
id: "formatting-your-code"
title: "Formatting Your Code"
short_title: "Formatting Your Code"
description: |
How to set up formatting of your code
category: "Editor Support"
---

## Using OCamlFormat

Automatic formatting with OCamlFormat requires an `.ocamlformat` configuration
file at the root of the project.

An empty file is accepted, but since different versions of OCamlFormat will
vary in formatting, it is good practice to specify the version you're using.
Running

```shell
echo "version = `ocamlformat --version`" > .ocamlformat
```

creates a configuration file for the currently installed version of
OCamlFormat.

In addition to editor plugins that use OCamlFormat for automatic code
formatting, Dune also offers a command to run OCamlFormat to automatically
format all files from your codebase:

```shell
opam exec -- dune fmt
```

## Using Topiary

[Topiary](https://topiary.tweag.io) is a Tree-sitter-based code formatter
supporting multiple languages, including OCaml & OCamllex. It can be invoked
with

<!-- markdownlint-disable commands-show-output -->
```shell-session
$ topiary format source.ml
```
<!-- markdownlint-restore -->

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).

### Example configuration setup

This example configuration will override the default configuration to use 1 tab
character for indentation by creating a `.topiary.ncl` Nickel configuration
file.

<!-- markdownlint-disable commands-show-output -->
```shell-session
$ touch .topiary.ncl
$ $EDITOR .topiary.ncl
```
<!-- markdownlint-restore -->

<!-- markdownlint-disable no-hard-tabs -->
```nickel
{
languages = {
nickel.indent | priority 1 = "\t",
ocaml.indent | priority 1 = "\t",
ocaml_interface.indent | priority 1 = "\t",
ocamllex.indent | priority 1 = "\t",
},
}
```
<!-- markdownlint-restore -->

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.

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`.

Afterwards, `--merge-configuration` will always merge in the example
configuration. Invoke Topiary’s formatting with

<!-- markdownlint-disable commands-show-output -->
```shell-session
$ topiary format --merge-configuration source.ml
```
<!-- markdownlint-restore -->

and/or configure your editor to run this command on saving the file.
25 changes: 0 additions & 25 deletions data/tutorials/platform/1_07_ocamlformat.md

This file was deleted.