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 -- | Absolute text file position.
26 { pos_line :: {-# UNPACK #-} !LineNum
27 , pos_column :: {-# UNPACK #-} !ColNum
29 instance Default Pos where
32 instance Semigroup Pos where
33 Pos lx cx <> Pos ly cy =
35 instance Monoid Pos where
39 instance Show Pos where
40 showsPrec _p Pos{..} =
41 showsPrec 11 pos_line .
43 showsPrec 11 pos_column
58 { cell_location :: !Location
60 } deriving (Eq, Ord, Functor)
61 instance Show a => Show (Cell a) where
62 showsPrec p Cell{..} =
65 showChar ' ' . showsPrec 11 unCell .
66 showChar ' ' . showsPrec 10 (toList cell_location)
67 instance (Pretty a, Show a) => Pretty (Cell a)
68 instance (FromPad a, Semigroup a) => Semigroup (Cell a) where
69 Cell (Span fx bx ex :| lx) x <> Cell (Span _fy by ey :| _ly) y =
70 Cell (Span fx bx ey :| lx) $
71 x<>fromPad (Pos lines columns)<>y
73 lines = pos_line by - pos_line ex
74 columns = pos_column by - pos_column (if lines <= 0 then ex else bx)
76 instance (FromPad a, Semigroup a, Monoid a) => Monoid (Cell a) where
82 cell0 = Cell (def :| [])
87 { span_file :: !FilePath
91 instance Default Span where
92 def = Span "" pos1 pos1
93 instance Show Span where
94 showsPrec _p Span{..} =
95 showString span_file .
96 showChar '#' . showsPrec 10 span_begin .
97 showChar '-' . showsPrec 10 span_end
100 type Location = NonEmpty Span
103 class FromPad a where
105 instance FromPad Text where
107 Text.replicate pos_line "\n" <>
108 Text.replicate pos_column " "
109 instance FromPad TL.Text where
111 TL.replicate (fromIntegral pos_line) "\n" <>
112 TL.replicate (fromIntegral pos_column) " "