prettyprinter-0.1: A modern, extensible and well-documented prettyprinter.

Safe HaskellSafe
LanguageHaskell2010

Data.Text.Prettyprint.Doc.Internal.Type

Description

Internal module with stability guarantees

This module exposes the internals of the Doc type so other libraries can write adaptors to/from it. For all other uses, please use only the API provided by non-internal modules.

Although this module is internal, it follows the usual package versioning policy, AKA Haskell’s version of semantic versioning. In other words, this module is as stable as the public API.

Synopsis

Documentation

data Doc ann Source #

The abstract data type Doc ann represents pretty documents that have been annotated with data of type ann.

More specifically, a value of type Doc represents a non-empty set of possible layouts of a document. The layout functions select one of these possibilities, taking into account things like the width of the output document.

The annotation is an arbitrary piece of data associated with (part of) a document. Annotations may be used by the rendering backends in order to display output differently, such as

  • color information (e.g. when rendering to the terminal)
  • mouseover text (e.g. when rendering to rich HTML)
  • metailed whether to show something or not (to allow simple or detailed versions)

The simplest way to display a Doc is via the Show class.

>>> putStrLn (show (vsep ["hello", "world"]))
hello
world

Constructors

Fail

Occurs when flattening a line. The layouter will reject this document, choosing a more suitable rendering.

Empty

The empty document; unit of Cat (observationally)

Char Char

invariant: char is not '\n'

Text !Int Text

Invariants: at least two characters long, does not contain '\n'. For empty documents, there is Empty; for singleton documents, there is Char; newlines should be replaced by e.g. Line.

Since the frequently used length of Text is O(length), we cache it in this constructor.

Line

Line break

FlatAlt (Doc ann) (Doc ann)

Lay out the first Doc, but when flattened (via group), fall back to the second. The flattened version should in general be higher and narrower than the fallback.

Cat (Doc ann) (Doc ann)

Concatenation of two documents

Nest !Int (Doc ann)

Document indented by a number of columns

Union (Doc ann) (Doc ann)

Invariant: first lines of first '(Doc ann)' longer than the first lines of the second one. Used to implement layout alternatives for group.

Column (Int -> Doc ann)

React on the current cursor position, see column

WithPageWidth (PageWidth -> Doc ann)

React on the document's width, see pageWidth

Nesting (Int -> Doc ann)

React on the current nesting level, see nesting

Annotated ann (Doc ann)

Add an annotation to the enclosed Doc. Can be used for example to add styling directives or alt texts that can then be used by the renderer.

Instances

Show (Doc ann) Source #

(show doc) prettyprints document doc with defaultLayoutOptions, ignoring all annotations.

Methods

showsPrec :: Int -> Doc ann -> ShowS #

show :: Doc ann -> String #

showList :: [Doc ann] -> ShowS #

IsString (Doc ann) Source #
>>> pretty ("hello\nworld")
hello
world

This instance uses the Pretty Text instance, and uses the same newline to line conversion.

Methods

fromString :: String -> Doc ann #

Semigroup (Doc ann) Source #
x <> y = hcat [x, y]
>>> "hello" <> "world" :: Doc ann
helloworld

Methods

(<>) :: Doc ann -> Doc ann -> Doc ann #

sconcat :: NonEmpty (Doc ann) -> Doc ann #

stimes :: Integral b => b -> Doc ann -> Doc ann #

Monoid (Doc ann) Source #
mempty = emptyDoc
mconcat = hcat
>>> mappend "hello" "world" :: Doc ann
helloworld

Methods

mempty :: Doc ann #

mappend :: Doc ann -> Doc ann -> Doc ann #

mconcat :: [Doc ann] -> Doc ann #

Pretty (Doc ann) Source #

Does not change the text, but removes all annotations. Pitfall: since this un-annotates its argument, nesting it means multiple, potentially costly, traversals over the Doc.

>>> pretty 123
123
>>> pretty (pretty 123)
123

Methods

pretty :: Doc ann -> Doc ann Source #

prettyList :: [Doc ann] -> Doc ann Source #