(<+>) :: Doc ann -> Doc ann -> Doc ann
(x concatenates document <+> y)x and y with a in
between.space
>>> "hello" <+> "world"
hello worldx '<+>' y = x '<>' 'space' '<>' y
Use \ Prettyprinter.Internal\ instead.
Prettyprinter.Internal prettyprinter-1.7.2 · 61 declarations(<+>) :: Doc ann -> Doc ann -> Doc ann(x concatenates document <+> y)x and y with a in
between.space
>>> "hello" <+> "world"
hello worldx '<+>' y = x '<>' 'space' '<>' y
align :: Doc ann -> Doc ann( lays out the document align x)x with the nesting level set to the
current column. It is used for example to implement hang.
As an example, we will put a document right above another one, regardless of
the current nesting level. Without alignment, the second line is put simply
below everything we've had so far:
>>> "lorem" <+> vsep ["ipsum", "dolor"]
lorem ipsum
dolorIf we add an align to the mix, the 's contents all start in the
same column:vsep
>>> "lorem" <+> align (vsep ["ipsum", "dolor"])
lorem ipsum
doloralterAnnotations :: (ann -> [ann']) -> Doc ann -> Doc ann'Change the annotations of a Document. Individual annotations can be
removed, changed, or replaced by multiple ones.
This is a general function that combines unAnnotate and reAnnotate, and
it is useful for mapping semantic annotations (such as »this is a keyword«)
to display annotations (such as »this is red and underlined«), because some
backends may not care about certain annotations, while others may.
Annotations earlier in the new list will be applied earlier, i.e. returning
[Bold, Green] will result in a bold document that contains green text, and
not vice-versa.
Since this traverses the entire tree, including parts that are not
rendered due to other layouts fitting better, it is preferable to reannotate
after producing the layout by using Doc.alterAnnotationsS
alterAnnotationsS :: (ann -> Maybe ann') -> SimpleDocStream ann -> SimpleDocStream ann'Change the annotation of a document to a different annotation, or none at
all. alterAnnotations for SimpleDocStream.
Note that the Doc version is more flexible, since it allows changing a
single annotation to multiple ones.
(SimpleDocTree restores
this flexibility again.)
annotate :: ann -> Doc ann -> Doc annAdd an annotation to a . This annotation can then be used by the
renderer to e.g. add color to certain parts of the output. For a full
tutorial example on how to use it, see the
DocPrettyprinter.Render.Tutorials.StackMachineTutorial or
Prettyprinter.Render.Tutorials.TreeRenderingTutorial modules.
This function is only relevant for custom formats with their own annotations,
and not relevant for basic prettyprinting. The predefined renderers, e.g.
Prettyprinter.Render.Text, should be enough for the most common
needs.
cat :: [Doc ann] -> Doc ann( tries laying out the documents cat xs)xs separated with nothing,
and if this does not fit the page, separates them with newlines. This is what
differentiates it from vcat, which always lays out its contents beneath
each other.
>>> let docs = Util.words "lorem ipsum dolor"
>>> putDocW 80 ("Docs:" <+> cat docs)
Docs: loremipsumdolorWhen there is enough space, the documents are put above one another:
>>> putDocW 10 ("Docs:" <+> cat docs)
Docs: lorem
ipsum
dolor'cat' = 'group' . 'vcat'
column :: (Int -> Doc ann) -> Doc annLayout a document depending on which column it starts at. align is
implemented in terms of column.
>>> column (\l -> "Columns are" <+> pretty l <> "-based.")
Columns are 0-based.>>> let doc = "prefix" <+> column (\l -> "| <- column" <+> pretty l)
>>> vsep [indent n doc | n <- [0,4,8]]
prefix | <- column 7
prefix | <- column 11
prefix | <- column 15concatWith :: Foldable t => (Doc ann -> Doc ann -> Doc ann) -> t (Doc ann) -> Doc annConcatenate all documents element-wise with a binary function.
'concatWith' _ [] = 'mempty'
'concatWith' (**) [x,y,z] = x ** y ** z
Multiple convenience definitions based on concatWith are already predefined,
for example:
'hsep' = 'concatWith' ('<+>')
'fillSep' = 'concatWith' (\\x y -> x '<>' 'softline' '<>' y)
This is also useful to define customized joiners:
>>> concatWith (surround dot) ["Prettyprinter", "Render", "Text"]
Prettyprinter.Render.TextdefaultLayoutOptions :: LayoutOptionsThe default layout options, suitable when you just want some output, and
don’t particularly care about the details. Used by the Show instance, for
example.
>>> defaultLayoutOptions
LayoutOptions {layoutPageWidth = AvailablePerLine 80 1.0}defaultPageWidth :: PageWidthemptyDoc :: Doc annThe empty document behaves like (, so it has a height of 1.
This may lead to surprising behaviour if we expect it to bear no weight
inside e.g. pretty "")vcat, where we get an empty line of output from it (parens
for visibility only):
>>> vsep ["hello", parens emptyDoc, "world"]
hello
()
worldTogether with <>, emptyDoc forms the Monoid Doc.
enclose :: Doc ann -> Doc ann -> Doc ann -> Doc ann( encloses document enclose l r x)x between documents l and r
using .<>
>>> enclose "A" "Z" "·"
A·Z'enclose' l r x = l '<>' x '<>' r
L
R
x
LxR
encloseSep :: Doc ann -> Doc ann -> Doc ann -> [Doc ann] -> Doc ann( concatenates the documents encloseSep l r sep xs)xs separated by
sep, and encloses the resulting document by l and r.
The documents are laid out horizontally if that fits the page:
>>> let doc = "list" <+> align (encloseSep lbracket rbracket comma (map pretty [1,20,300,4000]))
>>> putDocW 80 doc
list [1,20,300,4000]If there is not enough space, then the input is split into lines entry-wise otherwise they are laid out vertically, with separators put in the front:
>>> putDocW 10 doc
list [1
,20
,300
,4000]Note that doc contains an explicit call to align so that the list items
are aligned vertically.
For putting separators at the end of entries instead, have a look at
punctuate.
left delimiter
right delimiter
separator
[Doc ann]input documents
fill :: Int -> Doc ann -> Doc ann( lays out the document fill i x)x. It then appends spaces until
the width is equal to i. If the width of x is already larger, nothing is
appended.
This function is quite useful in practice to output a list of bindings:
>>> let types = [("empty","Doc"), ("nest","Int -> Doc -> Doc"), ("fillSep","[Doc] -> Doc")]
>>> let ptype (name, tp) = fill 5 (pretty name) <+> "::" <+> pretty tp
>>> "let" <+> align (vcat (map ptype types))
let empty :: Doc
nest :: Int -> Doc -> Doc
fillSep :: [Doc] -> DocIntAppend spaces until the document is at least this wide
fillBreak :: Int -> Doc ann -> Doc ann( first lays out the document fillBreak i x)x. It then appends spaces
until the width is equal to i. If the width of x is already larger than
i, the nesting level is increased by i and a line is appended. When we
redefine ptype in the example given in fill to use , we get
a useful variation of the output:fillBreak
>>> let types = [("empty","Doc"), ("nest","Int -> Doc -> Doc"), ("fillSep","[Doc] -> Doc")]
>>> let ptype (name, tp) = fillBreak 5 (pretty name) <+> "::" <+> pretty tp
>>> "let" <+> align (vcat (map ptype types))
let empty :: Doc
nest :: Int -> Doc -> Doc
fillSep
:: [Doc] -> DocIntAppend spaces until the document is at least this wide
fillCat :: [Doc ann] -> Doc ann( concatenates documents fillCat xs)xs horizontally with as
long as it fits the page, then inserts a <> and continues doing that
for all documents in line'xs. This is similar to how an ordinary word processor
lays out the text if you just keep typing after you hit the maximum line
length.
( means that if line'grouped, the documents are separated with nothing
instead of newlines. See fillSep if you want a space instead.)
Observe the difference between fillSep and fillCat. fillSep
concatenates the entries spaced when grouped:
>>> let docs = take 20 (cycle (["lorem", "ipsum", "dolor", "sit", "amet"]))
>>> putDocW 40 ("Grouped:" <+> group (fillSep docs))
Grouped: lorem ipsum dolor sit amet
lorem ipsum dolor sit amet lorem ipsum
dolor sit amet lorem ipsum dolor sit
ametOn the other hand, fillCat concatenates the entries directly when
grouped:
>>> putDocW 40 ("Grouped:" <+> group (fillCat docs))
Grouped: loremipsumdolorsitametlorem
ipsumdolorsitametloremipsumdolorsitamet
loremipsumdolorsitametfillSep :: [Doc ann] -> Doc ann( concatenates the documents fillSep xs)xs horizontally with
as long as it fits the page, then inserts a <+> and continues doing that
for all documents in linexs. ( means that if linegrouped, the documents
are separated with a space instead of newlines. Use fillCat if you do not
want a space.)
Let's print some words to fill the line:
>>> let docs = take 20 (cycle ["lorem", "ipsum", "dolor", "sit", "amet"])
>>> putDocW 80 ("Docs:" <+> fillSep docs)
Docs: lorem ipsum dolor sit amet lorem ipsum dolor sit amet lorem ipsum dolor
sit amet lorem ipsum dolor sit ametThe same document, printed at a width of only 40, yields
>>> putDocW 40 ("Docs:" <+> fillSep docs)
Docs: lorem ipsum dolor sit amet lorem
ipsum dolor sit amet lorem ipsum dolor
sit amet lorem ipsum dolor sit ametflatAlt :: Doc ann -> Doc ann -> Doc annBy default, ( renders as flatAlt x y)x. However when grouped,
y will be preferred, with x as the fallback for the case when y
doesn't fit.
>>> let doc = flatAlt "a" "b"
>>> putDoc doc
a
>>> putDoc (group doc)
b
>>> putDocW 0 (group doc)
aflatAlt is particularly useful for defining conditional separators such as
softline = 'group' ('flatAlt' 'hardline' " ")
>>> let hello = "Hello" <> softline <> "world!"
>>> putDocW 12 hello
Hello world!
>>> putDocW 11 hello
Hello
world!We can use this to render Haskell's do-notation nicely:
>>> let open = flatAlt "" "{ "
>>> let close = flatAlt "" " }"
>>> let separator = flatAlt "" "; "
>>> let prettyDo xs = group ("do" <+> align (encloseSep open close separator xs))
>>> let statements = ["name:_ <- getArgs", "let greet = \"Hello, \" <> name", "putStrLn greet"]This is put into a single line with {;} style if it fits:
>>> putDocW 80 (prettyDo statements)
do { name:_ <- getArgs; let greet = "Hello, " <> name; putStrLn greet }When there is not enough space the statements are broken up into lines nicely:
>>> putDocW 10 (prettyDo statements)
do name:_ <- getArgs
let greet = "Hello, " <> name
putStrLn greetUsers should be careful to choose x to be less wide than y.
Otherwise, if y turns out not to fit the page, we fall back on an even
wider layout:
>>> let ugly = group (flatAlt "even wider" "too wide")
>>> putDocW 7 ugly
even widerAlso note that group will flatten y:
>>> putDoc (group (flatAlt "x" ("y" <> line <> "y")))
y yThis also means that an "unflattenable" y which contains a hard linebreak
will never be rendered:
>>> putDoc (group (flatAlt "x" ("y" <> hardline <> "y")))
xDefault
Preferred when grouped
fuse :: FusionDepth -> Doc ann -> Doc ann( combines text nodes so they can be rendered more
efficiently. A fused document is always laid out identical to its unfused
version.fuse depth doc)
When laying a Document out to a SimpleDocStream, every component of the
input is translated directly to the simpler output format. This sometimes
yields undesirable chunking when many pieces have been concatenated together.
For example
>>> "a" <> "b" <> pretty 'c' <> "d"
abcdresults in a chain of four entries in a SimpleDocStream, although this is fully
equivalent to the tightly packed
>>> "abcd" :: Doc ann
abcdwhich is only a single SimpleDocStream entry, and can be processed faster.
It is therefore a good idea to run fuse on concatenations of lots of small
strings that are used many times:
>>> let oftenUsed = fuse Shallow ("a" <> "b" <> pretty 'c' <> "d")
>>> hsep (replicate 5 oftenUsed)
abcd abcd abcd abcd abcdgroup :: Doc ann -> Doc ann( tries laying out group x)x into a single line by removing the
contained line breaks; if this does not fit the page, or when a hardline
within x prevents it from being flattened, x is laid out without any
changes.
The group function is key to layouts that adapt to available space nicely.
See vcat, line, or flatAlt for examples that are related, or make good
use of it.
hang :: Int -> Doc ann -> Doc ann( lays out the document hang i x)x with a nesting level set to the
current column plus i. Negative values are allowed, and decrease the
nesting level accordingly.
>>> let doc = reflow "Indenting these words with hang"
>>> putDocW 24 ("prefix" <+> hang 4 doc)
prefix Indenting these
words with
hangThis differs from nest, which is based on the current nesting level plus
i. When you're not sure, try the more efficient nest first. In our
example, this would yield
>>> let doc = reflow "Indenting these words with nest"
>>> putDocW 24 ("prefix" <+> nest 4 doc)
prefix Indenting these
words with nest'hang' i doc = 'align' ('nest' i doc)
IntChange of nesting level, relative to the start of the first line
hardline :: Doc annA is always laid out as a line break, even when hardlinegrouped or
when there is plenty of space. Note that it might still be simply discarded
if it is part of a flatAlt inside a group.
>>> let doc = "lorem ipsum" <> hardline <> "dolor sit amet"
>>> putDocW 1000 doc
lorem ipsum
dolor sit amet>>> group doc
lorem ipsum
dolor sit amethcat :: [Doc ann] -> Doc ann( concatenates all documents hcat xs)xs horizontally with
(i.e. without any spacing).<>
It is provided only for consistency, since it is identical to mconcat.
>>> let docs = Util.words "lorem ipsum dolor"
>>> hcat docs
loremipsumdolorhsep :: [Doc ann] -> Doc ann( concatenates all documents hsep xs)xs horizontally with ,
i.e. it puts a space between all entries.<+>
>>> let docs = Util.words "lorem ipsum dolor sit amet">>> hsep docs
lorem ipsum dolor sit amet does not introduce line breaks on its own, even when the page is too
narrow:hsep
>>> putDocW 5 (hsep docs)
lorem ipsum dolor sit ametFor automatic line breaks, consider using fillSep instead.
indent :: Int -> Doc ann -> Doc ann( indents document indent i x)x by i columns, starting from the
current cursor position.
>>> let doc = reflow "The indent function indents these words!"
>>> putDocW 24 ("prefix" <> indent 4 doc)
prefix The indent
function
indents these
words!'indent' i d = 'hang' i ({i spaces} <> d)
IntNumber of spaces to increase indentation by
layoutCompact :: Doc ann1 -> SimpleDocStream ann2(layoutCompact x) lays out the document x without adding any
indentation and without preserving annotations.
Since no 'pretty' printing is involved, this layouter is very
fast. The resulting output contains fewer characters than a prettyprinted
version and can be used for output that is read by other programs.
>>> let doc = hang 4 (vsep ["lorem", "ipsum", hang 4 (vsep ["dolor", "sit"])])
>>> doc
lorem
ipsum
dolor
sit>>> let putDocCompact = renderIO System.IO.stdout . layoutCompact
>>> putDocCompact doc
lorem
ipsum
dolor
sitlayoutPretty :: LayoutOptions -> Doc ann -> SimpleDocStream annThis is the default layout algorithm, and it is used by show, putDoc
and hPutDoc.
commits to rendering something in a certain way if the
remainder of the current line fits the layout constraints; in other words,
it has up to one line of lookahead when rendering. Consider using the
smarter, but a bit less performant, layoutPretty algorithm if the results
seem to run off to the right before having lots of line breaks.layoutSmart
Width in which to fit the first line
layoutSmart :: LayoutOptions -> Doc ann -> SimpleDocStream annA layout algorithm with more lookahead than layoutPretty, that introduces
line breaks earlier if the content does not (or will not, rather) fit within
the page width.
Consider the following python-ish document,
>>> let fun x = hang 2 ("fun(" <> softline' <> x) <> ")"
>>> let doc = (fun . fun . fun . fun . fun) (align (list ["abcdef", "ghijklm"]))which we’ll be rendering using the following pipeline (where the layout algorithm has been left open):
>>> import Data.Text.IO as T
>>> import Prettyprinter.Render.Text
>>> let hr = pipe <> pretty (replicate (26-2) '-') <> pipe
>>> let go layouter x = (T.putStrLn . renderStrict . layouter (LayoutOptions (AvailablePerLine 26 1))) (vsep [hr, x, hr])If we render this using layoutPretty with a page width of 26 characters
per line, all the fun calls fit into the first line so they will be put
there:
>>> go layoutPretty doc
|------------------------|
fun(fun(fun(fun(fun(
[ abcdef
, ghijklm ])))))
|------------------------|Note that this exceeds the desired 26 character page width. The same
document, rendered with , fits the layout constraints:layoutSmart
>>> go layoutSmart doc
|------------------------|
fun(
fun(
fun(
fun(
fun(
[ abcdef
, ghijklm ])))))
|------------------------|The key difference between layoutPretty and layoutSmart is that the
latter will check the potential document until it encounters a line with the
same indentation or less than the start of the document. Any line encountered
earlier is assumed to belong to the same syntactic structure.
layoutPretty checks only the first line.
Consider for example the question of whether the As fit into the document
below:
1 A
2 A
3 A
4 B
5 BlayoutPretty will check only line 1, ignoring whether e.g. line 2 might
already be too wide.
By contrast, layoutSmart stops only once it reaches line 4, where the B
has the same indentation as the first A.
line :: Doc annThe document advances to the next line and indents to the current
nesting level.line
>>> let doc = "lorem ipsum" <> line <> "dolor sit amet"
>>> doc
lorem ipsum
dolor sit amet behaves like line if the line break is undone by spacegroup:
>>> group doc
lorem ipsum dolor sit ametline' :: Doc ann is like line', but behaves like line if the line break
is undone by memptygroup (instead of ).space
>>> let doc = "lorem ipsum" <> line' <> "dolor sit amet"
>>> doc
lorem ipsum
dolor sit amet
>>> group doc
lorem ipsumdolor sit ametlist :: [Doc ann] -> Doc annHaskell-inspired variant of encloseSep with braces and comma as
separator.
>>> let doc = list (map pretty [1,20,300,4000])>>> putDocW 80 doc
[1, 20, 300, 4000]>>> putDocW 10 doc
[ 1
, 20
, 300
, 4000 ]nest :: Int -> Doc ann -> Doc ann( lays out the document nest i x)x with the current nesting level
(indentation of the following lines) increased by i. Negative values are
allowed, and decrease the nesting level accordingly.
>>> vsep [nest 4 (vsep ["lorem", "ipsum", "dolor"]), "sit", "amet"]
lorem
ipsum
dolor
sit
ametSee also
hang (nest relative to current cursor position instead of
current nesting level)
align (set nesting level to current cursor position)
indent (increase indentation on the spot, padding with spaces).
IntChange of nesting level
nesting :: (Int -> Doc ann) -> Doc annLayout a document depending on the current nesting level. align is
implemented in terms of nesting.
>>> let doc = "prefix" <+> nesting (\l -> brackets ("Nested:" <+> pretty l))
>>> vsep [indent n doc | n <- [0,4,8]]
prefix [Nested: 0]
prefix [Nested: 4]
prefix [Nested: 8]pageWidth :: (PageWidth -> Doc ann) -> Doc annLayout a document depending on the page width, if one has been specified.
>>> let prettyPageWidth (AvailablePerLine l r) = "Width:" <+> pretty l <> ", ribbon fraction:" <+> pretty r
>>> let doc = "prefix" <+> pageWidth (brackets . prettyPageWidth)
>>> putDocW 32 (vsep [indent n doc | n <- [0,4,8]])
prefix [Width: 32, ribbon fraction: 1.0]
prefix [Width: 32, ribbon fraction: 1.0]
prefix [Width: 32, ribbon fraction: 1.0]plural :: (Num amount, Eq amount) => doc -> doc -> amount -> doc( is plural n one many)one if n is 1, and many otherwise. A
typical use case is adding a plural "s".
>>> let things = [True]
>>> let amount = length things
>>> pretty things <+> "has" <+> pretty amount <+> plural "entry" "entries" amount
[True] has 1 entrydoc1 case
docother cases
punctuate :: Doc ann -> [Doc ann] -> [Doc ann]( appends punctuate p xs)p to all but the last document in xs.
>>> let docs = punctuate comma (Util.words "lorem ipsum dolor sit amet")
>>> putDocW 80 (hsep docs)
lorem, ipsum, dolor, sit, ametThe separators are put at the end of the entries, which we can see if we position the result vertically:
>>> putDocW 20 (vsep docs)
lorem,
ipsum,
dolor,
sit,
ametIf you want put the commas in front of their elements instead of at the end,
you should use tupled or, in general, encloseSep.
Punctuation, e.g. comma
reAnnotate :: (ann -> ann') -> Doc ann -> Doc ann'Change the annotation of a Document.
Useful in particular to embed documents with one form of annotation in a more generally annotated document.
Since this traverses the entire tree, including parts that are not
rendered due to other layouts fitting better, it is preferable to reannotate
after producing the layout by using Doc.reAnnotateS
Since has the right type and satisfies reAnnotate'reAnnotate id = id',
it is used to define the instance of Functor.Doc
reAnnotateS :: (ann -> ann') -> SimpleDocStream ann -> SimpleDocStream ann'Change the annotation of a document. reAnnotate for SimpleDocStream.
removeTrailingWhitespace :: SimpleDocStream ann -> SimpleDocStream annRemove all trailing space characters.
This has some performance impact, because it does an entire additional pass
over the SimpleDocStream.
No trimming will be done inside annotations, which are considered to contain
no (trimmable) whitespace, since the annotation might actually be about the
whitespace, for example a renderer that colors the background of trailing
whitespace, as e.g. git diff can be configured to do.
Historical note: Since v1.7.0, layoutPretty and layoutSmart avoid
producing the trailing whitespace that was the original motivation for
creating removeTrailingWhitespace.
See https://github.com/quchen/prettyprinter/pull/139 for some background
info.
renderShowS :: SimpleDocStream ann -> ShowSRender a SimpleDocStream to a ShowS, useful to write Show instances
based on the prettyprinter.
instance 'Show' MyType where
'showsPrec' _ = 'renderShowS' . 'layoutPretty' 'defaultLayoutOptions' . 'pretty'
sep :: [Doc ann] -> Doc ann( tries laying out the documents sep xs)xs separated with spaces,
and if this does not fit the page, separates them with newlines. This is what
differentiates it from vsep, which always lays out its contents beneath
each other.
>>> let doc = "prefix" <+> sep ["text", "to", "lay", "out"]
>>> putDocW 80 doc
prefix text to lay outWith a narrower layout, the entries are separated by newlines:
>>> putDocW 20 doc
prefix text
to
lay
out'sep' = 'group' . 'vsep'
softline :: Doc annsoftline behaves like if the resulting output fits the page,
otherwise like space.line
Here, we have enough space to put everything in one line:
>>> let doc = "lorem ipsum" <> softline <> "dolor sit amet"
>>> putDocW 80 doc
lorem ipsum dolor sit ametIf we narrow the page to width 10, the layouter produces a line break:
>>> putDocW 10 doc
lorem ipsum
dolor sit amet'softline' = 'group' 'line'
softline' :: Doc ann is like softline', but behaves like softline if the
resulting output does not fit on the page (instead of mempty). In other
words, space is to line how line' is to softline.softline'
With enough space, we get direct concatenation:
>>> let doc = "ThisWord" <> softline' <> "IsWayTooLong"
>>> putDocW 80 doc
ThisWordIsWayTooLongIf we narrow the page to width 10, the layouter produces a line break:
>>> putDocW 10 doc
ThisWord
IsWayTooLong'softline'' = 'group' 'line''
surround :: Doc ann -> Doc ann -> Doc ann -> Doc ann( surrounds document surround x l r)x with l and r.
>>> surround "·" "A" "Z"
A·ZThis is merely an argument reordering of , but allows for
definitions likeenclose
>>> concatWith (surround dot) ["Prettyprinter", "Render", "Text"]
Prettyprinter.Render.TexttextSpaces :: Int -> TextA utility for producing indentation etc.
>>> textSpaces 3
" "This produces much better Core than the equivalent
T.replicate n " "tupled :: [Doc ann] -> Doc annHaskell-inspired variant of encloseSep with parentheses and comma as
separator.
>>> let doc = tupled (map pretty [1,20,300,4000])>>> putDocW 80 doc
(1, 20, 300, 4000)>>> putDocW 10 doc
( 1
, 20
, 300
, 4000 )unAnnotate :: Doc ann -> Doc xxxRemove all annotations.
Although unAnnotate is idempotent with respect to rendering,
'unAnnotate' . 'unAnnotate' = 'unAnnotate'
it should not be used without caution, for each invocation traverses the
entire contained document. If possible, it is preferable to unannotate after
producing the layout by using unAnnotateS.
unAnnotateS :: SimpleDocStream ann -> SimpleDocStream xxxRemove all annotations. unAnnotate for SimpleDocStream.
unsafeTextWithoutNewlines :: Text -> Doc ann(unsafeTextWithoutNewlines s) contains the literal string s.
The string must not contain any newline characters, since this is an
invariant of the Text constructor.
unsafeViaShow :: Show a => a -> Doc annConvenience function to convert a Showable value /that must not contain
newlines/ to a Doc. If there may be newlines, use viaShow instead.
vcat :: [Doc ann] -> Doc ann( vertically concatenates the documents vcat xs)xs. If it is
grouped, the line breaks are removed.
In other words is like vcat, with newlines removed instead of
replaced by vsepspaces.
>>> let docs = Util.words "lorem ipsum dolor"
>>> vcat docs
lorem
ipsum
dolor
>>> group (vcat docs)
loremipsumdolorSince grouping a vcat is rather common, cat is a built-in shortcut for
it.
viaShow :: Show a => a -> Doc annConvenience function to convert a Showable value to a Doc. If the
String does not contain newlines, consider using the more performant
unsafeViaShow.
vsep :: [Doc ann] -> Doc ann( concatenates all documents vsep xs)xs above each other. If a
group undoes the line breaks inserted by vsep, the documents are
separated with a space instead.
Using vsep alone yields
>>> "prefix" <+> vsep ["text", "to", "lay", "out"]
prefix text
to
lay
outgrouping a vsep separates the documents with a space if it fits the
page (and does nothing otherwise). See the convenience function for
this use case.sep
The align function can be used to align the documents under their first
element:
>>> "prefix" <+> align (vsep ["text", "to", "lay", "out"])
prefix text
to
lay
outSince grouping a vsep is rather common, sep is a built-in for doing
that.
width :: Doc ann -> (Int -> Doc ann) -> Doc ann( lays out the document width doc f)doc, and makes the column width
of it available to a function.
>>> let annotate doc = width (brackets doc) (\w -> " <- width:" <+> pretty w)
>>> align (vsep (map annotate ["---", "------", indent 3 "---", vsep ["---", indent 4 "---"]]))
[---] <- width: 5
[------] <- width: 8
[ ---] <- width: 8
[---
---] <- width: 8data Doc annThe abstract data type represents pretty documents that have
been annotated with data of type Doc annann.
More specifically, a value of type 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.Doc
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)
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
worldFail :: Doc annOccurs when flattening a line. The layouter will reject this document, choosing a more suitable rendering.
Empty :: Doc annChar :: Char -> Doc annText :: Int -> Text -> Doc annLine :: Doc annFlatAlt :: (Doc ann) -> (Doc ann) -> Doc annCat :: (Doc ann) -> (Doc ann) -> Doc annNest :: Int -> (Doc ann) -> Doc annUnion :: (Doc ann) -> (Doc ann) -> Doc annColumn :: (Int -> Doc ann) -> Doc annWithPageWidth :: (PageWidth -> Doc ann) -> Doc annNesting :: (Int -> Doc ann) -> Doc annAnnotated :: ann -> (Doc ann) -> Doc anndata FusionDepthFusion depth parameter, used by fuse.
Shallow :: FusionDepthDo not dive deep into nested documents, fusing mostly concatenations of text nodes together.
Deep :: FusionDepthnewtype LayoutOptionsOptions to influence the layout algorithms.
LayoutOptions :: PageWidth -> LayoutOptionslayoutPageWidth :: LayoutOptions -> PageWidthdata PageWidthMaximum number of characters that fit in one line. The layout algorithms
will try not to exceed the set limit by inserting line breaks when applicable
(e.g. via softline').
AvailablePerLine :: Int -> Double -> PageWidthDoubleLayouters should not exceed the specified space per line.
The Int is the number of characters, including whitespace, that
fit in a line. A typical value is 80.
The Double is the ribbon with, i.e. the fraction of the total
page width that can be printed on. This allows limiting the length
of printable text per line. Values must be between 0 and 1, and
0.4 to 1 is typical.
Unbounded :: PageWidthLayouters should not introduce line breaks on their own.
class Pretty aOverloaded conversion to Doc.
Laws:
output should be pretty. :-)
pretty :: a -> Doc ann>>> pretty 1 <+> pretty "hello" <+> pretty 1.234
1 hello 1.234prettyList :: [a] -> Doc ann is only used to define the @instance
prettyListPretty a => Pretty [a]. In normal circumstances only the pretty@
function is used.
>>> prettyList [1, 23, 456]
[1, 23, 456]class PrettyAnn ann aThis class is similar to Pretty, but allows you to embed annotations in
the Doc.
prettyAnn :: a -> Doc annprettyAnnList :: [a] -> Doc anndata SimpleDocStream annThe data type SimpleDocStream represents laid out documents and is used
by the display functions.
A simplified view is that , and the layout
functions pick one of the Doc = [SimpleDocStream]SimpleDocStreams based on which one fits the
layout constraints best. This means that SimpleDocStream has all complexity
contained in Doc resolved, making it very easy to convert it to other
formats, such as plain text or terminal output.
To write your own to X converter, it is therefore sufficient to
convert from Doc. The »Render« submodules provide some
built-in converters to do so, and helpers to create own ones.SimpleDocStream
SFail :: SimpleDocStream annSEmpty :: SimpleDocStream annSChar :: Char -> (SimpleDocStream ann) -> SimpleDocStream annSText :: Int -> Text -> (SimpleDocStream ann) -> SimpleDocStream annSLine :: Int -> (SimpleDocStream ann) -> SimpleDocStream annSAnnPush :: ann -> (SimpleDocStream ann) -> SimpleDocStream annSAnnPop :: (SimpleDocStream ann) -> SimpleDocStream annCommunity contribution. The service has not verified this documentation. The generator reported 21 diagnostics.
Target: Linux on x86-64. Some inferred types and resolved links are not available.
Generator: aihc-haddock-858b1951-web-1
Metadata snapshot: 19f659e0fd2a9606431ae6a3ec65f9ace725052e19ca6a9f9e9f2759e30fd129
Open the dependency plan · Download the documentation model