module Rasa.Internal.Editor
(
Editor
, HasEditor(..)
, buffers
, exiting
, ext
, bufExt
, nextBufId
, BufRef(..)
) where
import Rasa.Internal.Buffer
import Rasa.Internal.Extensions
import Unsafe.Coerce
import Data.Dynamic
import Data.Default
import Data.Maybe
import Data.IntMap
import Control.Lens
newtype BufRef =
BufRef Int
deriving (Show, Eq, Ord)
data Editor = Editor
{ _buffers' :: IntMap Buffer
, _exiting' :: Bool
, _extState' :: ExtMap
, _nextBufId' :: Int
}
makeLenses ''Editor
instance Show Editor where
show ed =
"Buffers==============\n" ++ show (ed^.buffers) ++ "\n\n"
++ "Editor Extensions==============\n" ++ show (ed^.extState) ++ "\n\n"
++ "---\n\n"
class HasEditor a where
editor :: Lens' a Editor
buffers :: HasEditor e => Lens' e (IntMap Buffer)
buffers = editor.buffers'
exiting :: HasEditor e => Lens' e Bool
exiting = editor.exiting'
extState :: HasEditor e => Lens' e ExtMap
extState = editor.extState'
nextBufId :: HasEditor e => Lens' e Int
nextBufId = editor.nextBufId'
instance HasEditor Editor where
editor = lens id (flip const)
instance Default Editor where
def =
Editor
{ _extState'=def
, _buffers'=empty
, _exiting'=False
, _nextBufId'=0
}
ext
:: forall a e.
(Show a, Typeable a, Default a, HasEditor e)
=> Lens' e a
ext = lens getter setter
where
getter ed =
fromMaybe def $ ed ^. extState . at (typeRep (Proxy :: Proxy a)) .
mapping coerce
setter ed new =
set
(extState . at (typeRep (Proxy :: Proxy a)) . mapping coerce)
(Just new)
ed
coerce = iso (\(Ext x) -> unsafeCoerce x) Ext