{-# LANGUAGE DataKinds                  #-}
{-# LANGUAGE StandaloneDeriving         #-}
{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE TypeFamilies               #-}
{-# LANGUAGE TypeInType                 #-}
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
module Language.LSP.Types.LspId where

import qualified Data.Aeson                                 as A
import           Data.Hashable
import           Data.Int (Int32)
import           Data.IxMap
import           Data.Text                                  (Text)

import           Language.LSP.Types.Method

-- | Id used for a request, Can be either a String or an Int
data LspId (m :: Method f Request) = IdInt !Int32 | IdString !Text
  deriving (Int -> LspId m -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (f :: From) (m :: Method f 'Request).
Int -> LspId m -> ShowS
forall (f :: From) (m :: Method f 'Request). [LspId m] -> ShowS
forall (f :: From) (m :: Method f 'Request). LspId m -> String
showList :: [LspId m] -> ShowS
$cshowList :: forall (f :: From) (m :: Method f 'Request). [LspId m] -> ShowS
show :: LspId m -> String
$cshow :: forall (f :: From) (m :: Method f 'Request). LspId m -> String
showsPrec :: Int -> LspId m -> ShowS
$cshowsPrec :: forall (f :: From) (m :: Method f 'Request).
Int -> LspId m -> ShowS
Show,ReadPrec [LspId m]
ReadPrec (LspId m)
ReadS [LspId m]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall (f :: From) (m :: Method f 'Request). ReadPrec [LspId m]
forall (f :: From) (m :: Method f 'Request). ReadPrec (LspId m)
forall (f :: From) (m :: Method f 'Request). Int -> ReadS (LspId m)
forall (f :: From) (m :: Method f 'Request). ReadS [LspId m]
readListPrec :: ReadPrec [LspId m]
$creadListPrec :: forall (f :: From) (m :: Method f 'Request). ReadPrec [LspId m]
readPrec :: ReadPrec (LspId m)
$creadPrec :: forall (f :: From) (m :: Method f 'Request). ReadPrec (LspId m)
readList :: ReadS [LspId m]
$creadList :: forall (f :: From) (m :: Method f 'Request). ReadS [LspId m]
readsPrec :: Int -> ReadS (LspId m)
$creadsPrec :: forall (f :: From) (m :: Method f 'Request). Int -> ReadS (LspId m)
Read,LspId m -> LspId m -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> Bool
/= :: LspId m -> LspId m -> Bool
$c/= :: forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> Bool
== :: LspId m -> LspId m -> Bool
$c== :: forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> Bool
Eq,LspId m -> LspId m -> Bool
LspId m -> LspId m -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (f :: From) (m :: Method f 'Request). Eq (LspId m)
forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> Bool
forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> Ordering
forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> LspId m
min :: LspId m -> LspId m -> LspId m
$cmin :: forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> LspId m
max :: LspId m -> LspId m -> LspId m
$cmax :: forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> LspId m
>= :: LspId m -> LspId m -> Bool
$c>= :: forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> Bool
> :: LspId m -> LspId m -> Bool
$c> :: forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> Bool
<= :: LspId m -> LspId m -> Bool
$c<= :: forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> Bool
< :: LspId m -> LspId m -> Bool
$c< :: forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> Bool
compare :: LspId m -> LspId m -> Ordering
$ccompare :: forall (f :: From) (m :: Method f 'Request).
LspId m -> LspId m -> Ordering
Ord)

instance A.ToJSON (LspId m) where
  toJSON :: LspId m -> Value
toJSON (IdInt Int32
i)    = forall a. ToJSON a => a -> Value
A.toJSON Int32
i
  toJSON (IdString Text
s) = forall a. ToJSON a => a -> Value
A.toJSON Text
s

instance A.FromJSON (LspId m) where
  parseJSON :: Value -> Parser (LspId m)
parseJSON v :: Value
v@(A.Number Scientific
_) = forall (f :: From) (m :: Method f 'Request). Int32 -> LspId m
IdInt forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromJSON a => Value -> Parser a
A.parseJSON Value
v
  parseJSON  (A.String  Text
s) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall (f :: From) (m :: Method f 'Request). Text -> LspId m
IdString Text
s)
  parseJSON Value
_              = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"LspId"

instance IxOrd LspId where
  type Base LspId = SomeLspId
  toBase :: forall (a :: Method f 'Request). LspId a -> Base LspId
toBase = forall {f :: From} (m :: Method f 'Request). LspId m -> SomeLspId
SomeLspId

instance Hashable (LspId m) where
  hashWithSalt :: Int -> LspId m -> Int
hashWithSalt Int
n (IdInt Int32
i) = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
n Int32
i
  hashWithSalt Int
n (IdString Text
t) = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
n Text
t

data SomeLspId where
  SomeLspId :: !(LspId m) -> SomeLspId

deriving instance Show SomeLspId
instance Eq SomeLspId where
  SomeLspId (IdInt Int32
a) == :: SomeLspId -> SomeLspId -> Bool
== SomeLspId (IdInt Int32
b) = Int32
a forall a. Eq a => a -> a -> Bool
== Int32
b
  SomeLspId (IdString Text
a) == SomeLspId (IdString Text
b) = Text
a forall a. Eq a => a -> a -> Bool
== Text
b
  SomeLspId
_ == SomeLspId
_ = Bool
False
instance Ord SomeLspId where
  compare :: SomeLspId -> SomeLspId -> Ordering
compare (SomeLspId LspId m
x) (SomeLspId LspId m
y) = forall {f :: From} {f :: From} {m :: Method f 'Request}
       {m :: Method f 'Request}.
LspId m -> LspId m -> Ordering
go LspId m
x LspId m
y
    where
      go :: LspId m -> LspId m -> Ordering
go (IdInt    Int32
a) (IdInt    Int32
b) = Int32
a forall a. Ord a => a -> a -> Ordering
`compare` Int32
b
      go (IdString Text
a) (IdString Text
b) = Text
a forall a. Ord a => a -> a -> Ordering
`compare` Text
b
      go (IdInt    Int32
_) (IdString Text
_) = Ordering
LT
      go (IdString Text
_) (IdInt    Int32
_) = Ordering
GT

instance Hashable SomeLspId where
  hashWithSalt :: Int -> SomeLspId -> Int
hashWithSalt Int
n (SomeLspId LspId m
lspId) = forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
n LspId m
lspId