module Data.Text.Region.Types (
Point(..), pointLine, pointColumn, pointRegion, Size, (.-.), (.+.),
Region(..), regionFrom, regionTo,
Map(..),
Contents, emptyContents,
concatCts, splitCts, splitted,
Editable(..), contents, by, measure,
Replace(..), replaceRegion, replaceWith, Edit(..), replaces,
Regioned(..),
module Data.Group
) where
import Prelude hiding (id, (.))
import Prelude.Unicode
import Control.Category
import Control.Lens hiding ((.=))
import Data.Aeson
import qualified Data.ByteString.Lazy.Char8 as L
import Data.Group
import Data.List
import Data.Text (Text)
import qualified Data.Text as T
data Point = Point {
_pointLine ∷ Int,
_pointColumn ∷ Int }
deriving (Eq, Ord, Read, Show)
makeLenses ''Point
pointRegion ∷ Iso' Point Region
pointRegion = iso (\p → Region p p) _regionFrom
instance ToJSON Point where
toJSON (Point l c) = object ["line" .= l, "column" .= c]
instance FromJSON Point where
parseJSON = withObject "point" $ \v → Point <$> v .: "line" <*> v .: "column"
instance Monoid Point where
mempty = Point 0 0
Point l c `mappend` Point bl bc
| l ≡ 0 = Point bl (c + bc)
| otherwise = Point (l + bl) c
instance Group Point where
invert (Point l c) = Point (negate l) (negate c)
type Size = Point
(.-.) ∷ Point → Point → Point
Point l c .-. Point bl bc
| bl < l = Point (l bl) c
| bl ≡ l = Point 0 (max 0 (c bc))
| otherwise = Point 0 0
(.+.) ∷ Point → Point → Point
(Point l c) .+. (Point bl bc)
| l ≡ 0 = Point bl (c + bc)
| otherwise = Point (l + bl) c
data Region = Region {
_regionFrom ∷ Point,
_regionTo ∷ Point }
deriving (Eq, Ord, Read, Show)
makeLenses ''Region
instance ToJSON Region where
toJSON (Region f t) = object ["from" .= f, "to" .= t]
instance FromJSON Region where
parseJSON = withObject "region" $ \v -> Region <$> v .: "from" <*> v .: "to"
newtype Map = Map { mapIso :: Iso' Region Region }
instance Monoid Map where
mempty = Map $ iso id id
Map l `mappend` Map r = Map (r . l)
instance Group Map where
invert (Map f) = Map (from f)
type Contents a = [a]
emptyContents ∷ Monoid a ⇒ Contents a
emptyContents = [mempty]
checkCts ∷ Contents a → Contents a
checkCts [] = error "Contents can't be empty"
checkCts cs = cs
concatCts ∷ Monoid a ⇒ Contents a → Contents a → Contents a
concatCts ls rs = init (checkCts ls) ++ [last (checkCts ls) `mappend` head (checkCts rs)] ++ tail (checkCts rs)
splitCts ∷ Editable a ⇒ Point → Contents a → (Contents a, Contents a)
splitCts (Point l c) cts = (take l cts ++ [p], s : drop (succ l) cts) where
(p, s) = splitContents c (cts !! l)
splitted ∷ Editable a ⇒ Point → Iso' (Contents a) (Contents a, Contents a)
splitted p = iso (splitCts p) (uncurry concatCts)
class Monoid a ⇒ Editable a where
splitContents ∷ Int → a → (a, a)
contentsLength ∷ a → Int
splitLines ∷ a → [a]
joinLines ∷ [a] → a
contents ∷ (Editable a, Editable b) ⇒ Iso a b (Contents a) (Contents b)
contents = iso splitLines joinLines
by ∷ Editable a ⇒ a → Contents a
by = splitLines
instance Editable String where
splitContents = splitAt
contentsLength = length
splitLines s = case break (≡ '\n') s of
(pre', "") → [pre']
(pre', _:post') → pre' : splitLines post'
joinLines = intercalate "\n"
instance Editable Text where
splitContents = T.splitAt
contentsLength = T.length
splitLines = T.split (≡ '\n')
joinLines = T.intercalate "\n"
measure ∷ Editable s ⇒ Contents s → Size
measure [] = error "Invalid input"
measure cts = Point (pred $ length cts) (contentsLength $ last cts)
data Replace s = Replace {
_replaceRegion ∷ Region,
_replaceWith ∷ Contents s }
deriving (Eq)
makeLenses ''Replace
instance (Editable s, ToJSON s) ⇒ ToJSON (Replace s) where
toJSON (Replace e c) = object ["region" .= e, "contents" .= view (from contents) c]
instance (Editable s, FromJSON s) ⇒ FromJSON (Replace s) where
parseJSON = withObject "edit" $ \v → Replace <$> v .: "region" <*> (view contents <$> v .: "contents")
instance (Editable s, ToJSON s) ⇒ Show (Replace s) where
show = L.unpack ∘ encode
newtype Edit s = Edit {
_replaces ∷ [Replace s] }
deriving (Eq, Show, Monoid)
makeLenses ''Edit
instance (Editable s, ToJSON s) ⇒ ToJSON (Edit s) where
toJSON = toJSON ∘ _replaces
instance (Editable s, FromJSON s) ⇒ FromJSON (Edit s) where
parseJSON = fmap Edit ∘ parseJSON
class Regioned a where
regions ∷ Traversal' a Region
instance Regioned Point where
regions = pointRegion
instance Regioned Region where
regions = id
instance Regioned (Replace s) where
regions = replaceRegion
instance Regioned (Edit s) where
regions = replaces . each . replaceRegion