OCamlFormat 0.8by Guillaume Petiot on Oct 17th, 2018
We are proud to announce the release of OCamlFormat 0.8 (available on opam). To ease the transition from the previous 0.7 release here are some highlights of the new features of this release. The full changelog is available on the project repository.
In the previous version you could override command line options with
.ocamlformat files configuration. 0.8 fixed this so that the OCamlFormat configuration is first established by reading
margin = 77 wrap-comments = true
By default, these files in current and all ancestor directories for each input file are used, as well as the global configuration defined in
$XDG_CONFIG_HOME/ocamlformat. The global
$XDG_CONFIG_HOME/ocamlformat configuration has the lowest priority, then the closer the directory is to the processed file, the higher the priority. In each directory, both
.ocp-indent files are read, with
.ocamlformat files having the higher priority.
ocp-indent options support is very partial and is expected to be extended in the future.
Then the parameters can be overriden with the
OCAMLFORMAT environment variable:
and finally the parameters can be overriden again with the command lines parameters.
It is now possible to read the input from stdin instead of OCaml files. The following command invokes OCamlFormat that reads its input from the pipe:
echo "let f x = x + 1" | ocamlformat --name a.ml -
- on the command line indicates that
ocamlformat should read from stdin instead of expecting input files. It is then necessary to use the
--name option to designate the input (
Preset profiles allow you to have a consistent configuration without needing to tune every option.
Preset profiles set all options, overriding lower priority configuration. A preset profile can be set using the
-p) option. You can pass the option
--profile=<name> on the command line or add
profile = <name> in an
.ocamlformat configuration file.
The available profiles are:
defaultsets each option to its default value
compactsets options for a generally compact code style
sparsesets options for a generally sparse code style
janestreetis the profile used at JaneStreet
To get a better feel of it, here is the formatting of the
mk_callback function from the OCaml compiler with the
let mk_callback rest name desc = function | None -> nothing | Some f -> ( fun () -> match rest with |  -> f name None | (hidden, _) :: _ -> f name (Some (desc, hidden)) )
then the same function formatted with the
let mk_callback rest name desc = function | None -> nothing | Some f -> fun () -> ( match rest with |  -> f name None | (hidden, _) :: _ -> f name (Some (desc, hidden)) )
The project root of an input file is taken to be the nearest ancestor directory that contains a
If the new option
--disable-outside-detected-project is set,
.ocamlformat configuration files are not read outside of the current project. If no configuration file is found, formatting is disabled.
A new option,
--root allows to specify the root directory for a project. If specified, OCamlFormat only takes into account
.ocamlformat configuration files inside the root directory and its subdirectories.
This release also contains many other changes and bug fixes that we cannot detail here. Check out the full changelog.
Special thanks to our maintainers and contributors for this release: David Allsopp, Josh Berdine, Hugo Heuzard, Brandon Kase, Anil Madhavapeddy and Guillaume Petiot.