exfmt v0.2.0 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
wide/1
has been added to support printing of forms that span to the end of the line, such as comments.
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
The wide algebra will never fit, it always causes a break. We use this to represent comments as they span to the end of the line, no matter what the line limit is
Link to this section Types
t :: :doc_nil | :doc_line | doc_cons | doc_nest | doc_break | doc_group | doc_wide | binary
Link to this section Functions
Concatenates a list of documents returning a new document.
Examples
iex> doc = concat(["a", "b", "c"])
...> format(doc, 80)
["a", "b", "c"]
Concatenates two document entities returning a new document.
Examples
iex> doc = concat("hello", "world")
...> format(doc, 80)
["hello", "world"]
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"
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"]
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"]