| Safe Haskell | Safe-Inferred |
|---|---|
| Language | Haskell2010 |
Data.Fmt.Tree
Description
Pretty-printer API over Doc document trees.
Smart constructors, combinators, layout algorithms, and rendering.
Synopsis
- fail_ :: Tree m ann
- emptyDoc :: Tree m ann
- leaf :: Int -> m -> Tree m ann
- hardline :: Tree m ann
- line :: IsString m => Tree m ann
- line' :: Tree m ann
- flatAlt :: Tree m ann -> Tree m ann -> Tree m ann
- nest :: Int -> Tree m ann -> Tree m ann
- union :: Tree m ann -> Tree m ann -> Tree m ann
- annotate :: ann -> Tree m ann -> Tree m ann
- column :: (Int -> Tree m ann) -> Tree m ann
- nesting :: (Int -> Tree m ann) -> Tree m ann
- flatten :: Tree m ann -> Tree m ann
- group :: Tree m ann -> Tree m ann
- align :: Tree m ann -> Tree m ann
- softline :: IsString m => Tree m ann
- softline' :: Tree m ann
- (<+>) :: IsString m => Tree m ann -> Tree m ann -> Tree m ann
- concatWith :: (Tree m ann -> Tree m ann -> Tree m ann) -> [Tree m ann] -> Tree m ann
- hsep :: IsString m => [Tree m ann] -> Tree m ann
- vsep :: IsString m => [Tree m ann] -> Tree m ann
- fillSep :: IsString m => [Tree m ann] -> Tree m ann
- sep :: IsString m => [Tree m ann] -> Tree m ann
- hcat :: [Tree m ann] -> Tree m ann
- vcat :: [Tree m ann] -> Tree m ann
- fillCat :: [Tree m ann] -> Tree m ann
- cat :: [Tree m ann] -> Tree m ann
- hang :: Int -> Tree m ann -> Tree m ann
- indent :: IsString m => Int -> Tree m ann -> Tree m ann
- surround :: Tree m ann -> Tree m ann -> Tree m ann -> Tree m ann
- encloseSep :: IsString m => Tree m ann -> Tree m ann -> Tree m ann -> [Tree m ann] -> Tree m ann
- list :: IsString m => [Tree m ann] -> Tree m ann
- tupled :: IsString m => [Tree m ann] -> Tree m ann
- punctuate :: Tree m ann -> [Tree m ann] -> [Tree m ann]
- width :: Tree m ann -> (Int -> Tree m ann) -> Tree m ann
- fill :: IsString m => Int -> Tree m ann -> Tree m ann
- fillBreak :: IsString m => Int -> Tree m ann -> Tree m ann
- reAnnotate :: (ann -> ann') -> Tree m ann -> Tree m ann'
- unAnnotate :: Tree m ann -> Tree m ann'
- alterAnnotations :: (ann -> [ann']) -> Tree m ann -> Tree m ann'
- data FlattenResult a
- = Flattened a
- | AlreadyFlat
- | NeverFlat
- changesUponFlattening :: Tree m ann -> FlattenResult (Tree m ann)
- group' :: Tree m ann -> Tree m ann
- fuse :: Semigroup m => Tree m ann -> Tree m ann
- removeTrailingWhitespace :: String -> String
- data Token m ann
- data PageWidth
- newtype LayoutOptions = LayoutOptions {}
- defaultLayoutOptions :: LayoutOptions
- layoutPretty :: LayoutOptions -> Tree m ann -> [Token m ann]
- layoutSmart :: LayoutOptions -> Tree m ann -> [Token m ann]
- layoutCompact :: Tree m ann -> [Token m ann]
- layoutStream :: LayoutOptions -> Tree m ann -> Nu (Cons (Token m ann))
- renderStream :: (Monoid m, IsString m) => Nu (Cons (Token m ann)) -> m
- prettyStream :: (Monoid m, IsString m) => LayoutOptions -> Tree m ann -> m
- render :: (Monoid m, IsString m) => [Token m ann] -> m
- pretty :: (Monoid m, IsString m) => LayoutOptions -> Tree m ann -> m
Smart constructors
flatAlt :: Tree m ann -> Tree m ann -> Tree m ann Source #
flatAlt default flat: use default normally,
flat when flattened by group.
union :: Tree m ann -> Tree m ann -> Tree m ann Source #
Layout alternatives. Invariant: first argument is the flattened form of the second.
Combinators
group :: Tree m ann -> Tree m ann Source #
Try to lay out the document on a single line. Falls back to the original if flattening fails.
group x = union (flatten x) x
align :: Tree m ann -> Tree m ann Source #
Lay out relative to the current column rather than the current nesting level.
align d = column (\k -> nesting (\i -> nest (k - i) d))
Line breaks
softline :: IsString m => Tree m ann Source #
A line break that behaves like a space when flattened by group.
softline = group line
softline' :: Tree m ann Source #
A line break that vanishes when flattened by group.
softline' = group line'
Separators
(<+>) :: IsString m => Tree m ann -> Tree m ann -> Tree m ann infixr 6 Source #
Concatenate with a space in between.
concatWith :: (Tree m ann -> Tree m ann -> Tree m ann) -> [Tree m ann] -> Tree m ann Source #
Concatenate documents using a binary operator.
Concatenation
Indentation
indent :: IsString m => Int -> Tree m ann -> Tree m ann Source #
indent i doc inserts i spaces then aligns.
Enclosure
surround :: Tree m ann -> Tree m ann -> Tree m ann -> Tree m ann Source #
surround mid left right = left <> mid <> right
encloseSep :: IsString m => Tree m ann -> Tree m ann -> Tree m ann -> [Tree m ann] -> Tree m ann Source #
Enclose a list with separators.
encloseSep lbrace rbrace comma [a, b, c] = lbrace <> a <> comma <> b <> comma <> c <> rbrace
When the content fits, renders on one line. Otherwise, each element gets its own line, aligned.
punctuate :: Tree m ann -> [Tree m ann] -> [Tree m ann] Source #
Append a separator to all but the last element.
Filling
width :: Tree m ann -> (Int -> Tree m ann) -> Tree m ann Source #
width doc f renders doc then passes its rendered width to f.
fill :: IsString m => Int -> Tree m ann -> Tree m ann Source #
fill n doc pads doc to width n with spaces.
fillBreak :: IsString m => Int -> Tree m ann -> Tree m ann Source #
fillBreak n doc pads or breaks after doc if it exceeds n.
Annotations
reAnnotate :: (ann -> ann') -> Tree m ann -> Tree m ann' Source #
Map over annotations.
unAnnotate :: Tree m ann -> Tree m ann' Source #
Remove all annotations.
alterAnnotations :: (ann -> [ann']) -> Tree m ann -> Tree m ann' Source #
Alter annotations, potentially adding or removing layers.
Optimized group
data FlattenResult a Source #
Result of checking whether flattening changes a document.
Constructors
| Flattened a | Flattening produces a different document |
| AlreadyFlat | Document is already flat (no FlatAlt/Line) |
| NeverFlat | Document can never be flattened (bare Line) |
Instances
| Functor FlattenResult Source # | |
Defined in Data.Fmt.Tree Methods fmap :: (a -> b) -> FlattenResult a -> FlattenResult b # (<$) :: a -> FlattenResult b -> FlattenResult a # | |
| Show a => Show (FlattenResult a) Source # | |
Defined in Data.Fmt.Tree Methods showsPrec :: Int -> FlattenResult a -> ShowS # show :: FlattenResult a -> String # showList :: [FlattenResult a] -> ShowS # | |
| Eq a => Eq (FlattenResult a) Source # | |
Defined in Data.Fmt.Tree Methods (==) :: FlattenResult a -> FlattenResult a -> Bool # (/=) :: FlattenResult a -> FlattenResult a -> Bool # | |
changesUponFlattening :: Tree m ann -> FlattenResult (Tree m ann) Source #
Check whether flattening changes a document, and if so, produce the flattened version.
This is the key optimization for group': by checking first,
we avoid creating unnecessary Union nodes.
Uses direct recursion via unwrap (matching prettyprinter's
approach). A zygomorphism formulation is possible but less
readable.
Fusion
Trailing whitespace
removeTrailingWhitespace :: String -> String Source #
Remove trailing whitespace from rendered output.
Drops spaces at the end of each line. Applied as a post-processing step on the rendered string.
Tokens
A single rendered token. The output of the layout algorithm.
Constructors
| TLeaf !Int !m | Content with display width |
| TLine !Int | Newline followed by |
| TAnnPush ann | Begin annotation scope |
| TAnnPop | End annotation scope |
Layout
Page width configuration.
Constructors
| AvailablePerLine !Int !Double | AvailablePerLine maxColumns ribbonFraction |
| Unbounded |
Instances
newtype LayoutOptions Source #
Layout options.
Constructors
| LayoutOptions | |
Fields | |
Instances
| Show LayoutOptions Source # | |
Defined in Data.Fmt.Tree Methods showsPrec :: Int -> LayoutOptions -> ShowS # show :: LayoutOptions -> String # showList :: [LayoutOptions] -> ShowS # | |
| Eq LayoutOptions Source # | |
Defined in Data.Fmt.Tree Methods (==) :: LayoutOptions -> LayoutOptions -> Bool # (/=) :: LayoutOptions -> LayoutOptions -> Bool # | |
defaultLayoutOptions :: LayoutOptions Source #
80 columns, ribbon fraction 1.0.
layoutPretty :: LayoutOptions -> Tree m ann -> [Token m ann] Source #
Wadler/Leijen layout with one-line lookahead.
At Union nodes, tries the flattened (first) branch. If the
remainder of the line fits within the page width, uses it.
Otherwise falls back to the second (default) branch.
layoutSmart :: LayoutOptions -> Tree m ann -> [Token m ann] Source #
Smart layout with multi-line lookahead.
Like layoutPretty, but the fitting predicate checks beyond
the first line break — it continues checking until it finds a
line that starts at the same or shallower indentation level.
This prevents surprises where content looks like it fits but
subsequent lines overflow.
Use this for deeply nested structures where layoutPretty
makes suboptimal choices.
layoutCompact :: Tree m ann -> [Token m ann] Source #
Streaming layout
layoutStream :: LayoutOptions -> Tree m ann -> Nu (Cons (Token m ann)) Source #
Wadler/Leijen layout producing a Nu stream.
Unlike layoutPretty which materializes a [Token] list,
layoutStream produces a Nu (Cons (Token m ann)) — a seed
+ step function that generates tokens lazily on demand.
Construction is O(1); tokens are computed as they are consumed.
renderStream :: (Monoid m, IsString m) => Nu (Cons (Token m ann)) -> m Source #
Render a Nu token stream to the output monoid.
prettyStream :: (Monoid m, IsString m) => LayoutOptions -> Tree m ann -> m Source #
Streaming layout + render.
prettyStream opts = renderStream . layoutStream opts