exfmt v0.1.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

Link to this type t()
t ::
  :doc_nil |
  :doc_line |
  doc_cons |
  doc_nest |
  doc_break |
  doc_group |
  doc_wide |
  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 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.

Link to this function wide(doc)
wide(t) :: t

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.

Examples

iex> doc = glue(wide("hello"), "world")
...> format(doc, 80)
["hello", "\n", "world"]