1 {-# LANGUAGE DeriveFunctor #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE OverloadedStrings #-}
4 module Hdoc.TCT.Cell where
6 import Data.Default.Class (Default(..))
7 import Data.Eq (Eq(..))
8 import Data.Foldable (toList)
9 import Data.Function (($), (.))
10 import Data.Functor (Functor)
11 import Data.List.NonEmpty (NonEmpty(..))
12 import Data.Ord (Ord(..))
13 import Data.Semigroup (Semigroup(..))
14 import Data.Text (Text)
15 import Prelude (Int, Num(..), fromIntegral)
16 import System.FilePath (FilePath)
17 import Text.Show (Show(..), showChar, showString, showParen)
18 import qualified Data.Text as Text
19 import qualified Data.Text.Lazy as TL
24 -- | Relative position
27 { pos_line :: {-# UNPACK #-} !LineNum
28 , pos_column :: {-# UNPACK #-} !ColNum
30 instance Default Pos where
33 instance Semigroup Pos where
34 Pos lx cx <> Pos ly cy =
36 instance Monoid Pos where
40 instance Show Pos where
41 showsPrec _p Pos{..} =
42 showsPrec 11 pos_line .
44 showsPrec 11 pos_column
59 { span_file :: !FilePath
63 instance Default Span where
64 def = Span "" pos1 pos1
65 instance Show Span where
66 showsPrec _p Span{..} =
67 showString span_file .
68 showChar '#' . showsPrec 10 span_begin .
69 showChar '-' . showsPrec 10 span_end
72 type Location = NonEmpty Span
77 { cell_location :: !Location
79 } deriving (Eq, Ord, Functor)
80 instance Show a => Show (Cell a) where
81 showsPrec p Cell{..} =
84 showChar ' ' . showsPrec 11 unCell .
85 showChar ' ' . showsPrec 10 (toList cell_location)
86 instance (Pretty a, Show a) => Pretty (Cell a)
87 instance (FromPad a, Semigroup a) => Semigroup (Cell a) where
88 Cell (Span fx bx ex :| lx) x <> Cell (Span _fy by ey :| _ly) y =
89 Cell (Span fx bx ey :| lx) $
90 x<>fromPad (Pos lines columns)<>y
92 lines = pos_line by - pos_line ex
93 columns = pos_column by - pos_column (if lines <= 0 then ex else bx)
95 instance (FromPad a, Semigroup a, Monoid a) => Monoid (Cell a) where
101 cell0 = Cell (def :| [])
104 class FromPad a where
106 instance FromPad Text where
108 Text.replicate pos_line "\n" <>
109 Text.replicate pos_column " "
110 instance FromPad TL.Text where
112 TL.replicate (fromIntegral pos_line) "\n" <>
113 TL.replicate (fromIntegral pos_column) " "