{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# OPTIONS_HADDOCK not-home #-}
#include "version-compatibility-macros.h"
module Prettyprinter.Internal (
Doc(..),
Pretty(..),
viaShow, unsafeViaShow, unsafeTextWithoutNewlines,
emptyDoc, nest, line, line', softline, softline', hardline,
group, flatAlt,
align, hang, indent, encloseSep, list, tupled,
(<+>),
concatWith,
hsep, vsep, fillSep, sep,
hcat, vcat, fillCat, cat,
punctuate,
column, nesting, width, pageWidth,
fill, fillBreak,
plural, enclose, surround,
annotate,
unAnnotate,
reAnnotate,
alterAnnotations,
unAnnotateS,
reAnnotateS,
alterAnnotationsS,
fuse, FusionDepth(..),
SimpleDocStream(..),
PageWidth(..), defaultPageWidth,
LayoutOptions(..), defaultLayoutOptions,
layoutPretty, layoutCompact, layoutSmart,
removeTrailingWhitespace,
renderShowS,
textSpaces
) where
import Control.Applicative
import Data.Int
import Data.List.NonEmpty (NonEmpty (..))
import Data.Maybe
import Data.String (IsString (..))
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.Lazy as Lazy
import Data.Typeable (Typeable)
import Data.Void
import Data.Word
import GHC.Generics (Generic)
import Data.Semigroup
#if NATURAL_IN_BASE
import Numeric.Natural
#endif
#if !(FOLDABLE_TRAVERSABLE_IN_PRELUDE)
import Data.Foldable (Foldable (..))
import Data.Traversable (Traversable (..))
import Prelude hiding (foldr, foldr1)
#endif
#if FUNCTOR_IDENTITY_IN_BASE
import Data.Functor.Identity
#endif
import Prettyprinter.Render.Util.Panic
data Doc ann =
Fail
| Empty
| Char !Char
| Text !Int !Text
| Line
| FlatAlt (Doc ann) (Doc ann)
| Cat (Doc ann) (Doc ann)
| Nest !Int (Doc ann)
| Union (Doc ann) (Doc ann)
| Column (Int -> Doc ann)
| WithPageWidth (PageWidth -> Doc ann)
| Nesting (Int -> Doc ann)
| Annotated ann (Doc ann)
deriving ((forall x. Doc ann -> Rep (Doc ann) x)
-> (forall x. Rep (Doc ann) x -> Doc ann) -> Generic (Doc ann)
forall x. Rep (Doc ann) x -> Doc ann
forall x. Doc ann -> Rep (Doc ann) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall ann x. Rep (Doc ann) x -> Doc ann
forall ann x. Doc ann -> Rep (Doc ann) x
$cfrom :: forall ann x. Doc ann -> Rep (Doc ann) x
from :: forall x. Doc ann -> Rep (Doc ann) x
$cto :: forall ann x. Rep (Doc ann) x -> Doc ann
to :: forall x. Rep (Doc ann) x -> Doc ann
Generic, Typeable)
instance Semigroup (Doc ann) where
<> :: Doc ann -> Doc ann -> Doc ann
(<>) = Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat
sconcat :: NonEmpty (Doc ann) -> Doc ann
sconcat (Doc ann
x :| [Doc ann]
xs) = [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
hcat (Doc ann
xDoc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
:[Doc ann]
xs)
stimes :: forall b. Integral b => b -> Doc ann -> Doc ann
stimes b
n Doc ann
x
| b
n b -> b -> Bool
forall a. Ord a => a -> a -> Bool
<= b
0 = Doc ann
forall ann. Doc ann
Empty
| b
n b -> b -> Bool
forall a. Eq a => a -> a -> Bool
== b
1 = Doc ann
x
| Bool
otherwise =
let n' :: Int
n' = b -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral b
n
nx :: Doc ann
nx = [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
hcat (Int -> Doc ann -> [Doc ann]
forall a. Int -> a -> [a]
replicate Int
n' Doc ann
x)
in case Doc ann
x of
Doc ann
Fail -> Doc ann
forall ann. Doc ann
Fail
Doc ann
Empty -> Doc ann
forall ann. Doc ann
Empty
Char Char
c -> Int -> Text -> Doc ann
forall ann. Int -> Text -> Doc ann
Text Int
n' (Int -> Text -> Text
T.replicate Int
n' (Char -> Text
T.singleton Char
c))
Text Int
l Text
t -> Int -> Text -> Doc ann
forall ann. Int -> Text -> Doc ann
Text (Int
n' Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
l) (Int -> Text -> Text
T.replicate Int
n' Text
t)
Doc ann
Line -> Doc ann
nx
FlatAlt{} -> Doc ann
nx
Cat{} -> Doc ann
nx
Nest{} -> Doc ann
nx
Union{} -> Doc ann
nx
Column{} -> Doc ann
nx
WithPageWidth{} -> Doc ann
nx
Nesting{} -> Doc ann
nx
Annotated{} -> Doc ann
nx
instance Monoid (Doc ann) where
mempty :: Doc ann
mempty = Doc ann
forall ann. Doc ann
emptyDoc
mappend :: Doc ann -> Doc ann -> Doc ann
mappend = Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
(<>)
mconcat :: [Doc ann] -> Doc ann
mconcat = [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
hcat
instance IsString (Doc ann) where
fromString :: String -> Doc ann
fromString = Text -> Doc ann
forall ann. Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Text -> Doc ann) -> (String -> Text) -> String -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack
instance Functor Doc where
fmap :: forall a b. (a -> b) -> Doc a -> Doc b
fmap = (a -> b) -> Doc a -> Doc b
forall a b. (a -> b) -> Doc a -> Doc b
reAnnotate
class Pretty a where
pretty :: a -> Doc ann
default pretty :: Show a => a -> Doc ann
pretty = a -> Doc ann
forall a ann. Show a => a -> Doc ann
viaShow
prettyList :: [a] -> Doc ann
prettyList = Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
align (Doc ann -> Doc ann) -> ([a] -> Doc ann) -> [a] -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
list ([Doc ann] -> Doc ann) -> ([a] -> [Doc ann]) -> [a] -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Doc ann) -> [a] -> [Doc ann]
forall a b. (a -> b) -> [a] -> [b]
map a -> Doc ann
forall ann. a -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty
{-# MINIMAL pretty #-}
instance Pretty a => Pretty (Const a b) where
pretty :: forall ann. Const a b -> Doc ann
pretty = a -> Doc ann
forall ann. a -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (a -> Doc ann) -> (Const a b -> a) -> Const a b -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Const a b -> a
forall {k} a (b :: k). Const a b -> a
getConst
#if FUNCTOR_IDENTITY_IN_BASE
instance Pretty a => Pretty (Identity a) where
pretty :: forall ann. Identity a -> Doc ann
pretty = a -> Doc ann
forall ann. a -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (a -> Doc ann) -> (Identity a -> a) -> Identity a -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Identity a -> a
forall a. Identity a -> a
runIdentity
#endif
instance Pretty a => Pretty [a] where
pretty :: forall ann. [a] -> Doc ann
pretty = [a] -> Doc ann
forall ann. [a] -> Doc ann
forall a ann. Pretty a => [a] -> Doc ann
prettyList
instance Pretty a => Pretty (NonEmpty a) where
pretty :: forall ann. NonEmpty a -> Doc ann
pretty (a
x:|[a]
xs) = [a] -> Doc ann
forall ann. [a] -> Doc ann
forall a ann. Pretty a => [a] -> Doc ann
prettyList (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs)
instance Pretty () where
pretty :: forall ann. () -> Doc ann
pretty ()
_ = Doc ann
"()"
instance Pretty Bool where
pretty :: forall ann. Bool -> Doc ann
pretty Bool
True = Doc ann
"True"
pretty Bool
False = Doc ann
"False"
instance Pretty Char where
pretty :: forall ann. Char -> Doc ann
pretty Char
'\n' = Doc ann
forall ann. Doc ann
line
pretty Char
c = Char -> Doc ann
forall ann. Char -> Doc ann
Char Char
c
#ifdef MIN_VERSION_text
prettyList :: forall ann. String -> Doc ann
prettyList = Text -> Doc ann
forall ann. Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Text -> Doc ann) -> (String -> Text) -> String -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Text
forall a. a -> a
id :: Text -> Text) (Text -> Text) -> (String -> Text) -> String -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
forall a. IsString a => String -> a
fromString
#else
prettyList = vsep . map unsafeTextWithoutNewlines . T.splitOn "\n"
#endif
viaShow :: Show a => a -> Doc ann
viaShow :: forall a ann. Show a => a -> Doc ann
viaShow = Text -> Doc ann
forall ann. Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Text -> Doc ann) -> (a -> Text) -> a -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show
unsafeViaShow :: Show a => a -> Doc ann
unsafeViaShow :: forall a ann. Show a => a -> Doc ann
unsafeViaShow = Text -> Doc ann
forall ann. Text -> Doc ann
unsafeTextWithoutNewlines (Text -> Doc ann) -> (a -> Text) -> a -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show
instance Pretty Int where pretty :: forall ann. Int -> Doc ann
pretty = Int -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
instance Pretty Int8 where pretty :: forall ann. Int8 -> Doc ann
pretty = Int8 -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
instance Pretty Int16 where pretty :: forall ann. Int16 -> Doc ann
pretty = Int16 -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
instance Pretty Int32 where pretty :: forall ann. Int32 -> Doc ann
pretty = Int32 -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
instance Pretty Int64 where pretty :: forall ann. Int64 -> Doc ann
pretty = Int64 -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
instance Pretty Word where pretty :: forall ann. Word -> Doc ann
pretty = Word -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
instance Pretty Word8 where pretty :: forall ann. Word8 -> Doc ann
pretty = Word8 -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
instance Pretty Word16 where pretty :: forall ann. Word16 -> Doc ann
pretty = Word16 -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
instance Pretty Word32 where pretty :: forall ann. Word32 -> Doc ann
pretty = Word32 -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
instance Pretty Word64 where pretty :: forall ann. Word64 -> Doc ann
pretty = Word64 -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
instance Pretty Integer where pretty :: forall ann. Integer -> Doc ann
pretty = Integer -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
#if NATURAL_IN_BASE
instance Pretty Natural where pretty :: forall ann. Natural -> Doc ann
pretty = Natural -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
#endif
instance Pretty Float where pretty :: forall ann. Float -> Doc ann
pretty = Float -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
instance Pretty Double where pretty :: forall ann. Double -> Doc ann
pretty = Double -> Doc ann
forall a ann. Show a => a -> Doc ann
unsafeViaShow
instance (Pretty a1, Pretty a2) => Pretty (a1,a2) where
pretty :: forall ann. (a1, a2) -> Doc ann
pretty (a1
x1,a2
x2) = [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
tupled [a1 -> Doc ann
forall ann. a1 -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty a1
x1, a2 -> Doc ann
forall ann. a2 -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty a2
x2]
instance (Pretty a1, Pretty a2, Pretty a3) => Pretty (a1,a2,a3) where
pretty :: forall ann. (a1, a2, a3) -> Doc ann
pretty (a1
x1,a2
x2,a3
x3) = [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
tupled [a1 -> Doc ann
forall ann. a1 -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty a1
x1, a2 -> Doc ann
forall ann. a2 -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty a2
x2, a3 -> Doc ann
forall ann. a3 -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty a3
x3]
instance Pretty a => Pretty (Maybe a) where
pretty :: forall ann. Maybe a -> Doc ann
pretty = Doc ann -> (a -> Doc ann) -> Maybe a -> Doc ann
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Doc ann
forall a. Monoid a => a
mempty a -> Doc ann
forall ann. a -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty
prettyList :: forall ann. [Maybe a] -> Doc ann
prettyList = [a] -> Doc ann
forall ann. [a] -> Doc ann
forall a ann. Pretty a => [a] -> Doc ann
prettyList ([a] -> Doc ann) -> ([Maybe a] -> [a]) -> [Maybe a] -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Maybe a] -> [a]
forall a. [Maybe a] -> [a]
catMaybes
#ifdef MIN_VERSION_text
instance Pretty Text where pretty :: forall ann. Text -> Doc ann
pretty = [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep ([Doc ann] -> Doc ann) -> (Text -> [Doc ann]) -> Text -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Doc ann) -> [Text] -> [Doc ann]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Doc ann
forall ann. Text -> Doc ann
unsafeTextWithoutNewlines ([Text] -> [Doc ann]) -> (Text -> [Text]) -> Text -> [Doc ann]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> [Text]
Text -> Text -> [Text]
T.splitOn Text
"\n"
instance Pretty Lazy.Text where pretty :: forall ann. Text -> Doc ann
pretty = Text -> Doc ann
forall ann. Text -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty (Text -> Doc ann) -> (Text -> Text) -> Text -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
Lazy.toStrict
#endif
instance Pretty Void where pretty :: forall ann. Void -> Doc ann
pretty = Void -> Doc ann
forall a. Void -> a
absurd
unsafeTextWithoutNewlines :: Text -> Doc ann
unsafeTextWithoutNewlines :: forall ann. Text -> Doc ann
unsafeTextWithoutNewlines Text
text = case Text -> Maybe (Char, Text)
T.uncons Text
text of
Maybe (Char, Text)
Nothing -> Doc ann
forall ann. Doc ann
Empty
Just (Char
t,Text
ext)
| Text -> Bool
T.null Text
ext -> Char -> Doc ann
forall ann. Char -> Doc ann
Char Char
t
| Bool
otherwise -> Int -> Text -> Doc ann
forall ann. Int -> Text -> Doc ann
Text (Text -> Int
T.length Text
text) Text
text
emptyDoc :: Doc ann
emptyDoc :: forall ann. Doc ann
emptyDoc = Doc ann
forall ann. Doc ann
Empty
nest
:: Int
-> Doc ann
-> Doc ann
nest :: forall ann. Int -> Doc ann -> Doc ann
nest Int
0 Doc ann
x = Doc ann
x
nest Int
i Doc ann
x = Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
Nest Int
i Doc ann
x
line :: Doc ann
line :: forall ann. Doc ann
line = Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
FlatAlt Doc ann
forall ann. Doc ann
Line (Char -> Doc ann
forall ann. Char -> Doc ann
Char Char
' ')
line' :: Doc ann
line' :: forall ann. Doc ann
line' = Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
FlatAlt Doc ann
forall ann. Doc ann
Line Doc ann
forall a. Monoid a => a
mempty
softline :: Doc ann
softline :: forall ann. Doc ann
softline = Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Union (Char -> Doc ann
forall ann. Char -> Doc ann
Char Char
' ') Doc ann
forall ann. Doc ann
Line
softline' :: Doc ann
softline' :: forall ann. Doc ann
softline' = Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Union Doc ann
forall a. Monoid a => a
mempty Doc ann
forall ann. Doc ann
Line
hardline :: Doc ann
hardline :: forall ann. Doc ann
hardline = Doc ann
forall ann. Doc ann
Line
group :: Doc ann -> Doc ann
group :: forall ann. Doc ann -> Doc ann
group Doc ann
x = case Doc ann
x of
Union{} -> Doc ann
x
FlatAlt Doc ann
a Doc ann
b -> case Doc ann -> FlattenResult (Doc ann)
forall ann. Doc ann -> FlattenResult (Doc ann)
changesUponFlattening Doc ann
b of
Flattened Doc ann
b' -> Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Union Doc ann
b' Doc ann
a
FlattenResult (Doc ann)
AlreadyFlat -> Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Union Doc ann
b Doc ann
a
FlattenResult (Doc ann)
NeverFlat -> Doc ann
a
Doc ann
_ -> case Doc ann -> FlattenResult (Doc ann)
forall ann. Doc ann -> FlattenResult (Doc ann)
changesUponFlattening Doc ann
x of
Flattened Doc ann
x' -> Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Union Doc ann
x' Doc ann
x
FlattenResult (Doc ann)
AlreadyFlat -> Doc ann
x
FlattenResult (Doc ann)
NeverFlat -> Doc ann
x
data FlattenResult a
= Flattened a
| AlreadyFlat
| NeverFlat
instance Functor FlattenResult where
fmap :: forall a b. (a -> b) -> FlattenResult a -> FlattenResult b
fmap a -> b
f (Flattened a
a) = b -> FlattenResult b
forall a. a -> FlattenResult a
Flattened (a -> b
f a
a)
fmap a -> b
_ FlattenResult a
AlreadyFlat = FlattenResult b
forall a. FlattenResult a
AlreadyFlat
fmap a -> b
_ FlattenResult a
NeverFlat = FlattenResult b
forall a. FlattenResult a
NeverFlat
changesUponFlattening :: Doc ann -> FlattenResult (Doc ann)
changesUponFlattening :: forall ann. Doc ann -> FlattenResult (Doc ann)
changesUponFlattening = \Doc ann
doc -> case Doc ann
doc of
FlatAlt Doc ann
_ Doc ann
y -> Doc ann -> FlattenResult (Doc ann)
forall a. a -> FlattenResult a
Flattened (Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten Doc ann
y)
Doc ann
Line -> FlattenResult (Doc ann)
forall a. FlattenResult a
NeverFlat
Union Doc ann
x Doc ann
_ -> Doc ann -> FlattenResult (Doc ann)
forall a. a -> FlattenResult a
Flattened Doc ann
x
Nest Int
i Doc ann
x -> (Doc ann -> Doc ann)
-> FlattenResult (Doc ann) -> FlattenResult (Doc ann)
forall a b. (a -> b) -> FlattenResult a -> FlattenResult b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
Nest Int
i) (Doc ann -> FlattenResult (Doc ann)
forall ann. Doc ann -> FlattenResult (Doc ann)
changesUponFlattening Doc ann
x)
Annotated ann
ann Doc ann
x -> (Doc ann -> Doc ann)
-> FlattenResult (Doc ann) -> FlattenResult (Doc ann)
forall a b. (a -> b) -> FlattenResult a -> FlattenResult b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (ann -> Doc ann -> Doc ann
forall ann. ann -> Doc ann -> Doc ann
Annotated ann
ann) (Doc ann -> FlattenResult (Doc ann)
forall ann. Doc ann -> FlattenResult (Doc ann)
changesUponFlattening Doc ann
x)
Column Int -> Doc ann
f -> Doc ann -> FlattenResult (Doc ann)
forall a. a -> FlattenResult a
Flattened ((Int -> Doc ann) -> Doc ann
forall ann. (Int -> Doc ann) -> Doc ann
Column (Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten (Doc ann -> Doc ann) -> (Int -> Doc ann) -> Int -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc ann
f))
Nesting Int -> Doc ann
f -> Doc ann -> FlattenResult (Doc ann)
forall a. a -> FlattenResult a
Flattened ((Int -> Doc ann) -> Doc ann
forall ann. (Int -> Doc ann) -> Doc ann
Nesting (Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten (Doc ann -> Doc ann) -> (Int -> Doc ann) -> Int -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc ann
f))
WithPageWidth PageWidth -> Doc ann
f -> Doc ann -> FlattenResult (Doc ann)
forall a. a -> FlattenResult a
Flattened ((PageWidth -> Doc ann) -> Doc ann
forall ann. (PageWidth -> Doc ann) -> Doc ann
WithPageWidth (Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten (Doc ann -> Doc ann)
-> (PageWidth -> Doc ann) -> PageWidth -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PageWidth -> Doc ann
f))
Cat Doc ann
x Doc ann
y -> case (Doc ann -> FlattenResult (Doc ann)
forall ann. Doc ann -> FlattenResult (Doc ann)
changesUponFlattening Doc ann
x, Doc ann -> FlattenResult (Doc ann)
forall ann. Doc ann -> FlattenResult (Doc ann)
changesUponFlattening Doc ann
y) of
(FlattenResult (Doc ann)
NeverFlat , FlattenResult (Doc ann)
_ ) -> FlattenResult (Doc ann)
forall a. FlattenResult a
NeverFlat
(FlattenResult (Doc ann)
_ , FlattenResult (Doc ann)
NeverFlat ) -> FlattenResult (Doc ann)
forall a. FlattenResult a
NeverFlat
(Flattened Doc ann
x' , Flattened Doc ann
y') -> Doc ann -> FlattenResult (Doc ann)
forall a. a -> FlattenResult a
Flattened (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat Doc ann
x' Doc ann
y')
(Flattened Doc ann
x' , FlattenResult (Doc ann)
AlreadyFlat ) -> Doc ann -> FlattenResult (Doc ann)
forall a. a -> FlattenResult a
Flattened (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat Doc ann
x' Doc ann
y)
(FlattenResult (Doc ann)
AlreadyFlat , Flattened Doc ann
y') -> Doc ann -> FlattenResult (Doc ann)
forall a. a -> FlattenResult a
Flattened (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat Doc ann
x Doc ann
y')
(FlattenResult (Doc ann)
AlreadyFlat , FlattenResult (Doc ann)
AlreadyFlat ) -> FlattenResult (Doc ann)
forall a. FlattenResult a
AlreadyFlat
Doc ann
Empty -> FlattenResult (Doc ann)
forall a. FlattenResult a
AlreadyFlat
Char{} -> FlattenResult (Doc ann)
forall a. FlattenResult a
AlreadyFlat
Text{} -> FlattenResult (Doc ann)
forall a. FlattenResult a
AlreadyFlat
Doc ann
Fail -> FlattenResult (Doc ann)
forall a. FlattenResult a
NeverFlat
where
flatten :: Doc ann -> Doc ann
flatten :: forall ann. Doc ann -> Doc ann
flatten = \Doc ann
doc -> case Doc ann
doc of
FlatAlt Doc ann
_ Doc ann
y -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten Doc ann
y
Cat Doc ann
x Doc ann
y -> Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat (Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten Doc ann
x) (Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten Doc ann
y)
Nest Int
i Doc ann
x -> Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
Nest Int
i (Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten Doc ann
x)
Doc ann
Line -> Doc ann
forall ann. Doc ann
Fail
Union Doc ann
x Doc ann
_ -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten Doc ann
x
Column Int -> Doc ann
f -> (Int -> Doc ann) -> Doc ann
forall ann. (Int -> Doc ann) -> Doc ann
Column (Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten (Doc ann -> Doc ann) -> (Int -> Doc ann) -> Int -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc ann
f)
WithPageWidth PageWidth -> Doc ann
f -> (PageWidth -> Doc ann) -> Doc ann
forall ann. (PageWidth -> Doc ann) -> Doc ann
WithPageWidth (Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten (Doc ann -> Doc ann)
-> (PageWidth -> Doc ann) -> PageWidth -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PageWidth -> Doc ann
f)
Nesting Int -> Doc ann
f -> (Int -> Doc ann) -> Doc ann
forall ann. (Int -> Doc ann) -> Doc ann
Nesting (Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten (Doc ann -> Doc ann) -> (Int -> Doc ann) -> Int -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc ann
f)
Annotated ann
ann Doc ann
x -> ann -> Doc ann -> Doc ann
forall ann. ann -> Doc ann -> Doc ann
Annotated ann
ann (Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
flatten Doc ann
x)
x :: Doc ann
x@Doc ann
Fail -> Doc ann
x
x :: Doc ann
x@Doc ann
Empty -> Doc ann
x
x :: Doc ann
x@Char{} -> Doc ann
x
x :: Doc ann
x@Text{} -> Doc ann
x
flatAlt
:: Doc ann
-> Doc ann
-> Doc ann
flatAlt :: forall ann. Doc ann -> Doc ann -> Doc ann
flatAlt = Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
FlatAlt
align :: Doc ann -> Doc ann
align :: forall ann. Doc ann -> Doc ann
align Doc ann
d = (Int -> Doc ann) -> Doc ann
forall ann. (Int -> Doc ann) -> Doc ann
column (\Int
k -> (Int -> Doc ann) -> Doc ann
forall ann. (Int -> Doc ann) -> Doc ann
nesting (\Int
i -> Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
nest (Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i) Doc ann
d))
hang
:: Int
-> Doc ann
-> Doc ann
hang :: forall ann. Int -> Doc ann -> Doc ann
hang Int
i Doc ann
d = Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
align (Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
nest Int
i Doc ann
d)
indent
:: Int
-> Doc ann
-> Doc ann
indent :: forall ann. Int -> Doc ann -> Doc ann
indent Int
i Doc ann
d = Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
hang Int
i (Int -> Doc ann
forall ann. Int -> Doc ann
spaces Int
i Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
d)
encloseSep
:: Doc ann
-> Doc ann
-> Doc ann
-> [Doc ann]
-> Doc ann
encloseSep :: forall ann. Doc ann -> Doc ann -> Doc ann -> [Doc ann] -> Doc ann
encloseSep Doc ann
l Doc ann
r Doc ann
s [Doc ann]
ds = case [Doc ann]
ds of
[] -> Doc ann
l Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
r
[Doc ann
d] -> Doc ann
l Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
d Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
r
[Doc ann]
_ -> [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
cat ((Doc ann -> Doc ann -> Doc ann)
-> [Doc ann] -> [Doc ann] -> [Doc ann]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
(<>) (Doc ann
l Doc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
: Doc ann -> [Doc ann]
forall a. a -> [a]
repeat Doc ann
s) [Doc ann]
ds) Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
r
list :: [Doc ann] -> Doc ann
list :: forall ann. [Doc ann] -> Doc ann
list = Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
group (Doc ann -> Doc ann)
-> ([Doc ann] -> Doc ann) -> [Doc ann] -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc ann -> Doc ann -> Doc ann -> [Doc ann] -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann -> [Doc ann] -> Doc ann
encloseSep (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
flatAlt Doc ann
"[ " Doc ann
"[")
(Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
flatAlt Doc ann
" ]" Doc ann
"]")
Doc ann
", "
tupled :: [Doc ann] -> Doc ann
tupled :: forall ann. [Doc ann] -> Doc ann
tupled = Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
group (Doc ann -> Doc ann)
-> ([Doc ann] -> Doc ann) -> [Doc ann] -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Doc ann -> Doc ann -> Doc ann -> [Doc ann] -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann -> [Doc ann] -> Doc ann
encloseSep (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
flatAlt Doc ann
"( " Doc ann
"(")
(Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
flatAlt Doc ann
" )" Doc ann
")")
Doc ann
", "
(<+>) :: Doc ann -> Doc ann -> Doc ann
Doc ann
x <+> :: forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
y = Doc ann
x Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Char -> Doc ann
forall ann. Char -> Doc ann
Char Char
' ' Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
y
infixr 6 <+>
concatWith :: Foldable t => (Doc ann -> Doc ann -> Doc ann) -> t (Doc ann) -> Doc ann
concatWith :: forall (t :: * -> *) ann.
Foldable t =>
(Doc ann -> Doc ann -> Doc ann) -> t (Doc ann) -> Doc ann
concatWith Doc ann -> Doc ann -> Doc ann
f t (Doc ann)
ds
#if !(FOLDABLE_TRAVERSABLE_IN_PRELUDE)
| foldr (\_ _ -> False) True ds = mempty
#else
| t (Doc ann) -> Bool
forall a. t a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null t (Doc ann)
ds = Doc ann
forall a. Monoid a => a
mempty
#endif
| Bool
otherwise = (Doc ann -> Doc ann -> Doc ann) -> t (Doc ann) -> Doc ann
forall a. (a -> a -> a) -> t a -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 Doc ann -> Doc ann -> Doc ann
f t (Doc ann)
ds
{-# INLINE concatWith #-}
{-# SPECIALIZE concatWith :: (Doc ann -> Doc ann -> Doc ann) -> [Doc ann] -> Doc ann #-}
hsep :: [Doc ann] -> Doc ann
hsep :: forall ann. [Doc ann] -> Doc ann
hsep = (Doc ann -> Doc ann -> Doc ann) -> [Doc ann] -> Doc ann
forall (t :: * -> *) ann.
Foldable t =>
(Doc ann -> Doc ann -> Doc ann) -> t (Doc ann) -> Doc ann
concatWith Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
(<+>)
vsep :: [Doc ann] -> Doc ann
vsep :: forall ann. [Doc ann] -> Doc ann
vsep = (Doc ann -> Doc ann -> Doc ann) -> [Doc ann] -> Doc ann
forall (t :: * -> *) ann.
Foldable t =>
(Doc ann -> Doc ann -> Doc ann) -> t (Doc ann) -> Doc ann
concatWith (\Doc ann
x Doc ann
y -> Doc ann
x Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
forall ann. Doc ann
line Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
y)
fillSep :: [Doc ann] -> Doc ann
fillSep :: forall ann. [Doc ann] -> Doc ann
fillSep = (Doc ann -> Doc ann -> Doc ann) -> [Doc ann] -> Doc ann
forall (t :: * -> *) ann.
Foldable t =>
(Doc ann -> Doc ann -> Doc ann) -> t (Doc ann) -> Doc ann
concatWith (\Doc ann
x Doc ann
y -> Doc ann
x Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
forall ann. Doc ann
softline Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
y)
sep :: [Doc ann] -> Doc ann
sep :: forall ann. [Doc ann] -> Doc ann
sep = Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
group (Doc ann -> Doc ann)
-> ([Doc ann] -> Doc ann) -> [Doc ann] -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vsep
hcat :: [Doc ann] -> Doc ann
hcat :: forall ann. [Doc ann] -> Doc ann
hcat = (Doc ann -> Doc ann -> Doc ann) -> [Doc ann] -> Doc ann
forall (t :: * -> *) ann.
Foldable t =>
(Doc ann -> Doc ann -> Doc ann) -> t (Doc ann) -> Doc ann
concatWith Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
(<>)
vcat :: [Doc ann] -> Doc ann
vcat :: forall ann. [Doc ann] -> Doc ann
vcat = (Doc ann -> Doc ann -> Doc ann) -> [Doc ann] -> Doc ann
forall (t :: * -> *) ann.
Foldable t =>
(Doc ann -> Doc ann -> Doc ann) -> t (Doc ann) -> Doc ann
concatWith (\Doc ann
x Doc ann
y -> Doc ann
x Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
forall ann. Doc ann
line' Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
y)
fillCat :: [Doc ann] -> Doc ann
fillCat :: forall ann. [Doc ann] -> Doc ann
fillCat = (Doc ann -> Doc ann -> Doc ann) -> [Doc ann] -> Doc ann
forall (t :: * -> *) ann.
Foldable t =>
(Doc ann -> Doc ann -> Doc ann) -> t (Doc ann) -> Doc ann
concatWith (\Doc ann
x Doc ann
y -> Doc ann
x Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
forall ann. Doc ann
softline' Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
y)
cat :: [Doc ann] -> Doc ann
cat :: forall ann. [Doc ann] -> Doc ann
cat = Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
group (Doc ann -> Doc ann)
-> ([Doc ann] -> Doc ann) -> [Doc ann] -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Doc ann] -> Doc ann
forall ann. [Doc ann] -> Doc ann
vcat
punctuate
:: Doc ann
-> [Doc ann]
-> [Doc ann]
punctuate :: forall ann. Doc ann -> [Doc ann] -> [Doc ann]
punctuate Doc ann
p = [Doc ann] -> [Doc ann]
go
where
go :: [Doc ann] -> [Doc ann]
go [] = []
go [Doc ann
d] = [Doc ann
d]
go (Doc ann
d:[Doc ann]
ds) = (Doc ann
d Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
p) Doc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
: [Doc ann] -> [Doc ann]
go [Doc ann]
ds
column :: (Int -> Doc ann) -> Doc ann
column :: forall ann. (Int -> Doc ann) -> Doc ann
column = (Int -> Doc ann) -> Doc ann
forall ann. (Int -> Doc ann) -> Doc ann
Column
nesting :: (Int -> Doc ann) -> Doc ann
nesting :: forall ann. (Int -> Doc ann) -> Doc ann
nesting = (Int -> Doc ann) -> Doc ann
forall ann. (Int -> Doc ann) -> Doc ann
Nesting
width :: Doc ann -> (Int -> Doc ann) -> Doc ann
width :: forall ann. Doc ann -> (Int -> Doc ann) -> Doc ann
width Doc ann
doc Int -> Doc ann
f
= (Int -> Doc ann) -> Doc ann
forall ann. (Int -> Doc ann) -> Doc ann
column (\Int
colStart ->
Doc ann
doc Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> (Int -> Doc ann) -> Doc ann
forall ann. (Int -> Doc ann) -> Doc ann
column (\Int
colEnd ->
Int -> Doc ann
f (Int
colEnd Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
colStart)))
pageWidth :: (PageWidth -> Doc ann) -> Doc ann
pageWidth :: forall ann. (PageWidth -> Doc ann) -> Doc ann
pageWidth = (PageWidth -> Doc ann) -> Doc ann
forall ann. (PageWidth -> Doc ann) -> Doc ann
WithPageWidth
fill
:: Int
-> Doc ann
-> Doc ann
fill :: forall ann. Int -> Doc ann -> Doc ann
fill Int
n Doc ann
doc = Doc ann -> (Int -> Doc ann) -> Doc ann
forall ann. Doc ann -> (Int -> Doc ann) -> Doc ann
width Doc ann
doc (\Int
w -> Int -> Doc ann
forall ann. Int -> Doc ann
spaces (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
w))
fillBreak
:: Int
-> Doc ann
-> Doc ann
fillBreak :: forall ann. Int -> Doc ann -> Doc ann
fillBreak Int
f Doc ann
x = Doc ann -> (Int -> Doc ann) -> Doc ann
forall ann. Doc ann -> (Int -> Doc ann) -> Doc ann
width Doc ann
x (\Int
w ->
if Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
f
then Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
nest Int
f Doc ann
forall ann. Doc ann
line'
else Int -> Doc ann
forall ann. Int -> Doc ann
spaces (Int
f Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
w))
spaces :: Int -> Doc ann
spaces :: forall ann. Int -> Doc ann
spaces Int
n
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Doc ann
forall ann. Doc ann
Empty
| Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = Char -> Doc ann
forall ann. Char -> Doc ann
Char Char
' '
| Bool
otherwise = Int -> Text -> Doc ann
forall ann. Int -> Text -> Doc ann
Text Int
n (Int -> Text
textSpaces Int
n)
plural
:: (Num amount, Eq amount)
=> doc
-> doc
-> amount
-> doc
plural :: forall amount doc.
(Num amount, Eq amount) =>
doc -> doc -> amount -> doc
plural doc
one doc
multiple amount
n
| amount
n amount -> amount -> Bool
forall a. Eq a => a -> a -> Bool
== amount
1 = doc
one
| Bool
otherwise = doc
multiple
enclose
:: Doc ann
-> Doc ann
-> Doc ann
-> Doc ann
enclose :: forall ann. Doc ann -> Doc ann -> Doc ann -> Doc ann
enclose Doc ann
l Doc ann
r Doc ann
x = Doc ann
l Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
x Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
r
surround
:: Doc ann
-> Doc ann
-> Doc ann
-> Doc ann
surround :: forall ann. Doc ann -> Doc ann -> Doc ann -> Doc ann
surround Doc ann
x Doc ann
l Doc ann
r = Doc ann
l Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
x Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
r
annotate :: ann -> Doc ann -> Doc ann
annotate :: forall ann. ann -> Doc ann -> Doc ann
annotate = ann -> Doc ann -> Doc ann
forall ann. ann -> Doc ann -> Doc ann
Annotated
unAnnotate :: Doc ann -> Doc xxx
unAnnotate :: forall ann xxx. Doc ann -> Doc xxx
unAnnotate = (ann -> [xxx]) -> Doc ann -> Doc xxx
forall ann ann'. (ann -> [ann']) -> Doc ann -> Doc ann'
alterAnnotations ([xxx] -> ann -> [xxx]
forall a b. a -> b -> a
const [])
reAnnotate :: (ann -> ann') -> Doc ann -> Doc ann'
reAnnotate :: forall a b. (a -> b) -> Doc a -> Doc b
reAnnotate ann -> ann'
re = (ann -> [ann']) -> Doc ann -> Doc ann'
forall ann ann'. (ann -> [ann']) -> Doc ann -> Doc ann'
alterAnnotations (ann' -> [ann']
forall a. a -> [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ann' -> [ann']) -> (ann -> ann') -> ann -> [ann']
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ann -> ann'
re)
alterAnnotations :: (ann -> [ann']) -> Doc ann -> Doc ann'
alterAnnotations :: forall ann ann'. (ann -> [ann']) -> Doc ann -> Doc ann'
alterAnnotations ann -> [ann']
re = Doc ann -> Doc ann'
go
where
go :: Doc ann -> Doc ann'
go = \Doc ann
doc -> case Doc ann
doc of
Doc ann
Fail -> Doc ann'
forall ann. Doc ann
Fail
Doc ann
Empty -> Doc ann'
forall ann. Doc ann
Empty
Char Char
c -> Char -> Doc ann'
forall ann. Char -> Doc ann
Char Char
c
Text Int
l Text
t -> Int -> Text -> Doc ann'
forall ann. Int -> Text -> Doc ann
Text Int
l Text
t
Doc ann
Line -> Doc ann'
forall ann. Doc ann
Line
FlatAlt Doc ann
x Doc ann
y -> Doc ann' -> Doc ann' -> Doc ann'
forall ann. Doc ann -> Doc ann -> Doc ann
FlatAlt (Doc ann -> Doc ann'
go Doc ann
x) (Doc ann -> Doc ann'
go Doc ann
y)
Cat Doc ann
x Doc ann
y -> Doc ann' -> Doc ann' -> Doc ann'
forall ann. Doc ann -> Doc ann -> Doc ann
Cat (Doc ann -> Doc ann'
go Doc ann
x) (Doc ann -> Doc ann'
go Doc ann
y)
Nest Int
i Doc ann
x -> Int -> Doc ann' -> Doc ann'
forall ann. Int -> Doc ann -> Doc ann
Nest Int
i (Doc ann -> Doc ann'
go Doc ann
x)
Union Doc ann
x Doc ann
y -> Doc ann' -> Doc ann' -> Doc ann'
forall ann. Doc ann -> Doc ann -> Doc ann
Union (Doc ann -> Doc ann'
go Doc ann
x) (Doc ann -> Doc ann'
go Doc ann
y)
Column Int -> Doc ann
f -> (Int -> Doc ann') -> Doc ann'
forall ann. (Int -> Doc ann) -> Doc ann
Column (Doc ann -> Doc ann'
go (Doc ann -> Doc ann') -> (Int -> Doc ann) -> Int -> Doc ann'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc ann
f)
WithPageWidth PageWidth -> Doc ann
f -> (PageWidth -> Doc ann') -> Doc ann'
forall ann. (PageWidth -> Doc ann) -> Doc ann
WithPageWidth (Doc ann -> Doc ann'
go (Doc ann -> Doc ann')
-> (PageWidth -> Doc ann) -> PageWidth -> Doc ann'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PageWidth -> Doc ann
f)
Nesting Int -> Doc ann
f -> (Int -> Doc ann') -> Doc ann'
forall ann. (Int -> Doc ann) -> Doc ann
Nesting (Doc ann -> Doc ann'
go (Doc ann -> Doc ann') -> (Int -> Doc ann) -> Int -> Doc ann'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc ann
f)
Annotated ann
ann Doc ann
x -> (ann' -> Doc ann' -> Doc ann') -> Doc ann' -> [ann'] -> Doc ann'
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ann' -> Doc ann' -> Doc ann'
forall ann. ann -> Doc ann -> Doc ann
Annotated (Doc ann -> Doc ann'
go Doc ann
x) (ann -> [ann']
re ann
ann)
unAnnotateS :: SimpleDocStream ann -> SimpleDocStream xxx
unAnnotateS :: forall ann xxx. SimpleDocStream ann -> SimpleDocStream xxx
unAnnotateS = SimpleDocStream ann -> SimpleDocStream xxx
forall ann xxx. SimpleDocStream ann -> SimpleDocStream xxx
go
where
go :: SimpleDocStream ann -> SimpleDocStream ann
go = \SimpleDocStream ann
doc -> case SimpleDocStream ann
doc of
SimpleDocStream ann
SFail -> SimpleDocStream ann
forall ann. SimpleDocStream ann
SFail
SimpleDocStream ann
SEmpty -> SimpleDocStream ann
forall ann. SimpleDocStream ann
SEmpty
SChar Char
c SimpleDocStream ann
rest -> Char -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. Char -> SimpleDocStream ann -> SimpleDocStream ann
SChar Char
c (SimpleDocStream ann -> SimpleDocStream ann
go SimpleDocStream ann
rest)
SText Int
l Text
t SimpleDocStream ann
rest -> Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
SText Int
l Text
t (SimpleDocStream ann -> SimpleDocStream ann
go SimpleDocStream ann
rest)
SLine Int
l SimpleDocStream ann
rest -> Int -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. Int -> SimpleDocStream ann -> SimpleDocStream ann
SLine Int
l (SimpleDocStream ann -> SimpleDocStream ann
go SimpleDocStream ann
rest)
SAnnPop SimpleDocStream ann
rest -> SimpleDocStream ann -> SimpleDocStream ann
go SimpleDocStream ann
rest
SAnnPush ann
_ann SimpleDocStream ann
rest -> SimpleDocStream ann -> SimpleDocStream ann
go SimpleDocStream ann
rest
reAnnotateS :: (ann -> ann') -> SimpleDocStream ann -> SimpleDocStream ann'
reAnnotateS :: forall ann ann'.
(ann -> ann') -> SimpleDocStream ann -> SimpleDocStream ann'
reAnnotateS ann -> ann'
re = SimpleDocStream ann -> SimpleDocStream ann'
go
where
go :: SimpleDocStream ann -> SimpleDocStream ann'
go = \SimpleDocStream ann
doc -> case SimpleDocStream ann
doc of
SimpleDocStream ann
SFail -> SimpleDocStream ann'
forall ann. SimpleDocStream ann
SFail
SimpleDocStream ann
SEmpty -> SimpleDocStream ann'
forall ann. SimpleDocStream ann
SEmpty
SChar Char
c SimpleDocStream ann
rest -> Char -> SimpleDocStream ann' -> SimpleDocStream ann'
forall ann. Char -> SimpleDocStream ann -> SimpleDocStream ann
SChar Char
c (SimpleDocStream ann -> SimpleDocStream ann'
go SimpleDocStream ann
rest)
SText Int
l Text
t SimpleDocStream ann
rest -> Int -> Text -> SimpleDocStream ann' -> SimpleDocStream ann'
forall ann.
Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
SText Int
l Text
t (SimpleDocStream ann -> SimpleDocStream ann'
go SimpleDocStream ann
rest)
SLine Int
l SimpleDocStream ann
rest -> Int -> SimpleDocStream ann' -> SimpleDocStream ann'
forall ann. Int -> SimpleDocStream ann -> SimpleDocStream ann
SLine Int
l (SimpleDocStream ann -> SimpleDocStream ann'
go SimpleDocStream ann
rest)
SAnnPop SimpleDocStream ann
rest -> SimpleDocStream ann' -> SimpleDocStream ann'
forall ann. SimpleDocStream ann -> SimpleDocStream ann
SAnnPop (SimpleDocStream ann -> SimpleDocStream ann'
go SimpleDocStream ann
rest)
SAnnPush ann
ann SimpleDocStream ann
rest -> ann' -> SimpleDocStream ann' -> SimpleDocStream ann'
forall ann. ann -> SimpleDocStream ann -> SimpleDocStream ann
SAnnPush (ann -> ann'
re ann
ann) (SimpleDocStream ann -> SimpleDocStream ann'
go SimpleDocStream ann
rest)
data AnnotationRemoval = Remove | DontRemove
deriving Typeable
alterAnnotationsS :: (ann -> Maybe ann') -> SimpleDocStream ann -> SimpleDocStream ann'
alterAnnotationsS :: forall ann ann'.
(ann -> Maybe ann') -> SimpleDocStream ann -> SimpleDocStream ann'
alterAnnotationsS ann -> Maybe ann'
re = [AnnotationRemoval] -> SimpleDocStream ann -> SimpleDocStream ann'
go []
where
go :: [AnnotationRemoval] -> SimpleDocStream ann -> SimpleDocStream ann'
go [AnnotationRemoval]
stack = \SimpleDocStream ann
sds -> case SimpleDocStream ann
sds of
SimpleDocStream ann
SFail -> SimpleDocStream ann'
forall ann. SimpleDocStream ann
SFail
SimpleDocStream ann
SEmpty -> SimpleDocStream ann'
forall ann. SimpleDocStream ann
SEmpty
SChar Char
c SimpleDocStream ann
rest -> Char -> SimpleDocStream ann' -> SimpleDocStream ann'
forall ann. Char -> SimpleDocStream ann -> SimpleDocStream ann
SChar Char
c ([AnnotationRemoval] -> SimpleDocStream ann -> SimpleDocStream ann'
go [AnnotationRemoval]
stack SimpleDocStream ann
rest)
SText Int
l Text
t SimpleDocStream ann
rest -> Int -> Text -> SimpleDocStream ann' -> SimpleDocStream ann'
forall ann.
Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
SText Int
l Text
t ([AnnotationRemoval] -> SimpleDocStream ann -> SimpleDocStream ann'
go [AnnotationRemoval]
stack SimpleDocStream ann
rest)
SLine Int
l SimpleDocStream ann
rest -> Int -> SimpleDocStream ann' -> SimpleDocStream ann'
forall ann. Int -> SimpleDocStream ann -> SimpleDocStream ann
SLine Int
l ([AnnotationRemoval] -> SimpleDocStream ann -> SimpleDocStream ann'
go [AnnotationRemoval]
stack SimpleDocStream ann
rest)
SAnnPush ann
ann SimpleDocStream ann
rest -> case ann -> Maybe ann'
re ann
ann of
Maybe ann'
Nothing -> [AnnotationRemoval] -> SimpleDocStream ann -> SimpleDocStream ann'
go (AnnotationRemoval
RemoveAnnotationRemoval -> [AnnotationRemoval] -> [AnnotationRemoval]
forall a. a -> [a] -> [a]
:[AnnotationRemoval]
stack) SimpleDocStream ann
rest
Just ann'
ann' -> ann' -> SimpleDocStream ann' -> SimpleDocStream ann'
forall ann. ann -> SimpleDocStream ann -> SimpleDocStream ann
SAnnPush ann'
ann' ([AnnotationRemoval] -> SimpleDocStream ann -> SimpleDocStream ann'
go (AnnotationRemoval
DontRemoveAnnotationRemoval -> [AnnotationRemoval] -> [AnnotationRemoval]
forall a. a -> [a] -> [a]
:[AnnotationRemoval]
stack) SimpleDocStream ann
rest)
SAnnPop SimpleDocStream ann
rest -> case [AnnotationRemoval]
stack of
[] -> SimpleDocStream ann'
forall void. void
panicPeekedEmpty
AnnotationRemoval
DontRemove:[AnnotationRemoval]
stack' -> SimpleDocStream ann' -> SimpleDocStream ann'
forall ann. SimpleDocStream ann -> SimpleDocStream ann
SAnnPop ([AnnotationRemoval] -> SimpleDocStream ann -> SimpleDocStream ann'
go [AnnotationRemoval]
stack' SimpleDocStream ann
rest)
AnnotationRemoval
Remove:[AnnotationRemoval]
stack' -> [AnnotationRemoval] -> SimpleDocStream ann -> SimpleDocStream ann'
go [AnnotationRemoval]
stack' SimpleDocStream ann
rest
data FusionDepth =
Shallow
| Deep
deriving (FusionDepth -> FusionDepth -> Bool
(FusionDepth -> FusionDepth -> Bool)
-> (FusionDepth -> FusionDepth -> Bool) -> Eq FusionDepth
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FusionDepth -> FusionDepth -> Bool
== :: FusionDepth -> FusionDepth -> Bool
$c/= :: FusionDepth -> FusionDepth -> Bool
/= :: FusionDepth -> FusionDepth -> Bool
Eq, Eq FusionDepth
Eq FusionDepth =>
(FusionDepth -> FusionDepth -> Ordering)
-> (FusionDepth -> FusionDepth -> Bool)
-> (FusionDepth -> FusionDepth -> Bool)
-> (FusionDepth -> FusionDepth -> Bool)
-> (FusionDepth -> FusionDepth -> Bool)
-> (FusionDepth -> FusionDepth -> FusionDepth)
-> (FusionDepth -> FusionDepth -> FusionDepth)
-> Ord FusionDepth
FusionDepth -> FusionDepth -> Bool
FusionDepth -> FusionDepth -> Ordering
FusionDepth -> FusionDepth -> FusionDepth
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: FusionDepth -> FusionDepth -> Ordering
compare :: FusionDepth -> FusionDepth -> Ordering
$c< :: FusionDepth -> FusionDepth -> Bool
< :: FusionDepth -> FusionDepth -> Bool
$c<= :: FusionDepth -> FusionDepth -> Bool
<= :: FusionDepth -> FusionDepth -> Bool
$c> :: FusionDepth -> FusionDepth -> Bool
> :: FusionDepth -> FusionDepth -> Bool
$c>= :: FusionDepth -> FusionDepth -> Bool
>= :: FusionDepth -> FusionDepth -> Bool
$cmax :: FusionDepth -> FusionDepth -> FusionDepth
max :: FusionDepth -> FusionDepth -> FusionDepth
$cmin :: FusionDepth -> FusionDepth -> FusionDepth
min :: FusionDepth -> FusionDepth -> FusionDepth
Ord, Int -> FusionDepth -> ShowS
[FusionDepth] -> ShowS
FusionDepth -> String
(Int -> FusionDepth -> ShowS)
-> (FusionDepth -> String)
-> ([FusionDepth] -> ShowS)
-> Show FusionDepth
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FusionDepth -> ShowS
showsPrec :: Int -> FusionDepth -> ShowS
$cshow :: FusionDepth -> String
show :: FusionDepth -> String
$cshowList :: [FusionDepth] -> ShowS
showList :: [FusionDepth] -> ShowS
Show, Typeable)
fuse :: FusionDepth -> Doc ann -> Doc ann
fuse :: forall ann. FusionDepth -> Doc ann -> Doc ann
fuse FusionDepth
depth = Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann
go
where
go :: Doc ann -> Doc ann
go = \Doc ann
doc -> case Doc ann
doc of
Cat Doc ann
Empty Doc ann
x -> Doc ann -> Doc ann
go Doc ann
x
Cat Doc ann
x Doc ann
Empty -> Doc ann -> Doc ann
go Doc ann
x
Cat (Char Char
c1) (Char Char
c2) -> Int -> Text -> Doc ann
forall ann. Int -> Text -> Doc ann
Text Int
2 (Char -> Text
T.singleton Char
c1 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Char -> Text
T.singleton Char
c2)
Cat (Text Int
lt Text
t) (Char Char
c) -> Int -> Text -> Doc ann
forall ann. Int -> Text -> Doc ann
Text (Int
ltInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) (Text -> Char -> Text
T.snoc Text
t Char
c)
Cat (Char Char
c) (Text Int
lt Text
t) -> Int -> Text -> Doc ann
forall ann. Int -> Text -> Doc ann
Text (Int
1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
lt) (Char -> Text -> Text
T.cons Char
c Text
t)
Cat (Text Int
l1 Text
t1) (Text Int
l2 Text
t2) -> Int -> Text -> Doc ann
forall ann. Int -> Text -> Doc ann
Text (Int
l1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
l2) (Text
t1 Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
t2)
Cat x :: Doc ann
x@Char{} (Cat y :: Doc ann
y@Char{} Doc ann
z) -> Doc ann -> Doc ann
go (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat (Doc ann -> Doc ann
go (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat Doc ann
x Doc ann
y)) Doc ann
z)
Cat x :: Doc ann
x@Text{} (Cat y :: Doc ann
y@Char{} Doc ann
z) -> Doc ann -> Doc ann
go (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat (Doc ann -> Doc ann
go (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat Doc ann
x Doc ann
y)) Doc ann
z)
Cat x :: Doc ann
x@Char{} (Cat y :: Doc ann
y@Text{} Doc ann
z) -> Doc ann -> Doc ann
go (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat (Doc ann -> Doc ann
go (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat Doc ann
x Doc ann
y)) Doc ann
z)
Cat x :: Doc ann
x@Text{} (Cat y :: Doc ann
y@Text{} Doc ann
z) -> Doc ann -> Doc ann
go (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat (Doc ann -> Doc ann
go (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat Doc ann
x Doc ann
y)) Doc ann
z)
Cat (Cat Doc ann
x y :: Doc ann
y@Char{}) Doc ann
z -> Doc ann -> Doc ann
go (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat Doc ann
x (Doc ann -> Doc ann
go (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat Doc ann
y Doc ann
z)))
Cat (Cat Doc ann
x y :: Doc ann
y@Text{}) Doc ann
z -> Doc ann -> Doc ann
go (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat Doc ann
x (Doc ann -> Doc ann
go (Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat Doc ann
y Doc ann
z)))
Cat Doc ann
x Doc ann
y -> Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Cat (Doc ann -> Doc ann
go Doc ann
x) (Doc ann -> Doc ann
go Doc ann
y)
Nest Int
i (Nest Int
j Doc ann
x) -> let !fused :: Doc ann
fused = Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
Nest (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
j) Doc ann
x
in Doc ann -> Doc ann
go Doc ann
fused
Nest Int
_ x :: Doc ann
x@Empty{} -> Doc ann
x
Nest Int
_ x :: Doc ann
x@Text{} -> Doc ann
x
Nest Int
_ x :: Doc ann
x@Char{} -> Doc ann
x
Nest Int
0 Doc ann
x -> Doc ann -> Doc ann
go Doc ann
x
Nest Int
i Doc ann
x -> Int -> Doc ann -> Doc ann
forall ann. Int -> Doc ann -> Doc ann
Nest Int
i (Doc ann -> Doc ann
go Doc ann
x)
Annotated ann
ann Doc ann
x -> ann -> Doc ann -> Doc ann
forall ann. ann -> Doc ann -> Doc ann
Annotated ann
ann (Doc ann -> Doc ann
go Doc ann
x)
FlatAlt Doc ann
x1 Doc ann
x2 -> Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
FlatAlt (Doc ann -> Doc ann
go Doc ann
x1) (Doc ann -> Doc ann
go Doc ann
x2)
Union Doc ann
x1 Doc ann
x2 -> Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
Union (Doc ann -> Doc ann
go Doc ann
x1) (Doc ann -> Doc ann
go Doc ann
x2)
Doc ann
other | FusionDepth
depth FusionDepth -> FusionDepth -> Bool
forall a. Eq a => a -> a -> Bool
== FusionDepth
Shallow -> Doc ann
other
Column Int -> Doc ann
f -> (Int -> Doc ann) -> Doc ann
forall ann. (Int -> Doc ann) -> Doc ann
Column (Doc ann -> Doc ann
go (Doc ann -> Doc ann) -> (Int -> Doc ann) -> Int -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc ann
f)
WithPageWidth PageWidth -> Doc ann
f -> (PageWidth -> Doc ann) -> Doc ann
forall ann. (PageWidth -> Doc ann) -> Doc ann
WithPageWidth (Doc ann -> Doc ann
go (Doc ann -> Doc ann)
-> (PageWidth -> Doc ann) -> PageWidth -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PageWidth -> Doc ann
f)
Nesting Int -> Doc ann
f -> (Int -> Doc ann) -> Doc ann
forall ann. (Int -> Doc ann) -> Doc ann
Nesting (Doc ann -> Doc ann
go (Doc ann -> Doc ann) -> (Int -> Doc ann) -> Int -> Doc ann
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Doc ann
f)
Doc ann
other -> Doc ann
other
data SimpleDocStream ann =
SFail
| SEmpty
| SChar !Char (SimpleDocStream ann)
| SText !Int !Text (SimpleDocStream ann)
| SLine !Int (SimpleDocStream ann)
| SAnnPush ann (SimpleDocStream ann)
| SAnnPop (SimpleDocStream ann)
deriving (SimpleDocStream ann -> SimpleDocStream ann -> Bool
(SimpleDocStream ann -> SimpleDocStream ann -> Bool)
-> (SimpleDocStream ann -> SimpleDocStream ann -> Bool)
-> Eq (SimpleDocStream ann)
forall ann.
Eq ann =>
SimpleDocStream ann -> SimpleDocStream ann -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall ann.
Eq ann =>
SimpleDocStream ann -> SimpleDocStream ann -> Bool
== :: SimpleDocStream ann -> SimpleDocStream ann -> Bool
$c/= :: forall ann.
Eq ann =>
SimpleDocStream ann -> SimpleDocStream ann -> Bool
/= :: SimpleDocStream ann -> SimpleDocStream ann -> Bool
Eq, Eq (SimpleDocStream ann)
Eq (SimpleDocStream ann) =>
(SimpleDocStream ann -> SimpleDocStream ann -> Ordering)
-> (SimpleDocStream ann -> SimpleDocStream ann -> Bool)
-> (SimpleDocStream ann -> SimpleDocStream ann -> Bool)
-> (SimpleDocStream ann -> SimpleDocStream ann -> Bool)
-> (SimpleDocStream ann -> SimpleDocStream ann -> Bool)
-> (SimpleDocStream ann
-> SimpleDocStream ann -> SimpleDocStream ann)
-> (SimpleDocStream ann
-> SimpleDocStream ann -> SimpleDocStream ann)
-> Ord (SimpleDocStream ann)
SimpleDocStream ann -> SimpleDocStream ann -> Bool
SimpleDocStream ann -> SimpleDocStream ann -> Ordering
SimpleDocStream ann -> SimpleDocStream ann -> SimpleDocStream ann
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall ann. Ord ann => Eq (SimpleDocStream ann)
forall ann.
Ord ann =>
SimpleDocStream ann -> SimpleDocStream ann -> Bool
forall ann.
Ord ann =>
SimpleDocStream ann -> SimpleDocStream ann -> Ordering
forall ann.
Ord ann =>
SimpleDocStream ann -> SimpleDocStream ann -> SimpleDocStream ann
$ccompare :: forall ann.
Ord ann =>
SimpleDocStream ann -> SimpleDocStream ann -> Ordering
compare :: SimpleDocStream ann -> SimpleDocStream ann -> Ordering
$c< :: forall ann.
Ord ann =>
SimpleDocStream ann -> SimpleDocStream ann -> Bool
< :: SimpleDocStream ann -> SimpleDocStream ann -> Bool
$c<= :: forall ann.
Ord ann =>
SimpleDocStream ann -> SimpleDocStream ann -> Bool
<= :: SimpleDocStream ann -> SimpleDocStream ann -> Bool
$c> :: forall ann.
Ord ann =>
SimpleDocStream ann -> SimpleDocStream ann -> Bool
> :: SimpleDocStream ann -> SimpleDocStream ann -> Bool
$c>= :: forall ann.
Ord ann =>
SimpleDocStream ann -> SimpleDocStream ann -> Bool
>= :: SimpleDocStream ann -> SimpleDocStream ann -> Bool
$cmax :: forall ann.
Ord ann =>
SimpleDocStream ann -> SimpleDocStream ann -> SimpleDocStream ann
max :: SimpleDocStream ann -> SimpleDocStream ann -> SimpleDocStream ann
$cmin :: forall ann.
Ord ann =>
SimpleDocStream ann -> SimpleDocStream ann -> SimpleDocStream ann
min :: SimpleDocStream ann -> SimpleDocStream ann -> SimpleDocStream ann
Ord, Int -> SimpleDocStream ann -> ShowS
[SimpleDocStream ann] -> ShowS
SimpleDocStream ann -> String
(Int -> SimpleDocStream ann -> ShowS)
-> (SimpleDocStream ann -> String)
-> ([SimpleDocStream ann] -> ShowS)
-> Show (SimpleDocStream ann)
forall ann. Show ann => Int -> SimpleDocStream ann -> ShowS
forall ann. Show ann => [SimpleDocStream ann] -> ShowS
forall ann. Show ann => SimpleDocStream ann -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall ann. Show ann => Int -> SimpleDocStream ann -> ShowS
showsPrec :: Int -> SimpleDocStream ann -> ShowS
$cshow :: forall ann. Show ann => SimpleDocStream ann -> String
show :: SimpleDocStream ann -> String
$cshowList :: forall ann. Show ann => [SimpleDocStream ann] -> ShowS
showList :: [SimpleDocStream ann] -> ShowS
Show, (forall x. SimpleDocStream ann -> Rep (SimpleDocStream ann) x)
-> (forall x. Rep (SimpleDocStream ann) x -> SimpleDocStream ann)
-> Generic (SimpleDocStream ann)
forall x. Rep (SimpleDocStream ann) x -> SimpleDocStream ann
forall x. SimpleDocStream ann -> Rep (SimpleDocStream ann) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall ann x. Rep (SimpleDocStream ann) x -> SimpleDocStream ann
forall ann x. SimpleDocStream ann -> Rep (SimpleDocStream ann) x
$cfrom :: forall ann x. SimpleDocStream ann -> Rep (SimpleDocStream ann) x
from :: forall x. SimpleDocStream ann -> Rep (SimpleDocStream ann) x
$cto :: forall ann x. Rep (SimpleDocStream ann) x -> SimpleDocStream ann
to :: forall x. Rep (SimpleDocStream ann) x -> SimpleDocStream ann
Generic, Typeable)
removeTrailingWhitespace :: SimpleDocStream ann -> SimpleDocStream ann
removeTrailingWhitespace :: forall ann. SimpleDocStream ann -> SimpleDocStream ann
removeTrailingWhitespace = WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go ([Int] -> Int -> WhitespaceStrippingState
RecordedWhitespace [] Int
0)
where
commitWhitespace
:: [Int]
-> Int
-> SimpleDocStream ann
-> SimpleDocStream ann
commitWhitespace :: forall ann.
[Int] -> Int -> SimpleDocStream ann -> SimpleDocStream ann
commitWhitespace [Int]
is !Int
n SimpleDocStream ann
sds = case [Int]
is of
[] -> case Int
n of
Int
0 -> SimpleDocStream ann
sds
Int
1 -> Char -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. Char -> SimpleDocStream ann -> SimpleDocStream ann
SChar Char
' ' SimpleDocStream ann
sds
Int
_ -> Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
SText Int
n (Int -> Text
textSpaces Int
n) SimpleDocStream ann
sds
(Int
i:[Int]
is') -> let !end :: SimpleDocStream ann
end = Int -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. Int -> SimpleDocStream ann -> SimpleDocStream ann
SLine (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
n) SimpleDocStream ann
sds
in [Int] -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. [Int] -> SimpleDocStream ann -> SimpleDocStream ann
prependEmptyLines [Int]
is' SimpleDocStream ann
end
prependEmptyLines :: [Int] -> SimpleDocStream ann -> SimpleDocStream ann
prependEmptyLines :: forall ann. [Int] -> SimpleDocStream ann -> SimpleDocStream ann
prependEmptyLines [Int]
is SimpleDocStream ann
sds0 = (Int -> SimpleDocStream ann -> SimpleDocStream ann)
-> SimpleDocStream ann -> [Int] -> SimpleDocStream ann
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Int
_ SimpleDocStream ann
sds -> Int -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. Int -> SimpleDocStream ann -> SimpleDocStream ann
SLine Int
0 SimpleDocStream ann
sds) SimpleDocStream ann
sds0 [Int]
is
go :: WhitespaceStrippingState -> SimpleDocStream ann -> SimpleDocStream ann
go :: forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go annLevel :: WhitespaceStrippingState
annLevel@(AnnotationLevel Int
annLvl) = \SimpleDocStream ann
sds -> case SimpleDocStream ann
sds of
SimpleDocStream ann
SFail -> SimpleDocStream ann
forall ann. SimpleDocStream ann
SFail
SimpleDocStream ann
SEmpty -> SimpleDocStream ann
forall ann. SimpleDocStream ann
SEmpty
SChar Char
c SimpleDocStream ann
rest -> Char -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. Char -> SimpleDocStream ann -> SimpleDocStream ann
SChar Char
c (WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go WhitespaceStrippingState
annLevel SimpleDocStream ann
rest)
SText Int
l Text
text SimpleDocStream ann
rest -> Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
SText Int
l Text
text (WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go WhitespaceStrippingState
annLevel SimpleDocStream ann
rest)
SLine Int
i SimpleDocStream ann
rest -> Int -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. Int -> SimpleDocStream ann -> SimpleDocStream ann
SLine Int
i (WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go WhitespaceStrippingState
annLevel SimpleDocStream ann
rest)
SAnnPush ann
ann SimpleDocStream ann
rest -> let !annLvl' :: Int
annLvl' = Int
annLvlInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1
in ann -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. ann -> SimpleDocStream ann -> SimpleDocStream ann
SAnnPush ann
ann (WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go (Int -> WhitespaceStrippingState
AnnotationLevel Int
annLvl') SimpleDocStream ann
rest)
SAnnPop SimpleDocStream ann
rest
| Int
annLvl Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 -> let !annLvl' :: Int
annLvl' = Int
annLvlInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1
in SimpleDocStream ann -> SimpleDocStream ann
forall ann. SimpleDocStream ann -> SimpleDocStream ann
SAnnPop (WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go (Int -> WhitespaceStrippingState
AnnotationLevel Int
annLvl') SimpleDocStream ann
rest)
| Bool
otherwise -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. SimpleDocStream ann -> SimpleDocStream ann
SAnnPop (WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go ([Int] -> Int -> WhitespaceStrippingState
RecordedWhitespace [] Int
0) SimpleDocStream ann
rest)
go (RecordedWhitespace [Int]
withheldLines Int
withheldSpaces) = \SimpleDocStream ann
sds -> case SimpleDocStream ann
sds of
SimpleDocStream ann
SFail -> SimpleDocStream ann
forall ann. SimpleDocStream ann
SFail
SimpleDocStream ann
SEmpty -> [Int] -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. [Int] -> SimpleDocStream ann -> SimpleDocStream ann
prependEmptyLines [Int]
withheldLines SimpleDocStream ann
forall ann. SimpleDocStream ann
SEmpty
SChar Char
c SimpleDocStream ann
rest
| Char
c Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ' -> WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go ([Int] -> Int -> WhitespaceStrippingState
RecordedWhitespace [Int]
withheldLines (Int
withheldSpacesInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)) SimpleDocStream ann
rest
| Bool
otherwise -> [Int] -> Int -> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
[Int] -> Int -> SimpleDocStream ann -> SimpleDocStream ann
commitWhitespace
[Int]
withheldLines
Int
withheldSpaces
(Char -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. Char -> SimpleDocStream ann -> SimpleDocStream ann
SChar Char
c (WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go ([Int] -> Int -> WhitespaceStrippingState
RecordedWhitespace [] Int
0) SimpleDocStream ann
rest))
SText Int
textLength Text
text SimpleDocStream ann
rest ->
let stripped :: Text
stripped = (Char -> Bool) -> Text -> Text
T.dropWhileEnd (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
' ') Text
text
strippedLength :: Int
strippedLength = Text -> Int
T.length Text
stripped
trailingLength :: Int
trailingLength = Int
textLength Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
strippedLength
isOnlySpace :: Bool
isOnlySpace = Int
strippedLength Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
in if Bool
isOnlySpace
then WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go ([Int] -> Int -> WhitespaceStrippingState
RecordedWhitespace [Int]
withheldLines (Int
withheldSpaces Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
textLength)) SimpleDocStream ann
rest
else [Int] -> Int -> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
[Int] -> Int -> SimpleDocStream ann -> SimpleDocStream ann
commitWhitespace
[Int]
withheldLines
Int
withheldSpaces
(Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
SText Int
strippedLength
Text
stripped
(WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go ([Int] -> Int -> WhitespaceStrippingState
RecordedWhitespace [] Int
trailingLength) SimpleDocStream ann
rest))
SLine Int
i SimpleDocStream ann
rest -> WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go ([Int] -> Int -> WhitespaceStrippingState
RecordedWhitespace (Int
iInt -> [Int] -> [Int]
forall a. a -> [a] -> [a]
:[Int]
withheldLines) Int
0) SimpleDocStream ann
rest
SAnnPush ann
ann SimpleDocStream ann
rest -> [Int] -> Int -> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
[Int] -> Int -> SimpleDocStream ann -> SimpleDocStream ann
commitWhitespace
[Int]
withheldLines
Int
withheldSpaces
(ann -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. ann -> SimpleDocStream ann -> SimpleDocStream ann
SAnnPush ann
ann (WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
WhitespaceStrippingState
-> SimpleDocStream ann -> SimpleDocStream ann
go (Int -> WhitespaceStrippingState
AnnotationLevel Int
1) SimpleDocStream ann
rest))
SAnnPop SimpleDocStream ann
_ -> String -> SimpleDocStream ann
forall a. HasCallStack => String -> a
error String
"Tried skipping spaces in unannotated data! Please report this as a bug in 'prettyprinter'."
data WhitespaceStrippingState
= AnnotationLevel !Int
| RecordedWhitespace [Int] !Int
deriving Typeable
instance Functor SimpleDocStream where
fmap :: forall ann ann'.
(ann -> ann') -> SimpleDocStream ann -> SimpleDocStream ann'
fmap = (a -> b) -> SimpleDocStream a -> SimpleDocStream b
forall ann ann'.
(ann -> ann') -> SimpleDocStream ann -> SimpleDocStream ann'
reAnnotateS
instance Foldable SimpleDocStream where
foldMap :: forall m a. Monoid m => (a -> m) -> SimpleDocStream a -> m
foldMap a -> m
f = SimpleDocStream a -> m
go
where
go :: SimpleDocStream a -> m
go = \SimpleDocStream a
sds -> case SimpleDocStream a
sds of
SimpleDocStream a
SFail -> m
forall a. Monoid a => a
mempty
SimpleDocStream a
SEmpty -> m
forall a. Monoid a => a
mempty
SChar Char
_ SimpleDocStream a
rest -> SimpleDocStream a -> m
go SimpleDocStream a
rest
SText Int
_ Text
_ SimpleDocStream a
rest -> SimpleDocStream a -> m
go SimpleDocStream a
rest
SLine Int
_ SimpleDocStream a
rest -> SimpleDocStream a -> m
go SimpleDocStream a
rest
SAnnPush a
ann SimpleDocStream a
rest -> a -> m
f a
ann m -> m -> m
forall a. Monoid a => a -> a -> a
`mappend` SimpleDocStream a -> m
go SimpleDocStream a
rest
SAnnPop SimpleDocStream a
rest -> SimpleDocStream a -> m
go SimpleDocStream a
rest
instance Traversable SimpleDocStream where
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> SimpleDocStream a -> f (SimpleDocStream b)
traverse a -> f b
f = SimpleDocStream a -> f (SimpleDocStream b)
go
where
go :: SimpleDocStream a -> f (SimpleDocStream b)
go = \SimpleDocStream a
sds -> case SimpleDocStream a
sds of
SimpleDocStream a
SFail -> SimpleDocStream b -> f (SimpleDocStream b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure SimpleDocStream b
forall ann. SimpleDocStream ann
SFail
SimpleDocStream a
SEmpty -> SimpleDocStream b -> f (SimpleDocStream b)
forall a. a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure SimpleDocStream b
forall ann. SimpleDocStream ann
SEmpty
SChar Char
c SimpleDocStream a
rest -> Char -> SimpleDocStream b -> SimpleDocStream b
forall ann. Char -> SimpleDocStream ann -> SimpleDocStream ann
SChar Char
c (SimpleDocStream b -> SimpleDocStream b)
-> f (SimpleDocStream b) -> f (SimpleDocStream b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SimpleDocStream a -> f (SimpleDocStream b)
go SimpleDocStream a
rest
SText Int
l Text
t SimpleDocStream a
rest -> Int -> Text -> SimpleDocStream b -> SimpleDocStream b
forall ann.
Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
SText Int
l Text
t (SimpleDocStream b -> SimpleDocStream b)
-> f (SimpleDocStream b) -> f (SimpleDocStream b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SimpleDocStream a -> f (SimpleDocStream b)
go SimpleDocStream a
rest
SLine Int
i SimpleDocStream a
rest -> Int -> SimpleDocStream b -> SimpleDocStream b
forall ann. Int -> SimpleDocStream ann -> SimpleDocStream ann
SLine Int
i (SimpleDocStream b -> SimpleDocStream b)
-> f (SimpleDocStream b) -> f (SimpleDocStream b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SimpleDocStream a -> f (SimpleDocStream b)
go SimpleDocStream a
rest
SAnnPush a
ann SimpleDocStream a
rest -> b -> SimpleDocStream b -> SimpleDocStream b
forall ann. ann -> SimpleDocStream ann -> SimpleDocStream ann
SAnnPush (b -> SimpleDocStream b -> SimpleDocStream b)
-> f b -> f (SimpleDocStream b -> SimpleDocStream b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> f b
f a
ann f (SimpleDocStream b -> SimpleDocStream b)
-> f (SimpleDocStream b) -> f (SimpleDocStream b)
forall a b. f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> SimpleDocStream a -> f (SimpleDocStream b)
go SimpleDocStream a
rest
SAnnPop SimpleDocStream a
rest -> SimpleDocStream b -> SimpleDocStream b
forall ann. SimpleDocStream ann -> SimpleDocStream ann
SAnnPop (SimpleDocStream b -> SimpleDocStream b)
-> f (SimpleDocStream b) -> f (SimpleDocStream b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SimpleDocStream a -> f (SimpleDocStream b)
go SimpleDocStream a
rest
newtype FittingPredicate ann
= FittingPredicate (Int
-> Int
-> Maybe Int
-> SimpleDocStream ann
-> Bool)
deriving Typeable
data LayoutPipeline ann =
Nil
| Cons !Int (Doc ann) (LayoutPipeline ann)
| UndoAnn (LayoutPipeline ann)
deriving Typeable
data PageWidth
= AvailablePerLine !Int !Double
| Unbounded
deriving (PageWidth -> PageWidth -> Bool
(PageWidth -> PageWidth -> Bool)
-> (PageWidth -> PageWidth -> Bool) -> Eq PageWidth
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PageWidth -> PageWidth -> Bool
== :: PageWidth -> PageWidth -> Bool
$c/= :: PageWidth -> PageWidth -> Bool
/= :: PageWidth -> PageWidth -> Bool
Eq, Eq PageWidth
Eq PageWidth =>
(PageWidth -> PageWidth -> Ordering)
-> (PageWidth -> PageWidth -> Bool)
-> (PageWidth -> PageWidth -> Bool)
-> (PageWidth -> PageWidth -> Bool)
-> (PageWidth -> PageWidth -> Bool)
-> (PageWidth -> PageWidth -> PageWidth)
-> (PageWidth -> PageWidth -> PageWidth)
-> Ord PageWidth
PageWidth -> PageWidth -> Bool
PageWidth -> PageWidth -> Ordering
PageWidth -> PageWidth -> PageWidth
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: PageWidth -> PageWidth -> Ordering
compare :: PageWidth -> PageWidth -> Ordering
$c< :: PageWidth -> PageWidth -> Bool
< :: PageWidth -> PageWidth -> Bool
$c<= :: PageWidth -> PageWidth -> Bool
<= :: PageWidth -> PageWidth -> Bool
$c> :: PageWidth -> PageWidth -> Bool
> :: PageWidth -> PageWidth -> Bool
$c>= :: PageWidth -> PageWidth -> Bool
>= :: PageWidth -> PageWidth -> Bool
$cmax :: PageWidth -> PageWidth -> PageWidth
max :: PageWidth -> PageWidth -> PageWidth
$cmin :: PageWidth -> PageWidth -> PageWidth
min :: PageWidth -> PageWidth -> PageWidth
Ord, Int -> PageWidth -> ShowS
[PageWidth] -> ShowS
PageWidth -> String
(Int -> PageWidth -> ShowS)
-> (PageWidth -> String)
-> ([PageWidth] -> ShowS)
-> Show PageWidth
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PageWidth -> ShowS
showsPrec :: Int -> PageWidth -> ShowS
$cshow :: PageWidth -> String
show :: PageWidth -> String
$cshowList :: [PageWidth] -> ShowS
showList :: [PageWidth] -> ShowS
Show, Typeable)
defaultPageWidth :: PageWidth
defaultPageWidth :: PageWidth
defaultPageWidth = Int -> Double -> PageWidth
AvailablePerLine Int
80 Double
1
remainingWidth :: Int -> Double -> Int -> Int -> Int
remainingWidth :: Int -> Double -> Int -> Int -> Int
remainingWidth Int
lineLength Double
ribbonFraction Int
lineIndent Int
currentColumn =
Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
columnsLeftInLine Int
columnsLeftInRibbon
where
columnsLeftInLine :: Int
columnsLeftInLine = Int
lineLength Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
currentColumn
columnsLeftInRibbon :: Int
columnsLeftInRibbon = Int
lineIndent Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
ribbonWidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
currentColumn
ribbonWidth :: Int
ribbonWidth =
(Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int -> Int) -> (Double -> Int) -> Double -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
lineLength (Int -> Int) -> (Double -> Int) -> Double -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
floor)
(Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
lineLength Double -> Double -> Double
forall a. Num a => a -> a -> a
* Double
ribbonFraction)
newtype LayoutOptions = LayoutOptions { LayoutOptions -> PageWidth
layoutPageWidth :: PageWidth }
deriving (LayoutOptions -> LayoutOptions -> Bool
(LayoutOptions -> LayoutOptions -> Bool)
-> (LayoutOptions -> LayoutOptions -> Bool) -> Eq LayoutOptions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LayoutOptions -> LayoutOptions -> Bool
== :: LayoutOptions -> LayoutOptions -> Bool
$c/= :: LayoutOptions -> LayoutOptions -> Bool
/= :: LayoutOptions -> LayoutOptions -> Bool
Eq, Eq LayoutOptions
Eq LayoutOptions =>
(LayoutOptions -> LayoutOptions -> Ordering)
-> (LayoutOptions -> LayoutOptions -> Bool)
-> (LayoutOptions -> LayoutOptions -> Bool)
-> (LayoutOptions -> LayoutOptions -> Bool)
-> (LayoutOptions -> LayoutOptions -> Bool)
-> (LayoutOptions -> LayoutOptions -> LayoutOptions)
-> (LayoutOptions -> LayoutOptions -> LayoutOptions)
-> Ord LayoutOptions
LayoutOptions -> LayoutOptions -> Bool
LayoutOptions -> LayoutOptions -> Ordering
LayoutOptions -> LayoutOptions -> LayoutOptions
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: LayoutOptions -> LayoutOptions -> Ordering
compare :: LayoutOptions -> LayoutOptions -> Ordering
$c< :: LayoutOptions -> LayoutOptions -> Bool
< :: LayoutOptions -> LayoutOptions -> Bool
$c<= :: LayoutOptions -> LayoutOptions -> Bool
<= :: LayoutOptions -> LayoutOptions -> Bool
$c> :: LayoutOptions -> LayoutOptions -> Bool
> :: LayoutOptions -> LayoutOptions -> Bool
$c>= :: LayoutOptions -> LayoutOptions -> Bool
>= :: LayoutOptions -> LayoutOptions -> Bool
$cmax :: LayoutOptions -> LayoutOptions -> LayoutOptions
max :: LayoutOptions -> LayoutOptions -> LayoutOptions
$cmin :: LayoutOptions -> LayoutOptions -> LayoutOptions
min :: LayoutOptions -> LayoutOptions -> LayoutOptions
Ord, Int -> LayoutOptions -> ShowS
[LayoutOptions] -> ShowS
LayoutOptions -> String
(Int -> LayoutOptions -> ShowS)
-> (LayoutOptions -> String)
-> ([LayoutOptions] -> ShowS)
-> Show LayoutOptions
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LayoutOptions -> ShowS
showsPrec :: Int -> LayoutOptions -> ShowS
$cshow :: LayoutOptions -> String
show :: LayoutOptions -> String
$cshowList :: [LayoutOptions] -> ShowS
showList :: [LayoutOptions] -> ShowS
Show, Typeable)
defaultLayoutOptions :: LayoutOptions
defaultLayoutOptions :: LayoutOptions
defaultLayoutOptions = LayoutOptions { layoutPageWidth :: PageWidth
layoutPageWidth = PageWidth
defaultPageWidth }
layoutPretty
:: LayoutOptions
-> Doc ann
-> SimpleDocStream ann
layoutPretty :: forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutPretty (LayoutOptions pageWidth_ :: PageWidth
pageWidth_@(AvailablePerLine Int
lineLength Double
ribbonFraction)) =
FittingPredicate ann -> PageWidth -> Doc ann -> SimpleDocStream ann
forall ann.
FittingPredicate ann -> PageWidth -> Doc ann -> SimpleDocStream ann
layoutWadlerLeijen
((Int -> Int -> Maybe Int -> SimpleDocStream ann -> Bool)
-> FittingPredicate ann
forall ann.
(Int -> Int -> Maybe Int -> SimpleDocStream ann -> Bool)
-> FittingPredicate ann
FittingPredicate
(\Int
lineIndent Int
currentColumn Maybe Int
_initialIndentY SimpleDocStream ann
sdoc ->
Int -> SimpleDocStream ann -> Bool
forall ann. Int -> SimpleDocStream ann -> Bool
fits
(Int -> Double -> Int -> Int -> Int
remainingWidth Int
lineLength Double
ribbonFraction Int
lineIndent Int
currentColumn)
SimpleDocStream ann
sdoc))
PageWidth
pageWidth_
where
fits :: Int
-> SimpleDocStream ann
-> Bool
fits :: forall ann. Int -> SimpleDocStream ann -> Bool
fits Int
w SimpleDocStream ann
_ | Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Bool
False
fits Int
_ SimpleDocStream ann
SFail = Bool
False
fits Int
_ SimpleDocStream ann
SEmpty = Bool
True
fits Int
w (SChar Char
_ SimpleDocStream ann
x) = Int -> SimpleDocStream ann -> Bool
forall ann. Int -> SimpleDocStream ann -> Bool
fits (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) SimpleDocStream ann
x
fits Int
w (SText Int
l Text
_t SimpleDocStream ann
x) = Int -> SimpleDocStream ann -> Bool
forall ann. Int -> SimpleDocStream ann -> Bool
fits (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l) SimpleDocStream ann
x
fits Int
_ SLine{} = Bool
True
fits Int
w (SAnnPush ann
_ SimpleDocStream ann
x) = Int -> SimpleDocStream ann -> Bool
forall ann. Int -> SimpleDocStream ann -> Bool
fits Int
w SimpleDocStream ann
x
fits Int
w (SAnnPop SimpleDocStream ann
x) = Int -> SimpleDocStream ann -> Bool
forall ann. Int -> SimpleDocStream ann -> Bool
fits Int
w SimpleDocStream ann
x
layoutPretty (LayoutOptions PageWidth
Unbounded) = Doc ann -> SimpleDocStream ann
forall ann. Doc ann -> SimpleDocStream ann
layoutUnbounded
layoutSmart
:: LayoutOptions
-> Doc ann
-> SimpleDocStream ann
layoutSmart :: forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutSmart (LayoutOptions pageWidth_ :: PageWidth
pageWidth_@(AvailablePerLine Int
lineLength Double
ribbonFraction)) =
FittingPredicate ann -> PageWidth -> Doc ann -> SimpleDocStream ann
forall ann.
FittingPredicate ann -> PageWidth -> Doc ann -> SimpleDocStream ann
layoutWadlerLeijen ((Int -> Int -> Maybe Int -> SimpleDocStream ann -> Bool)
-> FittingPredicate ann
forall ann.
(Int -> Int -> Maybe Int -> SimpleDocStream ann -> Bool)
-> FittingPredicate ann
FittingPredicate Int -> Int -> Maybe Int -> SimpleDocStream ann -> Bool
forall ann. Int -> Int -> Maybe Int -> SimpleDocStream ann -> Bool
fits) PageWidth
pageWidth_
where
fits :: Int -> Int -> Maybe Int -> SimpleDocStream ann -> Bool
fits :: forall ann. Int -> Int -> Maybe Int -> SimpleDocStream ann -> Bool
fits Int
lineIndent Int
currentColumn Maybe Int
initialIndentY = Int -> SimpleDocStream ann -> Bool
forall ann. Int -> SimpleDocStream ann -> Bool
go Int
availableWidth
where
go :: Int -> SimpleDocStream ann -> Bool
go Int
w SimpleDocStream ann
_ | Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 = Bool
False
go Int
_ SimpleDocStream ann
SFail = Bool
False
go Int
_ SimpleDocStream ann
SEmpty = Bool
True
go Int
w (SChar Char
_ SimpleDocStream ann
x) = Int -> SimpleDocStream ann -> Bool
go (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) SimpleDocStream ann
x
go Int
w (SText Int
l Text
_t SimpleDocStream ann
x) = Int -> SimpleDocStream ann -> Bool
go (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
l) SimpleDocStream ann
x
go Int
_ (SLine Int
i SimpleDocStream ann
x)
| Int
minNestingLevel Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
i = Int -> SimpleDocStream ann -> Bool
go (Int
lineLength Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i) SimpleDocStream ann
x
| Bool
otherwise = Bool
True
go Int
w (SAnnPush ann
_ SimpleDocStream ann
x) = Int -> SimpleDocStream ann -> Bool
go Int
w SimpleDocStream ann
x
go Int
w (SAnnPop SimpleDocStream ann
x) = Int -> SimpleDocStream ann -> Bool
go Int
w SimpleDocStream ann
x
availableWidth :: Int
availableWidth = Int -> Double -> Int -> Int -> Int
remainingWidth Int
lineLength Double
ribbonFraction Int
lineIndent Int
currentColumn
minNestingLevel :: Int
minNestingLevel =
case Maybe Int
initialIndentY of
Just Int
i ->
Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
i Int
currentColumn
Maybe Int
Nothing ->
Int
currentColumn
layoutSmart (LayoutOptions PageWidth
Unbounded) = Doc ann -> SimpleDocStream ann
forall ann. Doc ann -> SimpleDocStream ann
layoutUnbounded
layoutUnbounded :: Doc ann -> SimpleDocStream ann
layoutUnbounded :: forall ann. Doc ann -> SimpleDocStream ann
layoutUnbounded =
FittingPredicate ann -> PageWidth -> Doc ann -> SimpleDocStream ann
forall ann.
FittingPredicate ann -> PageWidth -> Doc ann -> SimpleDocStream ann
layoutWadlerLeijen
((Int -> Int -> Maybe Int -> SimpleDocStream ann -> Bool)
-> FittingPredicate ann
forall ann.
(Int -> Int -> Maybe Int -> SimpleDocStream ann -> Bool)
-> FittingPredicate ann
FittingPredicate
(\Int
_lineIndent Int
_currentColumn Maybe Int
_initialIndentY SimpleDocStream ann
sdoc -> Bool -> Bool
not (SimpleDocStream ann -> Bool
forall a. SimpleDocStream a -> Bool
failsOnFirstLine SimpleDocStream ann
sdoc)))
PageWidth
Unbounded
where
failsOnFirstLine :: SimpleDocStream ann -> Bool
failsOnFirstLine :: forall a. SimpleDocStream a -> Bool
failsOnFirstLine = SimpleDocStream ann -> Bool
forall a. SimpleDocStream a -> Bool
go
where
go :: SimpleDocStream ann -> Bool
go SimpleDocStream ann
sds = case SimpleDocStream ann
sds of
SimpleDocStream ann
SFail -> Bool
True
SimpleDocStream ann
SEmpty -> Bool
False
SChar Char
_ SimpleDocStream ann
s -> SimpleDocStream ann -> Bool
go SimpleDocStream ann
s
SText Int
_ Text
_ SimpleDocStream ann
s -> SimpleDocStream ann -> Bool
go SimpleDocStream ann
s
SLine Int
_ SimpleDocStream ann
_ -> Bool
False
SAnnPush ann
_ SimpleDocStream ann
s -> SimpleDocStream ann -> Bool
go SimpleDocStream ann
s
SAnnPop SimpleDocStream ann
s -> SimpleDocStream ann -> Bool
go SimpleDocStream ann
s
layoutWadlerLeijen
:: forall ann. FittingPredicate ann
-> PageWidth
-> Doc ann
-> SimpleDocStream ann
layoutWadlerLeijen :: forall ann.
FittingPredicate ann -> PageWidth -> Doc ann -> SimpleDocStream ann
layoutWadlerLeijen
(FittingPredicate Int -> Int -> Maybe Int -> SimpleDocStream ann -> Bool
fits)
PageWidth
pageWidth_
Doc ann
doc
= Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
0 Int
0 (Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
forall ann.
Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
Cons Int
0 Doc ann
doc LayoutPipeline ann
forall ann. LayoutPipeline ann
Nil)
where
best
:: Int
-> Int
-> LayoutPipeline ann
-> SimpleDocStream ann
best :: Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best !Int
_ !Int
_ LayoutPipeline ann
Nil = SimpleDocStream ann
forall ann. SimpleDocStream ann
SEmpty
best Int
nl Int
cc (UndoAnn LayoutPipeline ann
ds) = SimpleDocStream ann -> SimpleDocStream ann
forall ann. SimpleDocStream ann -> SimpleDocStream ann
SAnnPop (Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc LayoutPipeline ann
ds)
best Int
nl Int
cc (Cons Int
i Doc ann
d LayoutPipeline ann
ds) = case Doc ann
d of
Doc ann
Fail -> SimpleDocStream ann
forall ann. SimpleDocStream ann
SFail
Doc ann
Empty -> Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc LayoutPipeline ann
ds
Char Char
c -> let !cc' :: Int
cc' = Int
ccInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1 in Char -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. Char -> SimpleDocStream ann -> SimpleDocStream ann
SChar Char
c (Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc' LayoutPipeline ann
ds)
Text Int
l Text
t -> let !cc' :: Int
cc' = Int
ccInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
l in Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
SText Int
l Text
t (Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc' LayoutPipeline ann
ds)
Doc ann
Line -> let x :: SimpleDocStream ann
x = Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
i Int
i LayoutPipeline ann
ds
i' :: Int
i' = case SimpleDocStream ann
x of
SimpleDocStream ann
SEmpty -> Int
0
SLine{} -> Int
0
SimpleDocStream ann
_ -> Int
i
in Int -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. Int -> SimpleDocStream ann -> SimpleDocStream ann
SLine Int
i' SimpleDocStream ann
x
FlatAlt Doc ann
x Doc ann
_ -> Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc (Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
forall ann.
Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
Cons Int
i Doc ann
x LayoutPipeline ann
ds)
Cat Doc ann
x Doc ann
y -> Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc (Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
forall ann.
Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
Cons Int
i Doc ann
x (Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
forall ann.
Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
Cons Int
i Doc ann
y LayoutPipeline ann
ds))
Nest Int
j Doc ann
x -> let !ij :: Int
ij = Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
j in Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc (Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
forall ann.
Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
Cons Int
ij Doc ann
x LayoutPipeline ann
ds)
Union Doc ann
x Doc ann
y -> let x' :: SimpleDocStream ann
x' = Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc (Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
forall ann.
Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
Cons Int
i Doc ann
x LayoutPipeline ann
ds)
y' :: SimpleDocStream ann
y' = Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc (Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
forall ann.
Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
Cons Int
i Doc ann
y LayoutPipeline ann
ds)
in Int
-> Int
-> SimpleDocStream ann
-> SimpleDocStream ann
-> SimpleDocStream ann
selectNicer Int
nl Int
cc SimpleDocStream ann
x' SimpleDocStream ann
y'
Column Int -> Doc ann
f -> Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc (Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
forall ann.
Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
Cons Int
i (Int -> Doc ann
f Int
cc) LayoutPipeline ann
ds)
WithPageWidth PageWidth -> Doc ann
f -> Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc (Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
forall ann.
Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
Cons Int
i (PageWidth -> Doc ann
f PageWidth
pageWidth_) LayoutPipeline ann
ds)
Nesting Int -> Doc ann
f -> Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc (Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
forall ann.
Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
Cons Int
i (Int -> Doc ann
f Int
i) LayoutPipeline ann
ds)
Annotated ann
ann Doc ann
x -> ann -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. ann -> SimpleDocStream ann -> SimpleDocStream ann
SAnnPush ann
ann (Int -> Int -> LayoutPipeline ann -> SimpleDocStream ann
best Int
nl Int
cc (Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
forall ann.
Int -> Doc ann -> LayoutPipeline ann -> LayoutPipeline ann
Cons Int
i Doc ann
x (LayoutPipeline ann -> LayoutPipeline ann
forall ann. LayoutPipeline ann -> LayoutPipeline ann
UndoAnn LayoutPipeline ann
ds)))
selectNicer
:: Int
-> Int
-> SimpleDocStream ann
-> SimpleDocStream ann
-> SimpleDocStream ann
selectNicer :: Int
-> Int
-> SimpleDocStream ann
-> SimpleDocStream ann
-> SimpleDocStream ann
selectNicer Int
lineIndent Int
currentColumn SimpleDocStream ann
x SimpleDocStream ann
y
| Int -> Int -> Maybe Int -> SimpleDocStream ann -> Bool
fits Int
lineIndent Int
currentColumn (SimpleDocStream ann -> Maybe Int
initialIndentation SimpleDocStream ann
y) SimpleDocStream ann
x = SimpleDocStream ann
x
| Bool
otherwise = SimpleDocStream ann
y
initialIndentation :: SimpleDocStream ann -> Maybe Int
initialIndentation :: SimpleDocStream ann -> Maybe Int
initialIndentation SimpleDocStream ann
sds = case SimpleDocStream ann
sds of
SLine Int
i SimpleDocStream ann
_ -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
i
SAnnPush ann
_ SimpleDocStream ann
s -> SimpleDocStream ann -> Maybe Int
initialIndentation SimpleDocStream ann
s
SAnnPop SimpleDocStream ann
s -> SimpleDocStream ann -> Maybe Int
initialIndentation SimpleDocStream ann
s
SimpleDocStream ann
_ -> Maybe Int
forall a. Maybe a
Nothing
layoutCompact :: Doc ann1 -> SimpleDocStream ann2
layoutCompact :: forall ann1 ann2. Doc ann1 -> SimpleDocStream ann2
layoutCompact Doc ann1
doc = Int -> [Doc ann1] -> SimpleDocStream ann2
forall {ann} {ann}. Int -> [Doc ann] -> SimpleDocStream ann
scan Int
0 [Doc ann1
doc]
where
scan :: Int -> [Doc ann] -> SimpleDocStream ann
scan Int
_ [] = SimpleDocStream ann
forall ann. SimpleDocStream ann
SEmpty
scan !Int
col (Doc ann
d:[Doc ann]
ds) = case Doc ann
d of
Doc ann
Fail -> SimpleDocStream ann
forall ann. SimpleDocStream ann
SFail
Doc ann
Empty -> Int -> [Doc ann] -> SimpleDocStream ann
scan Int
col [Doc ann]
ds
Char Char
c -> Char -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. Char -> SimpleDocStream ann -> SimpleDocStream ann
SChar Char
c (Int -> [Doc ann] -> SimpleDocStream ann
scan (Int
colInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) [Doc ann]
ds)
Text Int
l Text
t -> let !col' :: Int
col' = Int
colInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
l in Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
forall ann.
Int -> Text -> SimpleDocStream ann -> SimpleDocStream ann
SText Int
l Text
t (Int -> [Doc ann] -> SimpleDocStream ann
scan Int
col' [Doc ann]
ds)
FlatAlt Doc ann
x Doc ann
_ -> Int -> [Doc ann] -> SimpleDocStream ann
scan Int
col (Doc ann
xDoc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
:[Doc ann]
ds)
Doc ann
Line -> Int -> SimpleDocStream ann -> SimpleDocStream ann
forall ann. Int -> SimpleDocStream ann -> SimpleDocStream ann
SLine Int
0 (Int -> [Doc ann] -> SimpleDocStream ann
scan Int
0 [Doc ann]
ds)
Cat Doc ann
x Doc ann
y -> Int -> [Doc ann] -> SimpleDocStream ann
scan Int
col (Doc ann
xDoc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
:Doc ann
yDoc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
:[Doc ann]
ds)
Nest Int
_ Doc ann
x -> Int -> [Doc ann] -> SimpleDocStream ann
scan Int
col (Doc ann
xDoc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
:[Doc ann]
ds)
Union Doc ann
_ Doc ann
y -> Int -> [Doc ann] -> SimpleDocStream ann
scan Int
col (Doc ann
yDoc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
:[Doc ann]
ds)
Column Int -> Doc ann
f -> Int -> [Doc ann] -> SimpleDocStream ann
scan Int
col (Int -> Doc ann
f Int
colDoc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
:[Doc ann]
ds)
WithPageWidth PageWidth -> Doc ann
f -> Int -> [Doc ann] -> SimpleDocStream ann
scan Int
col (PageWidth -> Doc ann
f PageWidth
Unbounded Doc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
: [Doc ann]
ds)
Nesting Int -> Doc ann
f -> Int -> [Doc ann] -> SimpleDocStream ann
scan Int
col (Int -> Doc ann
f Int
0 Doc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
: [Doc ann]
ds)
Annotated ann
_ Doc ann
x -> Int -> [Doc ann] -> SimpleDocStream ann
scan Int
col (Doc ann
xDoc ann -> [Doc ann] -> [Doc ann]
forall a. a -> [a] -> [a]
:[Doc ann]
ds)
instance Show (Doc ann) where
showsPrec :: Int -> Doc ann -> ShowS
showsPrec Int
_ Doc ann
doc = SimpleDocStream ann -> ShowS
forall ann. SimpleDocStream ann -> ShowS
renderShowS (LayoutOptions -> Doc ann -> SimpleDocStream ann
forall ann. LayoutOptions -> Doc ann -> SimpleDocStream ann
layoutPretty LayoutOptions
defaultLayoutOptions Doc ann
doc)
renderShowS :: SimpleDocStream ann -> ShowS
renderShowS :: forall ann. SimpleDocStream ann -> ShowS
renderShowS = \SimpleDocStream ann
sds -> case SimpleDocStream ann
sds of
SimpleDocStream ann
SFail -> ShowS
forall void. void
panicUncaughtFail
SimpleDocStream ann
SEmpty -> ShowS
forall a. a -> a
id
SChar Char
c SimpleDocStream ann
x -> Char -> ShowS
showChar Char
c ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimpleDocStream ann -> ShowS
forall ann. SimpleDocStream ann -> ShowS
renderShowS SimpleDocStream ann
x
SText Int
_l Text
t SimpleDocStream ann
x -> String -> ShowS
showString (Text -> String
T.unpack Text
t) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimpleDocStream ann -> ShowS
forall ann. SimpleDocStream ann -> ShowS
renderShowS SimpleDocStream ann
x
SLine Int
i SimpleDocStream ann
x -> String -> ShowS
showString (Char
'\n' Char -> ShowS
forall a. a -> [a] -> [a]
: Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
i Char
' ') ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SimpleDocStream ann -> ShowS
forall ann. SimpleDocStream ann -> ShowS
renderShowS SimpleDocStream ann
x
SAnnPush ann
_ SimpleDocStream ann
x -> SimpleDocStream ann -> ShowS
forall ann. SimpleDocStream ann -> ShowS
renderShowS SimpleDocStream ann
x
SAnnPop SimpleDocStream ann
x -> SimpleDocStream ann -> ShowS
forall ann. SimpleDocStream ann -> ShowS
renderShowS SimpleDocStream ann
x
textSpaces :: Int -> Text
textSpaces :: Int -> Text
textSpaces Int
n = Int -> Text -> Text
T.replicate Int
n (Char -> Text
T.singleton Char
' ')