Skip to content

Ocamlformat

rudihorn edited this page Apr 9, 2019 · 5 revisions

Ocamlformat has not officially been adopted by the links project. This is simply a guide to introduce it.

Installation

Install Ocamlformat using opam install ocamlformat

Emacs setup:

Add the opam site-lisp directory to your load path, then load the ocamlformat file.

(push "~/.opam/4.07.0/share/emacs/site-lisp/" load-path)
(require 'ocamlformat)

Usage

Formatting the document

Commandline

Just call ocamlformat <file-name> to format a file.

It is possible to use the script tools/rules/ocamlformat_check.skip fix to apply to all files.

Emacs

Use the ocamlformat command (invoke using Alt-q).

It is also possible to use ocamlformat-write-on-save to have ocamlformat run automatically.

Configuration

Ocamlformat has multiple ways to change settings:

  • project wide (.ocamlformat file)
  • environmental variable OCAMLFORMAT=option=val
  • file wide [@@@ocamlformat "option=val"]
  • expression [@@ocamlformat "option=val"]

Disable per file

To prevent a file from being run add the file directive to the top of the file:

[@@@ocamlformat "disable"]

Recommended links settings

Make sure that match cases aren't put on the same line:

break-cases=toplevel

Ensure that documentation comments (** ... *) are formatted before the declaration:

doc-comments=before

Further settings

It is worth reading the man page for ocamlformat using man ocamlformat or ocamlformat --help.