{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MagicHash #-}
module Language.LSP.Protocol.Utils.SMethodMap (
SMethodMap,
singleton,
insert,
delete,
member,
lookup,
map,
) where
import Data.IntMap (IntMap)
import Data.IntMap.Strict qualified as IntMap
import Data.Kind (Type)
import Data.Map (Map)
import Data.Map.Strict qualified as Map
import GHC.Exts (Any, Int (..), dataToTag#)
import Unsafe.Coerce (unsafeCoerce)
import Prelude hiding (lookup, map)
import GHC.TypeLits (symbolVal)
import Language.LSP.Protocol.Message (Method (..), SMethod (..))
data SMethodMap (v :: Method f t -> Type)
=
SMethodMap !(IntMap (v Any)) !(Map String (v Any))
toIx :: SMethod a -> Int
toIx :: forall {f :: MessageDirection} {t :: MessageKind}
(a :: Method f t).
SMethod a -> Int
toIx SMethod a
k = Int# -> Int
I# (SMethod a -> Int#
forall a. a -> Int#
dataToTag# SMethod a
k)
singleton :: SMethod a -> v a -> SMethodMap v
singleton :: forall {f :: MessageDirection} {t :: MessageKind} (a :: Method f t)
(v :: Method f t -> *).
SMethod a -> v a -> SMethodMap v
singleton (SMethod_CustomMethod Proxy s
t) v a
v = IntMap (v Any) -> Map String (v Any) -> SMethodMap v
forall (f :: MessageDirection) (t :: MessageKind)
(v :: Method f t -> *).
IntMap (v Any) -> Map String (v Any) -> SMethodMap v
SMethodMap IntMap (v Any)
forall a. Monoid a => a
mempty (String -> v Any -> Map String (v Any)
forall k a. k -> a -> Map k a
Map.singleton (Proxy s -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal Proxy s
t) (v a -> v Any
forall a b. a -> b
unsafeCoerce v a
v))
singleton SMethod a
k v a
v = IntMap (v Any) -> Map String (v Any) -> SMethodMap v
forall (f :: MessageDirection) (t :: MessageKind)
(v :: Method f t -> *).
IntMap (v Any) -> Map String (v Any) -> SMethodMap v
SMethodMap (Int -> v Any -> IntMap (v Any)
forall a. Int -> a -> IntMap a
IntMap.singleton (SMethod a -> Int
forall {f :: MessageDirection} {t :: MessageKind}
(a :: Method f t).
SMethod a -> Int
toIx SMethod a
k) (v a -> v Any
forall a b. a -> b
unsafeCoerce v a
v)) Map String (v Any)
forall a. Monoid a => a
mempty
insert :: SMethod a -> v a -> SMethodMap v -> SMethodMap v
insert :: forall {f :: MessageDirection} {t :: MessageKind} (a :: Method f t)
(v :: Method f t -> *).
SMethod a -> v a -> SMethodMap v -> SMethodMap v
insert (SMethod_CustomMethod Proxy s
t) v a
v (SMethodMap IntMap (v Any)
xs Map String (v Any)
ys) = IntMap (v Any) -> Map String (v Any) -> SMethodMap v
forall (f :: MessageDirection) (t :: MessageKind)
(v :: Method f t -> *).
IntMap (v Any) -> Map String (v Any) -> SMethodMap v
SMethodMap IntMap (v Any)
xs (String -> v Any -> Map String (v Any) -> Map String (v Any)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert (Proxy s -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal Proxy s
t) (v a -> v Any
forall a b. a -> b
unsafeCoerce v a
v) Map String (v Any)
ys)
insert SMethod a
k v a
v (SMethodMap IntMap (v Any)
xs Map String (v Any)
ys) = IntMap (v Any) -> Map String (v Any) -> SMethodMap v
forall (f :: MessageDirection) (t :: MessageKind)
(v :: Method f t -> *).
IntMap (v Any) -> Map String (v Any) -> SMethodMap v
SMethodMap (Int -> v Any -> IntMap (v Any) -> IntMap (v Any)
forall a. Int -> a -> IntMap a -> IntMap a
IntMap.insert (SMethod a -> Int
forall {f :: MessageDirection} {t :: MessageKind}
(a :: Method f t).
SMethod a -> Int
toIx SMethod a
k) (v a -> v Any
forall a b. a -> b
unsafeCoerce v a
v) IntMap (v Any)
xs) Map String (v Any)
ys
delete :: SMethod a -> SMethodMap v -> SMethodMap v
delete :: forall {f :: MessageDirection} {t :: MessageKind}
{f :: MessageDirection} {t :: MessageKind} (a :: Method f t)
(v :: Method f t -> *).
SMethod a -> SMethodMap v -> SMethodMap v
delete (SMethod_CustomMethod Proxy s
t) (SMethodMap IntMap (v Any)
xs Map String (v Any)
ys) = IntMap (v Any) -> Map String (v Any) -> SMethodMap v
forall (f :: MessageDirection) (t :: MessageKind)
(v :: Method f t -> *).
IntMap (v Any) -> Map String (v Any) -> SMethodMap v
SMethodMap IntMap (v Any)
xs (String -> Map String (v Any) -> Map String (v Any)
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete (Proxy s -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal Proxy s
t) Map String (v Any)
ys)
delete SMethod a
k (SMethodMap IntMap (v Any)
xs Map String (v Any)
ys) = IntMap (v Any) -> Map String (v Any) -> SMethodMap v
forall (f :: MessageDirection) (t :: MessageKind)
(v :: Method f t -> *).
IntMap (v Any) -> Map String (v Any) -> SMethodMap v
SMethodMap (Int -> IntMap (v Any) -> IntMap (v Any)
forall a. Int -> IntMap a -> IntMap a
IntMap.delete (SMethod a -> Int
forall {f :: MessageDirection} {t :: MessageKind}
(a :: Method f t).
SMethod a -> Int
toIx SMethod a
k) IntMap (v Any)
xs) Map String (v Any)
ys
member :: SMethod a -> SMethodMap v -> Bool
member :: forall {f :: MessageDirection} {t :: MessageKind}
{f :: MessageDirection} {t :: MessageKind} (a :: Method f t)
(v :: Method f t -> *).
SMethod a -> SMethodMap v -> Bool
member (SMethod_CustomMethod Proxy s
t) (SMethodMap IntMap (v Any)
_ Map String (v Any)
ys) = String -> Map String (v Any) -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member (Proxy s -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal Proxy s
t) Map String (v Any)
ys
member SMethod a
k (SMethodMap IntMap (v Any)
xs Map String (v Any)
_) = Int -> IntMap (v Any) -> Bool
forall a. Int -> IntMap a -> Bool
IntMap.member (SMethod a -> Int
forall {f :: MessageDirection} {t :: MessageKind}
(a :: Method f t).
SMethod a -> Int
toIx SMethod a
k) IntMap (v Any)
xs
lookup :: SMethod a -> SMethodMap v -> Maybe (v a)
lookup :: forall {f :: MessageDirection} {t :: MessageKind} (a :: Method f t)
(v :: Method f t -> *).
SMethod a -> SMethodMap v -> Maybe (v a)
lookup (SMethod_CustomMethod Proxy s
t) (SMethodMap IntMap (v Any)
_ Map String (v Any)
ys) = Maybe (v Any) -> Maybe (v a)
forall a b. a -> b
unsafeCoerce (String -> Map String (v Any) -> Maybe (v Any)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (Proxy s -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal Proxy s
t) Map String (v Any)
ys)
lookup SMethod a
k (SMethodMap IntMap (v Any)
xs Map String (v Any)
_) = Maybe (v Any) -> Maybe (v a)
forall a b. a -> b
unsafeCoerce (Int -> IntMap (v Any) -> Maybe (v Any)
forall a. Int -> IntMap a -> Maybe a
IntMap.lookup (SMethod a -> Int
forall {f :: MessageDirection} {t :: MessageKind}
(a :: Method f t).
SMethod a -> Int
toIx SMethod a
k) IntMap (v Any)
xs)
map :: (forall a. u a -> v a) -> SMethodMap u -> SMethodMap v
map :: forall {f :: MessageDirection} {t :: MessageKind}
(u :: Method f t -> *) (v :: Method f t -> *).
(forall (a :: Method f t). u a -> v a)
-> SMethodMap u -> SMethodMap v
map forall (a :: Method f t). u a -> v a
f (SMethodMap IntMap (u Any)
xs Map String (u Any)
ys) = IntMap (v Any) -> Map String (v Any) -> SMethodMap v
forall (f :: MessageDirection) (t :: MessageKind)
(v :: Method f t -> *).
IntMap (v Any) -> Map String (v Any) -> SMethodMap v
SMethodMap ((u Any -> v Any) -> IntMap (u Any) -> IntMap (v Any)
forall a b. (a -> b) -> IntMap a -> IntMap b
IntMap.map u Any -> v Any
forall (a :: Method f t). u a -> v a
f IntMap (u Any)
xs) ((u Any -> v Any) -> Map String (u Any) -> Map String (v Any)
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map u Any -> v Any
forall (a :: Method f t). u a -> v a
f Map String (u Any)
ys)
instance Semigroup (SMethodMap v) where
SMethodMap IntMap (v Any)
xs Map String (v Any)
ys <> :: SMethodMap v -> SMethodMap v -> SMethodMap v
<> SMethodMap IntMap (v Any)
xs' Map String (v Any)
ys' = IntMap (v Any) -> Map String (v Any) -> SMethodMap v
forall (f :: MessageDirection) (t :: MessageKind)
(v :: Method f t -> *).
IntMap (v Any) -> Map String (v Any) -> SMethodMap v
SMethodMap (IntMap (v Any)
xs IntMap (v Any) -> IntMap (v Any) -> IntMap (v Any)
forall a. Semigroup a => a -> a -> a
<> IntMap (v Any)
xs') (Map String (v Any)
ys Map String (v Any) -> Map String (v Any) -> Map String (v Any)
forall a. Semigroup a => a -> a -> a
<> Map String (v Any)
ys')
instance Monoid (SMethodMap v) where
mempty :: SMethodMap v
mempty = IntMap (v Any) -> Map String (v Any) -> SMethodMap v
forall (f :: MessageDirection) (t :: MessageKind)
(v :: Method f t -> *).
IntMap (v Any) -> Map String (v Any) -> SMethodMap v
SMethodMap IntMap (v Any)
forall a. Monoid a => a
mempty Map String (v Any)
forall a. Monoid a => a
mempty