1 module Language.Symantic.Document.Term.Dim
2 ( module Language.Symantic.Document.Sym
3 , module Language.Symantic.Document.Term.Dim
6 import Control.Applicative (Applicative(..))
8 import Data.Eq (Eq(..))
9 import Data.Function (($), (.), id)
11 import Data.Monoid (Monoid(..))
12 import Data.Ord (Ord(..))
13 import Data.Semigroup (Semigroup(..))
14 import Data.String (IsString(..))
15 import GHC.Exts (IsList(..))
16 import Prelude ((+), pred)
17 import Text.Show (Show(..))
18 import qualified Data.List as List
19 import qualified Data.Text as Text
20 import qualified Data.Text.Lazy as TL
22 import Language.Symantic.Document.Sym
27 { dim_width :: Int -- ^ Maximun line length.
28 , dim_height :: Int -- ^ Number of newlines.
29 , dim_width_first :: Int -- ^ Length of the first line.
30 , dim_width_last :: Int -- ^ Length of the last line.
32 instance Semigroup Dim where
33 Dim{dim_width=wx, dim_height=hx, dim_width_first=wfx, dim_width_last=wlx} <>
34 Dim{dim_width=wy, dim_height=hy, dim_width_first=wfy, dim_width_last=wly} =
37 (0, 0) -> let w = wx + wy in Dim w h w w
38 (0, _) -> let v = wfx + wfy in Dim (max v (wx + wy)) h v wly
39 (_, 0) -> let v = wlx + wfy in Dim (max v (wx + wy)) h wfx v
40 _ -> Dim (max wx wy) h wfx wly
41 instance Monoid Dim where
48 { reader_indent :: !(Indent Dimension) -- ^ Current indentation level, used by 'newline'.
49 , reader_newline :: Dimension -- ^ How to display 'newline'.
50 , reader_wrap_column :: !(Column Dimension) -- ^ 'Column' after which 'wrap' breaks on a 'breakpoint' or 'breakspace'.
51 , reader_colorable :: !Bool -- ^ Whether colors are activated or not.
52 , reader_decorable :: !Bool -- ^ Whether decorations are activated or not.
55 -- | Default 'Reader'.
59 , reader_newline = newlineWithIndent
60 , reader_wrap_column = 80
61 , reader_colorable = True
62 , reader_decorable = True
66 type State = Column Dimension
74 { unDimension :: Reader ->
76 (State -> Dim -> Dim) -> -- normal continuation
77 (State -> Dim -> Dim) -> -- should-wrap continuation
79 type instance Column Dimension = Int
80 type instance Indent Dimension = Int
82 dim :: Dimension -> Dim
83 dim (Dimension p) = p defReader defState oko oko
86 instance IsList Dimension where
87 type Item Dimension = Dimension
90 instance Semigroup Dimension where
91 x <> y = Dimension $ \ro st ok ko ->
93 (\sx tx -> unDimension y ro sx
94 (\sy ty -> ok sy (tx<>ty))
95 (\sy ty -> ko sy (tx<>ty)))
96 (\sx tx -> unDimension y ro sx
97 (\sy ty -> ko sy (tx<>ty))
98 (\sy ty -> ko sy (tx<>ty)))
99 instance Monoid Dimension where
102 instance IsString Dimension where
105 writeH :: Column Dimension -> Dimension
107 Dimension $ \ro col ok ko ->
108 let newCol = col + len in
109 (if newCol <= reader_wrap_column ro then ok else ko)
113 , dim_width_last = newCol
114 , dim_width_first = newCol
117 instance Doc_Text Dimension where
118 empty = Dimension $ \_ro st ok _ko -> ok st mempty
120 stringH t = writeH $ List.length t
121 textH t = writeH $ Text.length t
122 ltextH t = writeH $ intOfInt64 $ TL.length t
124 integer = stringH . show
125 replicate cnt p | cnt <= 0 = empty
126 | otherwise = p <> replicate (pred cnt) p
127 newline = Dimension $ \ro -> unDimension (reader_newline ro) ro
128 instance Doc_Align Dimension where
129 align p = Dimension $ \ro st -> unDimension p ro{reader_indent=st} st
130 withNewline nl p = Dimension $ \ro -> unDimension p ro{reader_newline=nl}
131 withIndent ind p = Dimension $ \ro -> unDimension p ro{reader_indent=ind}
132 incrIndent ind p = Dimension $ \ro -> unDimension p ro{reader_indent=reader_indent ro + ind}
133 column f = Dimension $ \ro st -> unDimension (f st) ro st
134 newlineWithoutIndent = Dimension $ \_ro _st ok _ko ->
138 , dim_width_first = 0
141 newlineWithIndent = Dimension $ \ro _st ok _ko ->
142 let ind = reader_indent ro in
146 , dim_width_first = 0
147 , dim_width_last = ind
150 instance Doc_Wrap Dimension where
151 ifFit x y = Dimension $ \ro st ok ko ->
152 unDimension x ro st ok (\_sx _tx -> unDimension y ro st ok ko)
153 breakpoint onNoBreak onBreak p = Dimension $ \ro st ok ko ->
154 unDimension (onNoBreak <> p) ro st ok
155 (\_sp _tp -> unDimension (onBreak <> p) ro st ok ko)
156 withWrapColumn col p = Dimension $ \ro -> unDimension p ro{reader_wrap_column=col}
157 instance Doc_Color Dimension where
158 colorable f = Dimension $ \ro -> unDimension (f (reader_colorable ro)) ro
159 withColorable b t = Dimension $ \ro -> unDimension t ro{reader_colorable=b}
193 instance Doc_Decoration Dimension where
194 decorable f = Dimension $ \ro -> unDimension (f (reader_decorable ro)) ro
195 withDecorable b t = Dimension $ \ro -> unDimension t ro{reader_decorable=b}