exfmt v0.2.2 Exfmt.Algebra

A set of functions for creating and manipulating algebra documents.

This module implements the functionality described in “Strictly Pretty” (2000) by Christian Lindig, with a few extensions detailed below.

It serves an alternative printer to the one defined in Inspect.Algebra, which is part of the Elixir standard library but does not entirely conform to the algorithm described by Christian Lindig in a way that makes it unsuitable for use in ExFmt.

Extensions

  • nest/1 can take the atom :current instead of an integer. With this value the formatter will set the indentation level to the current column position.

Link to this section Summary

Functions

Concatenates a list of documents returning a new document

Concatenates two document entities returning a new document

Formats a given document for a given width

Glues two documents together inserting " " as a break between them

Glues two documents (doc1 and doc2) together inserting the given break break_string between them

Nests the given document at the given level

Link to this section Types

Link to this type t()
t ::
  :doc_nil |
  :doc_line |
  doc_cons |
  doc_nest |
  doc_break |
  doc_group |
  binary

Link to this section Functions

Link to this function concat(docs)
concat([t]) :: t

Concatenates a list of documents returning a new document.

Examples

iex> doc = concat(["a", "b", "c"])
...> format(doc, 80)
["a", "b", "c"]
Link to this function concat(doc1, doc2)
concat(t, t) :: t

Concatenates two document entities returning a new document.

Examples

iex> doc = concat("hello", "world")
...> format(doc, 80)
["hello", "world"]
Link to this function format(doc, width)
format(t, non_neg_integer | :infinity) :: iodata

Formats a given document for a given width.

Takes the maximum width and a document to print as its arguments and returns an IO data representation of the best layout for the document to fit in the given width.

Examples

iex> doc = glue("hello", " ", "world")
iex> format(doc, 30) |> IO.iodata_to_binary()
"hello world"
iex> format(doc, 10) |> IO.iodata_to_binary()
"hello\nworld"
Link to this function glue(doc1, doc2)
glue(t, t) :: t

Glues two documents together inserting " " as a break between them.

This means the two documents will be separated by " " in case they fit in the same line. Otherwise a line break is used.

Examples

iex> doc = glue("hello", "world")
...> format(doc, 80)
["hello", " ", "world"]
Link to this function glue(doc1, break_string, doc2)
glue(t, binary, t) :: t

Glues two documents (doc1 and doc2) together inserting the given break break_string between them.

For more information on how the break is inserted, see break/1.

Examples

iex> doc = glue("hello", "\t", "world")
...> format(doc, 80)
["hello", "\t", "world"]
Link to this function nest(doc, level)
nest(t, non_neg_integer | :current) :: doc_nest

Nests the given document at the given level.

Nesting will be appended to the line breaks.

Examples

iex> doc = Inspect.Algebra.nest(Inspect.Algebra.glue("hello", "world"), 5)
iex> Inspect.Algebra.format(doc, 5)
["hello", "\n     ", "world"]
Link to this function surround(left, doc, right)

See Inspect.Algebra.surround/3.

Link to this function surround_many(l, docs, r, opts, fun)

See Inspect.Algebra.surround_many/5.

Link to this function surround_many(l, docs, r, opts, fun, sep)

See Inspect.Algebra.surround_many/6.