+failSession :: String -> Session ()
+failSession reason = do
+ lift $ putStrLn reason
+ passVar <- ask
+ lift $ putMVar passVar False
+
+passSession :: Session ()
+passSession = do
+ passVar <- ask
+ lift $ putMVar passVar True
+
+-- | Listens to the server output, makes sure it matches the record and
+-- signals any semaphores
+listenServer :: [B.ByteString] -> Handle -> (MVar LSP.LspIdRsp, MVar LSP.LspId) -> Session ()
+listenServer [] _ _ = passSession
+listenServer expectedMsgs h semas@(reqSema, rspSema) = do
+ msg <- lift $ getNextMessage h
+
+ newExpectedMsgs <- case decode msg of
+ Just m -> request m
+ Nothing -> case decode msg of
+ Just m -> notification m
+ Nothing -> case decode msg of
+ Just m -> response m
+ Nothing -> failSession "Malformed message" >> return expectedMsgs
+
+ listenServer newExpectedMsgs h semas
+
+
+ where response :: LSP.ResponseMessage Value -> Session [B.ByteString]
+ response res = do
+ lift $ putStrLn $ "Got response for id " ++ show (res ^. LSP.id)
+
+ lift $ print res
+
+ checkOrder res
+
+ lift $ putMVar reqSema (res ^. LSP.id) -- unblock the handler waiting to send a request
+
+ markReceived res
+
+ request :: LSP.RequestMessage LSP.ServerMethod Value Value -> Session [B.ByteString]
+ request req = do
+ lift $ putStrLn $ "Got request for id " ++ show (req ^. LSP.id) ++ " " ++ show (req ^. LSP.method)