prettyprinter-1.7.1: A modern, easy to use, well-documented, extensible pretty-printer.

Safe Haskell | Safe-Inferred
Language | Haskell2010

Prettyprinter.Render.Util.SimpleDocTree

Contents

- Type and conversion
- Manipulating annotations
- Common use case shortcut definitions

Description

Conversion of the linked-list-like `SimpleDocStream` to a tree-like `SimpleDocTree`.

Synopsis

- data SimpleDocTree ann
  - = STEmpty
  - | STChar Char
  - | STText !Int Text
  - | STLine !Int
  - | STAnn ann (SimpleDocTree ann)
  - | STConcat [SimpleDocTree ann]
- treeForm :: SimpleDocStream ann -> SimpleDocTree ann
- unAnnotateST :: SimpleDocTree ann -> SimpleDocTree xxx
- reAnnotateST :: (ann -> ann') -> SimpleDocTree ann -> SimpleDocTree ann'
- alterAnnotationsST :: (ann -> [ann']) -> SimpleDocTree ann -> SimpleDocTree ann'
- renderSimplyDecorated :: Monoid out => (Text -> out) -> (ann -> out -> out) -> SimpleDocTree ann -> out
- renderSimplyDecoratedA :: (Applicative f, Monoid out) => (Text -> f out) -> (ann -> f out -> f out) -> SimpleDocTree ann -> f out

# Type and conversion

data SimpleDocTree ann #

A `SimpleDocStream` is a linked list of different annotated cons cells (`SText` and then some further `SimpleDocStream`, `SLine` and then some further `SimpleDocStream`, …). This format is very suitable as a target for a layout engine, but not very useful for rendering to a structured format such as HTML, where we don’t want to do a lookahead until the end of some markup. These formats benefit from a tree-like structure that explicitly marks its contents as annotated. `SimpleDocTree` is that format.

Constructors

STEmpty |
STChar Char |
STText !Int Text | `length` is O(n), so we cache it in the `Int` field.
STLine !Int | `Int` = indentation level for the (next) line
STAnn ann (SimpleDocTree ann) | Annotate the contained document.
STConcat [SimpleDocTree ann] | Horizontal concatenation of multiple documents.

#### Instances

Instances details

Foldable SimpleDocTree # | Collect all annotations from a document.

Instance details

Defined in Prettyprinter.Render.Util.SimpleDocTree

Methods

fold :: Monoid m => SimpleDocTree m -> m

foldMap :: Monoid m => (a -> m) -> SimpleDocTree a -> m

foldMap' :: Monoid m => (a -> m) -> SimpleDocTree a -> m

foldr :: (a -> b -> b) -> b -> SimpleDocTree a -> b

foldr' :: (a -> b -> b) -> b -> SimpleDocTree a -> b

foldl :: (b -> a -> b) -> b -> SimpleDocTree a -> b

foldl' :: (b -> a -> b) -> b -> SimpleDocTree a -> b

foldr1 :: (a -> a -> a) -> SimpleDocTree a -> a

foldl1 :: (a -> a -> a) -> SimpleDocTree a -> a

toList :: SimpleDocTree a -> [a]

null :: SimpleDocTree a -> Bool

length :: SimpleDocTree a -> Int

elem :: Eq a => a -> SimpleDocTree a -> Bool

maximum :: Ord a => SimpleDocTree a -> a

minimum :: Ord a => SimpleDocTree a -> a

sum :: Num a => SimpleDocTree a -> a

product :: Num a => SimpleDocTree a -> a

Traversable SimpleDocTree # | Transform a document based on its annotations, possibly leveraging `Applicative` effects.
Instance details Defined in Prettyprinter.Render.Util.SimpleDocTree Methods traverse :: Applicative f => (a -> f b) -> SimpleDocTree a -> f (SimpleDocTree b) sequenceA :: Applicative f => SimpleDocTree (f a) -> f (SimpleDocTree a) mapM :: Monad m => (a -> m b) -> SimpleDocTree a -> m (SimpleDocTree b) sequence :: Monad m => SimpleDocTree (m a) -> m (SimpleDocTree a)
Functor SimpleDocTree # | Alter the document’s annotations. This instance makes `SimpleDocTree` more flexible (because it can be used in `Functor`-polymorphic values), but ``fmap`` is much less readable compared to using ``reAnnotateST`` in code that only works for ``SimpleDocTree`` anyway. Consider using the latter when the type does not matter.
Instance details Defined in Prettyprinter.Render.Util.SimpleDocTree Methods fmap :: (a -> b) -> SimpleDocTree a -> SimpleDocTree b (<$) :: a -> SimpleDocTree b -> SimpleDocTree a
Generic (SimpleDocTree ann) # |
Instance details Defined in Prettyprinter.Render.Util.SimpleDocTree Associated Types type Rep (SimpleDocTree ann) :: Type -> Type Methods from :: SimpleDocTree ann -> Rep (SimpleDocTree ann) x to :: Rep (SimpleDocTree ann) x -> SimpleDocTree ann
Show ann => Show (SimpleDocTree ann) # |
Instance details Defined in Prettyprinter.Render.Util.SimpleDocTree Methods showsPrec :: Int -> SimpleDocTree ann -> ShowS show :: SimpleDocTree ann -> String showList :: [SimpleDocTree ann] -> ShowS
Eq ann => Eq (SimpleDocTree ann) # |
Instance details Defined in Prettyprinter.Render.Util.SimpleDocTree Methods (==) :: SimpleDocTree ann -> SimpleDocTree ann -> Bool (/=) :: SimpleDocTree ann -> SimpleDocTree ann -> Bool
Ord ann => Ord (SimpleDocTree ann) # |

Instance details

Defined in Prettyprinter.Render.Util.SimpleDocTree

Methods

compare :: SimpleDocTree ann -> SimpleDocTree ann -> Ordering

(<) :: SimpleDocTree ann -> SimpleDocTree ann -> Bool

(<=) :: SimpleDocTree ann -> SimpleDocTree ann -> Bool

(>) :: SimpleDocTree ann -> SimpleDocTree ann -> Bool

(>=) :: SimpleDocTree ann -> SimpleDocTree ann -> Bool

max :: SimpleDocTree ann -> SimpleDocTree ann -> SimpleDocTree ann

min :: SimpleDocTree ann -> SimpleDocTree ann -> SimpleDocTree ann

type Rep (SimpleDocTree ann) # |

Instance details

Defined in Prettyprinter.Render.Util.SimpleDocTree

type Rep (SimpleDocTree ann) = D1 ('MetaData "SimpleDocTree" "Prettyprinter.Render.Util.SimpleDocTree" "prettyprinter-1.7.1-GuAcpDZRBAeGkZlhvyZh0C" 'False) ((C1 ('MetaCons "STEmpty" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "STChar" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Char)) :+: C1 ('MetaCons "STText" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Text)))) :+: (C1 ('MetaCons "STLine" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Int)) :+: (C1 ('MetaCons "STAnn" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ann) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (SimpleDocTree ann))) :+: C1 ('MetaCons "STConcat" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [SimpleDocTree ann])))))

treeForm :: SimpleDocStream ann -> SimpleDocTree ann #

Convert a `SimpleDocStream` to its `SimpleDocTree` representation.

# Manipulating annotations

unAnnotateST :: SimpleDocTree ann -> SimpleDocTree xxx #

Remove all annotations. `unAnnotate` for `SimpleDocTree`.

reAnnotateST :: (ann -> ann') -> SimpleDocTree ann -> SimpleDocTree ann' #

Change the annotation of a document. `reAnnotate` for `SimpleDocTree`.

alterAnnotationsST :: (ann -> [ann']) -> SimpleDocTree ann -> SimpleDocTree ann' #

Change the annotation of a document to a different annotation, or none at all. `alterAnnotations` for `SimpleDocTree`.

Note that this is as powerful as `alterAnnotations`, allowing one annotation to become multiple ones, contrary to `alterAnnotationsS`, which cannot do this.

# Common use case shortcut definitions

renderSimplyDecorated #

Arguments

:: Monoid out |
=> (Text -> out) | Render plain `Doc`
-> (ann -> out -> out) | How to modify an element with an annotation
-> SimpleDocTree ann |
-> out |

Simplest possible tree-based renderer.

For example, here is a document annotated with `()`, and the behaviour is to surround annotated regions with »>>>« and »<<<«:

    >>> let doc = "hello" <+> annotate () "world" <> "!"
    >>> let stdoc = treeForm (layoutPretty defaultLayoutOptions doc)
    >>> T.putStrLn (renderSimplyDecorated id (\() x -> ">>>" <> x <> "<<<") stdoc)
    hello >>>world<<<!

renderSimplyDecoratedA #

Arguments

:: (Applicative f, Monoid out) |
=> (Text -> f out) | Render plain `Doc`
-> (ann -> f out -> f out) | How to modify an element with an annotation
-> SimpleDocTree ann |
-> f out |

Version of `renderSimplyDecoratedA` that allows for `Applicative` effects.