package reason

  1. Overview
  2. Docs

Easy_format: indentation made easy.

This module provides a functional, simplified layer over the Format module of the standard library.

Input data must be first modelled as a tree using 3 kinds of nodes:

  • atoms
  • lists
  • labelled 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:".

type wrap = [
  1. | `Wrap_atoms
  2. | `Always_wrap
  3. | `Never_wrap
  4. | `Force_breaks
  5. | `Force_breaks_rec
  6. | `No_breaks
]

List wrapping conditions:

  • `Wrap_atoms: wrap if the list contains only atoms
  • `Always_wrap: always wrap when needed
  • `Never_wrap: never wrap, i.e. the list is either horizontal or vertical
  • `Force_breaks: align vertically, i.e. always break line between list items and align the left edge of each item.
  • `Force_breaks_rec: same as `Force_breaks but turns any wrappable ancestor node's wrap property (`Wrap_atoms or `Always_wrap) into `Force_breaks.
  • `No_breaks: align horizontally, i.e. never break line between list items
type label_break = [
  1. | `Auto
  2. | `Always
  3. | `Always_rec
  4. | `Never
]

When to break the line after a Label:

  • Auto: break after the label if there's not enough room
  • Always: always break after the label
  • Always_rec: always break after the label and force breaks in all parent lists and labels, similarly to `Force_breaks_rec for lists.
  • Never: never break after the label
type style_name = string
type style = {
  1. tag_open : string;
  2. tag_close : string;
}

Pair of opening and closing tags that are inserted around text after pretty-printing.

type atom_param = {
  1. atom_style : style_name option;
    (*

    Default: None

    *)
}
val atom : atom_param
type list_param = {
  1. space_after_opening : bool;
    (*

    Whether there must be some whitespace after the opening string. Default: true

    *)
  2. space_after_separator : bool;
    (*

    Whether there must be some whitespace after the item separators. Default: true

    *)
  3. space_before_separator : bool;
    (*

    Whether there must be some whitespace before the item separators. Default: false

    *)
  4. separators_stick_left : bool;
    (*

    Whether the separators must stick to the item on the left. Default: true

    *)
  5. space_before_closing : bool;
    (*

    Whether there must be some whitespace before the closing string. Default: true

    *)
  6. stick_to_label : bool;
    (*

    Whether the opening string should be fused with the preceding label. Default: true

    *)
  7. align_closing : bool;
    (*

    Whether the beginning of the closing string must be aligned with the beginning of the opening string (stick_to_label = false) or with the beginning of the label if any (stick_to_label = true). Default: true

    *)
  8. wrap_body : wrap;
    (*

    Defines under which conditions the list body may be wrapped, i.e. allow several lines and several list items per line. Default: `Wrap_atoms

    *)
  9. indent_body : int;
    (*

    Extra indentation of the list body. Default: 2

    *)
  10. list_style : style_name option;
    (*

    Default: None

    *)
  11. opening_style : style_name option;
    (*

    Default: None

    *)
  12. body_style : style_name option;
    (*

    Default: None

    *)
  13. separator_style : style_name option;
    (*

    Default: None

    *)
  14. closing_style : style_name option;
    (*

    Default: None

    *)
}

List-formatting parameters. Always derive a new set of parameters from an existing record. See Easy_format.list.

val list : list_param

Default list-formatting parameters, using the default values described in the type definition above.

In order to make code compatible with future versions of the library, the record inheritance syntax should be used, e.g. { list with align_closing = false } . If new record fields are added, the program would still compile and work as before.

type label_param = {
  1. label_break : label_break;
    (*

    Whether to break the line after the label. Introduced in version 1.2.0. Default: `Auto

    *)
  2. space_after_label : bool;
    (*

    Whether there must be some whitespace after the label. Default: true

    *)
  3. indent_after_label : int;
    (*

    Extra indentation before the item that comes after a label. Default: 2

    *)
  4. label_style : style_name option;
    (*

    Default: None

    *)
}

Label-formatting parameters. Always derive a new set of parameters from an existing record. See Easy_format.label.

val label : label_param

Default label-formatting parameters, using the default values described in the type definition above.

In order to make code compatible with future versions of the library, the record inheritance syntax should be used, e.g. { label with indent_after_label = 0 } . If new record fields are added, the program would still compile and work as before.

type t =
  1. | Atom of string * atom_param
    (*

    Plain string normally without line breaks.

    *)
  2. | List of string * string * string * list_param * t list
    (*

    List ((opening, separator, closing, param), nodes)

    *)
  3. | Label of t * label_param * t
    (*

    Label ((label, param), node): labelled node.

    *)
  4. | Custom of Stdlib.Format.formatter -> unit
    (*

    User-defined printing function that allows to use the Format module directly if necessary. It is responsible for leaving the formatter in a clean state.

    *)

The type of the tree to be pretty-printed. Each node contains its own formatting parameters.

Detail of a list node List ((opening, separator, closing, param), nodes):

  • opening: opening string such as "\{" "[" "(" "begin" "" etc.
  • separator: node separator such as ";" "," "" "+" "|" etc.
  • closing: closing string such as "\}" "]" ")" "end" "" etc.
  • nodes: elements of the list.
type escape = [
  1. | `None
  2. | `Escape of (string -> int -> int -> unit) -> string -> int -> int -> unit
  3. | `Escape_string of string -> string
]
type styles = (style_name * style) list
module Pretty : sig ... end

The regular pretty-printing functions

module Compact : sig ... end

No spacing or newlines other than those in the input data or those produced by Custom printing.

OCaml

Innovation. Community. Security.