OCamlFormat can be installed with opam:
opam install ocamlformatAlternatively, it can be built manually with:
# To properly set `ocamlformat --version`
dune subst
dune build @installFirst of all, make sure you have an .ocamlformat file at the root of your project. Setting up your project to use the default profile and the OCamlFormat version you installed (hopefully the last one) in this .ocamlformat file is considered good practice:
profile = default
version = 0.27.0To manually invoke OCamlformat the general command is:
ocamlformat [OPTION]... [SRC]...The most common usecase involves using the Dune build system, once your project is correctly setup (see Dune's manual) you can reformat your project using:
dune build @fmtWith this command, Dune will apply the appropriate autoformatter on each kind of file (where OCamlFormat is the one called on .ml, .mli, .mlt, .eliom and .eliomi files). All files are formatted, unless explicitly excluded (see below).
There are a number of preset code style profiles, selected using the --profile option by passing --profile=<name> on the command line or adding profile = <name> to an .ocamlformat configuration file. The available profiles are:
conventional (also known as default);ocamlformat;janestreet.It is considered a good practice to set a profile in the .ocamlformat configuration file of a project, even if you plan on using the default profile, explicitly setting profile = default is recommended.
Each profile is a collection of settings for all options, overriding lower priority configuration of individual options. So a profile can be selected and then individual options can be overridden if desired.
The conventional (or default) profile aims to be as familiar and "conventional" appearing as the available options allow.
The ocamlformat profile aims to take advantage of the strengths of a parsetree-based auto-formatter, and to limit the consequences of the weaknesses imposed by the current implementation. This is a style which optimizes for what the formatter can do best, rather than to match the style of any existing code. Instead of familiarity, the focus is on legibility, keeping the common cases reasonably compact while attempting to avoid confusing formatting in corner cases. General guidelines that have directed the design include:
If no profile is selected, the default one is used.
The full options' documentation is available on this page or through ocamlformat --help. Options can be modified by the means of:
.ocamlformat configuration file with an option = VAL lineOCAMLFORMAT environment variable: OCAMLFORMAT=option=VAL,...,option=VAL[@@@ocamlformat "option=VAL"] attribute in the processed file[@@ocamlformat "option=VAL"] attribute on an expression in the processed fileFor more details, read about how OCamlFormat finds its root project and computes its configuration.
On existing projects, it is likely an .ocamlformat file is already present, specifying a field version = A.B.C (e.g. 0.20.0).
It is the user's responsability to install the appropriate OCamlFormat binary, requesting an older version if one's project requires it:
opam install ocamlformat.0.20.0Setting an OCamlFormat version in the configuration file is a good practice to ensure every contributor of a project gets the same formatting.
OCamlFormat requires source code that meets the following conditions:
.ml), an interface (.mli) or a sequence of toplevel phrases (.mlt). dune files in OCaml syntax also work.Under those conditions, OCamlFormat is expected to produce output equivalent to the input. As a safety check in case of bugs, prior to terminating or modifying any input file, OCamlFormat enforces the following checks:
.equal).