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 Source #

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

Functor SimpleDocTree Source # | 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 Source # (<$) :: a -> SimpleDocTree b -> SimpleDocTree a Source #
Foldable SimpleDocTree Source # | Collect all annotations from a document.

Instance details

Defined in Prettyprinter.Render.Util.SimpleDocTree

Methods

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

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

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

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

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

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

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

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

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

toList :: SimpleDocTree a -> [a] Source #

null :: SimpleDocTree a -> Bool Source #

length :: SimpleDocTree a -> Int Source #

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

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

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

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

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

Traversable SimpleDocTree Source # | 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) Source #

sequenceA :: Applicative f => SimpleDocTree (f a) -> f (SimpleDocTree a) Source #

mapM :: Monad m => (a -> m b) -> SimpleDocTree a -> m (SimpleDocTree b) Source #

sequence :: Monad m => SimpleDocTree (m a) -> m (SimpleDocTree a) Source #

Generic (SimpleDocTree ann) Source # |

Instance details

Defined in Prettyprinter.Render.Util.SimpleDocTree

Associated Types

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-K35zp2YrYoHG4xnuHsN7jV" '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])))))

Methods

from :: SimpleDocTree ann -> Rep (SimpleDocTree ann) x Source #

to :: Rep (SimpleDocTree ann) x -> SimpleDocTree ann Source #

Show ann => Show (SimpleDocTree ann) Source # |
Instance details Defined in Prettyprinter.Render.Util.SimpleDocTree Methods showsPrec :: Int -> SimpleDocTree ann -> ShowS Source # show :: SimpleDocTree ann -> String Source # showList :: [SimpleDocTree ann] -> ShowS Source #
Eq ann => Eq (SimpleDocTree ann) Source # |
Instance details Defined in Prettyprinter.Render.Util.SimpleDocTree Methods (==) :: SimpleDocTree ann -> SimpleDocTree ann -> Bool Source # (/=) :: SimpleDocTree ann -> SimpleDocTree ann -> Bool Source #
Ord ann => Ord (SimpleDocTree ann) Source # |

Instance details

Defined in Prettyprinter.Render.Util.SimpleDocTree

Methods

compare :: SimpleDocTree ann -> SimpleDocTree ann -> Ordering Source #

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

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

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

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

max :: SimpleDocTree ann -> SimpleDocTree ann -> SimpleDocTree ann Source #

min :: SimpleDocTree ann -> SimpleDocTree ann -> SimpleDocTree ann Source #

type Rep (SimpleDocTree ann) Source # |

Instance details

Defined in Prettyprinter.Render.Util.SimpleDocTree

type Rep (SimpleDocTree ann) = D1 ('MetaData "SimpleDocTree" "Prettyprinter.Render.Util.SimpleDocTree" "prettyprinter-1.7.1-K35zp2YrYoHG4xnuHsN7jV" '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 Source #

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

# Manipulating annotations

unAnnotateST :: SimpleDocTree ann -> SimpleDocTree xxx Source #

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

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

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

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

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 Source #

Arguments

:: Monoid out |
=> (Text -> out) | Render plain `Text`
-> (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 Source #

Arguments

:: (Applicative f, Monoid out) |
=> (Text -> f out) | Render plain `Text`
-> (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.