{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE RecordWildCards #-}
module Neovim.Plugin.IPC.Classes (
SomeMessage(..),
Message(..),
FunctionCall(..),
Request(..),
Notification(..),
module Data.Int,
module Data.Time,
) where
import Neovim.Plugin.Classes (FunctionName)
import Control.Concurrent.STM
import Data.Data (Typeable, cast)
import Data.Int (Int64)
import Data.MessagePack
import Data.Time
import Data.Time.Locale.Compat
import Text.PrettyPrint.ANSI.Leijen hiding ((<$>))
import Prelude
data SomeMessage = forall msg. Message msg => SomeMessage msg
class Typeable message => Message message where
fromMessage :: SomeMessage -> Maybe message
fromMessage (SomeMessage message) = cast message
data FunctionCall
= FunctionCall FunctionName [Object] (TMVar (Either Object Object)) UTCTime
deriving (Typeable)
instance Message FunctionCall
instance Pretty FunctionCall where
pretty (FunctionCall fname args _ t) =
nest 2 $ text "Function call for:" <+> pretty fname
<$$> text "Arguments:" <+> text (show args)
<$$> text "Timestamp:"
<+> (yellow . text . formatTime defaultTimeLocale "%H:%M:%S (%q)") t
data Request = Request
{ reqMethod :: FunctionName
, reqId :: !Int64
, reqArgs :: [Object]
} deriving (Eq, Ord, Show, Typeable)
instance Message Request
instance Pretty Request where
pretty Request{..} =
nest 2 $ text "Request" <+> (yellow . text . ('#':) . show) reqId
<$$> (text "Method:" <+> pretty reqMethod)
<$$> (text "Arguments:" <+> text (show reqArgs))
data Notification = Notification
{ notMethod :: FunctionName
, notArgs :: [Object]
} deriving (Eq, Ord, Show, Typeable)
instance Message Notification
instance Pretty Notification where
pretty Notification{..} =
nest 2 $ text "Notification"
<$$> (text "Method:" <+> pretty notMethod)
<$$> (text "Arguments:" <+> text (show notArgs))