1+ {-# LANGUAGE FlexibleInstances #-}
2+
13module Options.Applicative.Help.Chunk
24 ( Chunk (.. )
35 , chunked
@@ -11,6 +13,8 @@ module Options.Applicative.Help.Chunk
1113 , paragraph
1214 , extractChunk
1315 , tabulate
16+ , chunkFlatAlt
17+ , chunkIsEffectivelyEmpty
1418 ) where
1519
1620import Control.Applicative
@@ -20,13 +24,17 @@ import Data.Maybe
2024import Data.Semigroup
2125import Prelude
2226
27+ import Options.Applicative.Help.Ann
2328import Options.Applicative.Help.Pretty
2429
2530-- | The free monoid on a semigroup 'a'.
2631newtype Chunk a = Chunk
2732 { unChunk :: Maybe a }
2833 deriving (Eq , Show )
2934
35+ instance CanAnnotate (Chunk Doc ) where
36+ annTrace n = fmap . annTrace n
37+
3038instance Functor Chunk where
3139 fmap f = Chunk . fmap f . unChunk
3240
@@ -89,20 +97,20 @@ extractChunk = fromMaybe mempty . unChunk
8997-- Unlike '<+>' for 'Doc', this operation has a unit element, namely the empty
9098-- 'Chunk'.
9199(<<+>>) :: Chunk Doc -> Chunk Doc -> Chunk Doc
92- (<<+>>) = chunked (<+>)
100+ (<<+>>) = fmap (annTrace 1 " (<<+>>) " ) . chunked (<+>)
93101
94102-- | Concatenate two 'Chunk's with a softline in between. This is exactly like
95103-- '<<+>>', but uses a softline instead of a space.
96104(<</>>) :: Chunk Doc -> Chunk Doc -> Chunk Doc
97- (<</>>) = chunked (</>)
105+ (<</>>) = fmap (annTrace 1 " (<</>>) " ) . chunked (</>)
98106
99107-- | Concatenate 'Chunk's vertically.
100108vcatChunks :: [Chunk Doc ] -> Chunk Doc
101- vcatChunks = foldr (chunked (.$.) ) mempty
109+ vcatChunks = fmap (annTrace 1 " vcatChunks " ) . foldr (chunked (.$.) ) mempty
102110
103111-- | Concatenate 'Chunk's vertically separated by empty lines.
104112vsepChunks :: [Chunk Doc ] -> Chunk Doc
105- vsepChunks = foldr (chunked (\ x y -> x .$. mempty .$. y)) mempty
113+ vsepChunks = annTrace 1 " vsepChunks " . foldr (chunked (\ x y -> x .$. mempty .$. y)) mempty
106114
107115-- | Whether a 'Chunk' is empty. Note that something like 'pure mempty' is not
108116-- considered an empty chunk, even though the underlying 'Doc' is empty.
@@ -114,8 +122,8 @@ isEmpty = isNothing . unChunk
114122-- > isEmpty . stringChunk = null
115123-- > extractChunk . stringChunk = string
116124stringChunk :: String -> Chunk Doc
117- stringChunk " " = mempty
118- stringChunk s = pure (string s)
125+ stringChunk " " = annTrace 0 " stringChunk " mempty
126+ stringChunk s = annTrace 0 " stringChunk " $ pure (string s)
119127
120128-- | Convert a paragraph into a 'Chunk'. The resulting chunk is composed by the
121129-- words of the original paragraph separated by softlines, so it will be
@@ -125,12 +133,19 @@ stringChunk s = pure (string s)
125133--
126134-- > isEmpty . paragraph = null . words
127135paragraph :: String -> Chunk Doc
128- paragraph = foldr (chunked (</>) . stringChunk) mempty
129- . words
136+ paragraph = annTrace 0 " paragraph"
137+ . foldr (chunked (</>) . stringChunk) mempty
138+ . words
130139
131140-- | Display pairs of strings in a table.
132141tabulate :: Int -> [(Doc , Doc )] -> Chunk Doc
133- tabulate _ [] = mempty
134- tabulate size table = pure $ vcat
142+ tabulate _ [] = annTrace 1 " tabulate " mempty
143+ tabulate size table = annTrace 1 " tabulate " . pure $ vcat
135144 [ indent 2 (fillBreak size key <+> value)
136145 | (key, value) <- table ]
146+
147+ chunkFlatAlt :: Chunk Doc -> Chunk Doc -> Chunk Doc
148+ chunkFlatAlt a b = pure (flatAlt (extractChunk a) (extractChunk b))
149+
150+ chunkIsEffectivelyEmpty :: Chunk Doc -> Bool
151+ chunkIsEffectivelyEmpty = fromMaybe True . fmap isEffectivelyEmpty . unChunk
0 commit comments