This module offers a high-level and functional interface to the Format module of the OCaml standard library. It is a pretty-printing facility, i.e. it takes as input some code represented as a tree and formats this code into the most visually satisfying result, breaking and indenting lines of code where appropriate.
Input data must be first modelled and converted into a tree using 3 kinds of nodes:
Atoms represent any text that is guaranteed to be printed as-is. Lists can model any sequence of items such as arrays of data or lists of definitions that are labelled with something like "int main", "let x =" or "x:".
This software was written by Martin Jambon. It is distributed under a BSD license. The current version is 1.0.0; see Changes. Download it here.
The development version of easy-format is hosted on GitHub.
Ocamldoc-generated interface documentation
Indentation is performed using ASCII space character, and whitespace as produced by the Format module. The current implementation uses reasonable characters: ASCII space ' ' and ASCII line feeds '\n' although the output functions may perform a conversion, i.e. convert "\n" into "\r\n" on Windows out_channels opened in text mode.
No strict indentation rules are guaranteed. The target language, if any, is assumed to treat any sequence of whitespace characters between nodes as insignificant.
Formatting hints can be specified for each node of the tree and they allow to cover a variety of formatting styles.
A fairly complete example (HTML, raw source).
More examples used for testing and debugging are provided with the source distribution.
label opening ... closing
or
label opening ... closing
Parameters:
stick_to_label = true align_closing = true
label opening item1 item2 closing
or
label opening ... closing
Parameters:
stick_to_label = false align_closing = true
label opening ... closing
or
label opening ... closing
Parameters:
indent_after_label = 0 stick_to_label = false align_closing = true
opening item1 item2 closing
or
opening item1 item2 closing
Parameters:
indent_items = 2 align_closing = true
opening item1 item2 item3 closing
or
opening item1 item2 item3 closing
Parameters:
align_closing = false
opening item1 item2 item3 closing
or
opening item1 item2 item3 closing
Parameters:
wrap_body = `Always_wrap
opening item1 item2 item3 closing
or
opening item1 item2 item3 closing
Parameters:
wrap_body = `Never_wrap
opening item1 item2 item3 closing
Parameters:
wrap_body = `Force_breaks
opening item1 item2 item3 closing
or
opening item1 item2 item3 closing
Parameters:
wrap_body = `No_breaks
( item1 item2 )
or
( item1 item2 )
Parameters:
space_after_opening = true space_before_closing = true
(item1 item2)
or
( item1 item2 )
Parameters:
space_after_opening = false space_before_closing = false
opening item1, item2 closing
or
opening item1, item2 closing
Parameters:
space_after_separator = true
opening item1,item2 closing
or
opening item1, item2 closing
Parameters:
space_after_separator = false
opening item1 separator item2 separator item3 closing
or
opening item1 separator item2 separator item3 closing
or
opening item1 separator item2 separator item3 closing
Parameters:
separators_stick_left = false space_before_separator = true space_after_separator = true