packages feed

prettyprinter 1.0.1 → 1.1

raw patch · 4 files changed

+32/−31 lines, 4 filesdep ~doctestdep ~template-haskell

Dependency ranges changed: doctest, template-haskell

Files

CONTRIBUTORS.md view
@@ -7,8 +7,6 @@  ## `prettyprinter` library (this one) -- Moritz Kiefer, @cocreature-- Daniel Mendler, @minad – several ideas after the preliminary release - David Luposchainsky, @quchen – current maintainer  ## `ansi-wl-pprint` (origin forked from)
prettyprinter.cabal view
@@ -1,5 +1,5 @@ name:                prettyprinter-version:             1.0.1+version:             1.1 cabal-version:       >= 1.10 category:            User Interfaces, Text synopsis:            A modern, easy to use, well-documented, extensible prettyprinter.
src/Data/Text/Prettyprint/Doc/Internal.hs view
@@ -203,17 +203,6 @@ instance Pretty a => Pretty (NonEmpty a) where     pretty (x:|xs) = prettyList (x:xs) --- | 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-instance Pretty (Doc ann) where-    pretty = unAnnotate- -- | >>> pretty () -- () --@@ -1314,7 +1303,7 @@ -- entire contained document. If possible, it is preferrable to unannotate after -- producing the layout by using 'unAnnotateS'. unAnnotate :: Doc ann -> Doc xxx-unAnnotate = alterAnnotations (const Nothing)+unAnnotate = alterAnnotations (const [])  -- | Change the annotation of a 'Doc'ument. --@@ -1328,19 +1317,24 @@ -- Since @'reAnnotate'@ has the right type and satisfies @'reAnnotate id = id'@, -- it is used to define the @'Functor'@ instance of @'Doc'@. reAnnotate :: (ann -> ann') -> Doc ann -> Doc ann'-reAnnotate re = alterAnnotations (Just . re)+reAnnotate re = alterAnnotations (pure . re) --- | Change the annotation of a 'Doc'ument to a different one, or none at all.+-- | Change the annotations of a 'Doc'ument. 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«), because some backends may not--- care about certain annotations, while others may.+-- 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 @'Doc'@ tree, including parts that are not -- rendered due to other layouts fitting better, it is preferrable to reannotate -- after producing the layout by using @'alterAnnotationsS'@.-alterAnnotations :: (ann -> Maybe ann') -> Doc ann -> Doc ann'+alterAnnotations :: (ann -> [ann']) -> Doc ann -> Doc ann' alterAnnotations re = go   where     go = \case@@ -1357,10 +1351,14 @@         Column f        -> Column (go . f)         WithPageWidth f -> WithPageWidth (go . f)         Nesting f       -> Nesting (go . f)-        Annotated ann x -> case re ann of-            Nothing   -> go x-            Just ann' -> Annotated ann' (go x)+        Annotated ann x -> foldr Annotated (go x) (re ann) +-- $+-- >>> let doc = "lorem" <+> annotate () "ipsum" <+> "dolor"+-- >>> let re () = ["FOO", "BAR"]+-- >>> layoutPretty defaultLayoutOptions (alterAnnotations re doc)+-- SText 5 "lorem" (SChar ' ' (SAnnPush "FOO" (SAnnPush "BAR" (SText 5 "ipsum" (SAnnPop (SAnnPop (SChar ' ' (SText 5 "dolor" SEmpty))))))))+ -- | Remove all annotations. 'unAnnotate' for 'SimpleDocStream'. unAnnotateS :: SimpleDocStream ann -> SimpleDocStream xxx unAnnotateS = alterAnnotationsS (const Nothing)@@ -1371,6 +1369,11 @@  -- | 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.+-- ('Data.Text.Prettyprint.Doc.Render.Util.SimpleDocTree.SimpleDocTree' restores+-- this flexibility again.) alterAnnotationsS :: (ann -> Maybe ann') -> SimpleDocStream ann -> SimpleDocStream ann' alterAnnotationsS re = go   where@@ -1777,8 +1780,6 @@ -- indentation. 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.------ This layout function does not add any colorisation information. -- -- >>> let doc = hang 4 (vsep ["lorem", "ipsum", hang 4 (vsep ["dolor", "sit"])]) -- >>> doc
src/Data/Text/Prettyprint/Doc/Render/Util/SimpleDocTree.hs view
@@ -217,15 +217,19 @@  -- | Remove all annotations. 'unAnnotate' for 'SimpleDocTree'. unAnnotateST :: SimpleDocTree ann -> SimpleDocTree xxx-unAnnotateST = alterAnnotationsST (const Nothing)+unAnnotateST = alterAnnotationsST (const [])  -- | Change the annotation of a document. 'reAnnotate' for 'SimpleDocTree'. reAnnotateST :: (ann -> ann') -> SimpleDocTree ann -> SimpleDocTree ann'-reAnnotateST f = alterAnnotationsST (Just . f)+reAnnotateST f = alterAnnotationsST (pure . f)  -- | Change the annotation of a document to a different annotation, or none at -- all. 'alterAnnotations' for 'SimpleDocTree'.-alterAnnotationsST :: (ann -> Maybe ann') -> SimpleDocTree ann -> SimpleDocTree ann'+--+-- Note that this is as powerful as 'alterAnnotations', allowing one annotation+-- to become multiple ones, contrary to 'alterAnnotationsS', which cannot do+-- this.+alterAnnotationsST :: (ann -> [ann']) -> SimpleDocTree ann -> SimpleDocTree ann' alterAnnotationsST re = go   where     go = \case@@ -234,9 +238,7 @@         STText l t     -> STText l t         STLine i       -> STLine i         STConcat xs    -> STConcat (map go xs)-        STAnn ann rest -> case re ann of-            Nothing   -> go rest-            Just ann' -> STAnn ann' (go rest)+        STAnn ann rest -> Prelude.foldr STAnn (go rest) (re ann)  -- | Collect all annotations from a document. instance Foldable SimpleDocTree where