Module

Dodo

Package
purescript-dodo-printer
Repository
natefaubion/purescript-dodo-printer

#indent Source

indent :: forall a. Doc a -> Doc a

Increases the indentation level by one indent.

#align Source

align :: forall a. Int -> Doc a -> Doc a

Increases the indentation level by the number of spaces (for alignment purposes).

#alignCurrentColumn Source

alignCurrentColumn :: forall a. Doc a -> Doc a

Increases the indentation level so that it aligns to the current column.

#annotate Source

annotate :: forall a. a -> Doc a -> Doc a

Adds an annotation to a document. Printers can interpret annotations to style their output, eg. ANSI colors.

#withPosition Source

withPosition :: forall a. (Position -> Doc a) -> Doc a

Build a document based on the current layout position.

#text Source

text :: forall a. String -> Doc a

The most basic document leaf. This should not contain newlines. If it does your document will look very funny.

#break Source

break :: forall a. Doc a

Inserts a hard line break.

#spaceBreak Source

spaceBreak :: forall a. Doc a

Inserts a space when in a flex group, otherwise inserts a break.

#softBreak Source

softBreak :: forall a. Doc a

Inserts nothing when in a flex group, otherwise inserts a break.

#space Source

space :: forall a. Doc a

A singe space character.

#lines Source

lines :: forall f a. Foldable f => f (Doc a) -> Doc a

Appends documents with a break in between them.

#words Source

words :: forall f a. Foldable f => f (Doc a) -> Doc a

Appends documents with a space in between them.

#(<%>) Source

Operator alias for Dodo.appendBreak (right-associative / precedence 1)

#(<+>) Source

Operator alias for Dodo.appendSpace (right-associative / precedence 2)

#(</>) Source

Operator alias for Dodo.appendSpaceBreak (left-associative / precedence 2)

#appendBreak Source

appendBreak :: forall a. Doc a -> Doc a -> Doc a

Appends two documents with a break between them.

#appendSpace Source

appendSpace :: forall a. Doc a -> Doc a -> Doc a

Appends two documents with a space between them.

#appendSpaceBreak Source

appendSpaceBreak :: forall a. Doc a -> Doc a -> Doc a

Appends two documents with a space between them, falling back to a break if that does not fit.

#flexAlt Source

flexAlt :: forall a. Doc a -> Doc a -> Doc a

Attempts to layout the first document when in a flex group, falling back to the second as a default.

#flexGroup Source

flexGroup :: forall a. Doc a -> Doc a

Attempts to layout the document with flex alternatives, falling back to defaults if it doesn't fit the page width.

#flexSelect Source

flexSelect :: forall a. Doc a -> Doc a -> Doc a -> Doc a

Attempts to layout the first document with flex alternatives, falling back to defaults if it doesn't fit the page width. If the flex alternatives are used then the second document will be appended, otherwise the third document will be appended.

#paragraph Source

paragraph :: forall f a. Foldable f => f (Doc a) -> Doc a

Appends documents with a space-break in between them.

#textParagraph Source

textParagraph :: forall a. String -> Doc a

Constructs a wrapping paragraph from a blob of text. Ignores newlines and multiple spaces.

#enclose Source

enclose :: forall a. Doc a -> Doc a -> Doc a -> Doc a

Uses an opening and closing document to wrap another document.

example = enclose (text "(") (text ")") (text "inner")

#encloseEmptyAlt Source

encloseEmptyAlt :: forall a. Doc a -> Doc a -> Doc a -> Doc a -> Doc a

Uses an opening and closing document to wrap another document, falling back when the inner document is empty.

example = encloseEmptyAlt (text "[ ") (text " ]") (text "[]") mempty

#encloseWithSeparator Source

encloseWithSeparator :: forall f a. Foldable f => Doc a -> Doc a -> Doc a -> f (Doc a) -> Doc a

Uses an opening and closing document, as a well as a separator, to render a series of documents. ``purescript example = encloseWithSeparator (text "[") (text "]") (",") [ text "one", text "two" ]


#foldWithSeparator Source

foldWithSeparator :: forall f a. Foldable f => Doc a -> f (Doc a) -> Doc a

Appends a series of documents together with a separator in between them.

#foldWith Source

foldWith :: forall f a. Foldable f => (Doc a -> Doc a -> Doc a) -> f (Doc a) -> Doc a

Appends a series of documents together with a given append function. This is notable because it ignores empty documents.

#locally Source

locally :: forall a. (LocalOptions -> LocalOptions) -> Doc a -> Doc a

EXPERIMENTAL: modifies printing state and options locally for a document. This may change or be removed at any time.

#withLocalOptions Source

withLocalOptions :: forall a. (LocalOptions -> Tuple LocalOptions (Doc a)) -> Doc a

EXPERIMENTAL: modifies printing state and options locally for a document. This may change or be removed at any time. Differs from locally in that the document can be responsive to options.

#print Source

print :: forall b a r. Printer b a r -> PrintOptions -> Doc a -> r

Prints a documents given a printer and print options.

print plainText twoSpaces myDoc

This will use full line-lookahead from the start of a flex group. If it encounters a break or content overflows the page-width, it will layout the group using flex alternative defaults instead.

#Printer Source

newtype Printer buff ann res

Custom printers can be used to render richer documents than just plain text.

  • emptyBuffer - The initial buffer.
  • writeText - Should write a string with the given width to the buffer.
  • writeIndent - Should write indentation with the given width to the buffer.
  • writeBreak - Should write a line break to the buffer.
  • enterAnnotation - Called when entering a new annotated region. Provides the full annotation stack.
  • leaveAnnotation - Called when leaving an annotated region. Provides the full annotation stack.
  • flushBuffer - Called at the end of the document to get the final result.

Constructors

  • Printer { emptyBuffer :: buff, enterAnnotation :: ann -> List ann -> buff -> buff, flushBuffer :: buff -> res, leaveAnnotation :: ann -> List ann -> buff -> buff, writeBreak :: buff -> buff, writeIndent :: Int -> String -> buff -> buff, writeText :: Int -> String -> buff -> buff }

#plainText Source

plainText :: forall a. Printer String a String

A plain text printer. Can be used with any document.

#PrintOptions Source

type PrintOptions = { indentUnit :: String, indentWidth :: Int, pageWidth :: Int, ribbonRatio :: Number }

Configuration options for the printer.

  • pageWidth - The printer will try not to exceed this width on any given line.
  • ribbonRatio - Ratio between 0.0 and 1.0, defaults to 1.0. The printer will use this ratio to calculate the printable area between the current indentation level and the pageWidth.
  • indentUnit - The string used for a single indent.
  • indentWidth - The assumed character width of a single indentUnit.

#twoSpaces Source

twoSpaces :: PrintOptions

Prints 2-space indents, with a default 80-column page width.

#fourSpaces Source

fourSpaces :: PrintOptions

Prints 4-space indents, with a default 120-column page width.

#tabs Source

tabs :: PrintOptions

Prints tab indents (4-wide), with a default 120-column page width.

Re-exports from Dodo.Internal

#Position Source

type Position = { column :: Int, indent :: Int, line :: Int, nextIndent :: Int, pageWidth :: Int, ribbonWidth :: Int }

Document lines and columns are 0-based offsets.

#Doc Source

data Doc a

Documents are built using <> as horizontal, line-wise concatenation. The functions in this module let you build documents that respond well to width constraints (such as flexGroup and flexAlt).

Instances

#notEmpty Source

notEmpty :: forall a. (Doc a -> Doc a) -> Doc a -> Doc a

Only applies the provided function if the document is non-empty.

#isEmpty Source

isEmpty :: forall a. Doc a -> Boolean

Checks whether the document is empty.

#bothNotEmpty Source

bothNotEmpty :: forall a. (Doc a -> Doc a -> Doc a) -> Doc a -> Doc a -> Doc a

Only applies the provided function if both documents are non-empty, otherwise just yields whichever is non-empty.