Skip to content
This repository was archived by the owner on Oct 7, 2020. It is now read-only.

PluginDescriptor no longer serialisable. #93

Merged
merged 2 commits into from
Nov 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions hie-plugin-api/Haskell/Ide/Engine/PluginDescriptor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ instance Show PluginDescriptor where
showString " " .
showList used

-- | Ideally a Command is defined in such a way that it can be exposed via the
-- native CLI for the tool being exposed as well. Perhaps use
-- Options.Applicative for this in some way.
-- | Ideally a Command is defined in such a way that its CommandDescriptor
-- can be exposed via the native CLI for the tool being exposed as well.
-- Perhaps use Options.Applicative for this in some way.
data Command = forall a .(ValidResponse a) => Command
{ cmdDesc :: !CommandDescriptor
, cmdFunc :: !(CommandFunc a)
Expand All @@ -85,6 +85,11 @@ type PluginName = T.Text
data ExtendedCommandDescriptor =
ExtendedCommandDescriptor CommandDescriptor
PluginName
deriving (Show, Eq)

-- | Subset type extracted from 'Plugins' to be sent to the IDE as
-- a description of the available commands
type IdePlugins = Map.Map PluginId [CommandDescriptor]


-- | Define what context will be accepted from the frontend for the specific
Expand Down Expand Up @@ -346,11 +351,10 @@ instance ValidResponse CommandDescriptor where
<*> v .: "contexts"
<*> v .: "additional_params"

instance ValidResponse Plugins where
instance ValidResponse IdePlugins where
jsWrite m = H.fromList ["plugins" .= H.fromList
( map (\(k,v)-> k .= toJSON v)
$ Map.assocs m)]

jsRead v = do
ps <- v .: "plugins"
liftM Map.fromList $ mapM (\(k,vp) -> do
Expand Down Expand Up @@ -458,21 +462,6 @@ instance FromJSON Service where

-- -------------------------------------

instance ToJSON PluginDescriptor where
toJSON pluginDescriptor = object [ "commands" .= map cmdDesc (pdCommands pluginDescriptor)
, "exposed_services" .= pdExposedServices pluginDescriptor
, "used_services" .= pdUsedServices pluginDescriptor
]

instance FromJSON PluginDescriptor where
parseJSON (Object v) =
PluginDescriptor <$> fmap (fmap (\desc -> Command desc (CmdAsync (\_ _ _ -> return ())::CommandFunc T.Text))) (v .: "commands")
<*> v .: "exposed_services"
<*> v .: "used_services"
parseJSON _ = empty

-- -------------------------------------

instance ToJSON IdeRequest where
toJSON (IdeRequest{ideCommand = command, ideParams = params}) =
object [ "command" .= command
Expand Down
6 changes: 4 additions & 2 deletions src/Haskell/Ide/Engine/BasePlugin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ baseDescriptor = PluginDescriptor
versionCmd :: CommandFunc String
versionCmd = CmdSync $ \_ _ -> return (IdeResponseOk version)

pluginsCmd :: CommandFunc Plugins
pluginsCmd :: CommandFunc IdePlugins
pluginsCmd = CmdSync $ \_ _ -> do
plugins <- getPlugins
return (IdeResponseOk plugins)
let commands = Map.fromList $ map getOne $ Map.toList plugins
getOne (pid,pd) = (pid,map (\c -> cmdDesc c) $ pdCommands pd)
return (IdeResponseOk commands)

commandsCmd :: CommandFunc [CommandName]
commandsCmd = CmdSync $ \_ req -> do
Expand Down
5 changes: 4 additions & 1 deletion src/Haskell/Ide/Engine/Console.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ module Haskell.Ide.Engine.Console where
import Control.Concurrent.STM.TChan
import Control.Monad.IO.Class
import Control.Monad.STM
import Data.Aeson
import Data.Attoparsec.Text
import qualified Data.ByteString.Lazy.Char8 as C8
import qualified Data.Map as Map
import Data.Monoid
import qualified Data.Text as T
Expand Down Expand Up @@ -54,7 +56,8 @@ consoleListener plugins cin = do
Right (plugin,reqVal) -> do
liftIO $ atomically $ writeTChan cin (CReq plugin cid reqVal cout)
rsp <- liftIO $ atomically $ readTChan cout
outputStrLn $ show (coutResp rsp)
-- outputStrLn $ show (coutResp rsp)
outputStrLn $ C8.unpack $ encode (coutResp rsp)
loop env (cid + 1)

runInputT defaultSettings (startLoop emptyEnv 1)
Expand Down
10 changes: 8 additions & 2 deletions test/JsonSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ jsonSpec = do
prop "()" (propertyValidRoundtrip :: () -> Bool)
prop "Aeson.Object" (propertyValidRoundtrip :: Object -> Bool)
prop "CommandDescriptor" (propertyValidRoundtrip :: CommandDescriptor -> Bool)
prop "Plugins" (propertyValidRoundtrip :: Plugins -> Bool)
prop "ExtendedCommandDescriptor" (propertyValidRoundtrip :: ExtendedCommandDescriptor -> Bool)
prop "IdePlugins" (propertyValidRoundtrip :: IdePlugins -> Bool)
prop "TypeInfo" (propertyValidRoundtrip :: TypeInfo -> Bool)

describe "General JSON instances round trip" $ do
prop "ParamValP" (propertyJsonRoundtrip :: ParamValP -> Bool)
prop "CabalSection" (propertyJsonRoundtrip :: CabalSection -> Bool)
Expand All @@ -51,7 +53,6 @@ jsonSpec = do
prop "ParamDescription" (propertyJsonRoundtrip :: ParamDescription -> Bool)
prop "CommandDescriptor" (propertyJsonRoundtrip :: CommandDescriptor -> Bool)
prop "Service" (propertyJsonRoundtrip :: Service -> Bool)
prop "PluginDescriptor" (propertyJsonRoundtrip :: PluginDescriptor -> Bool)
prop "IdeRequest" (propertyJsonRoundtrip :: IdeRequest -> Bool)
prop "IdeErrorCode" (propertyJsonRoundtrip :: IdeErrorCode -> Bool)
prop "IdeError" (propertyJsonRoundtrip :: IdeError -> Bool)
Expand All @@ -78,6 +79,11 @@ instance Arbitrary CommandDescriptor where
<*> smallList arbitraryBoundedEnum
<*> smallList arbitrary

instance Arbitrary ExtendedCommandDescriptor where
arbitrary = ExtendedCommandDescriptor
<$> arbitrary
<*> arbitrary

instance Arbitrary ParamDescription where
arbitrary = do
i <- choose (1::Int,2)
Expand Down