+import Language.Haskell.LSP.Test.Session
+
+-- $receiving
+-- To receive a message, just specify the type that expect:
+--
+-- @
+-- msg1 <- message :: Session ApplyWorkspaceEditRequest
+-- msg2 <- message :: Session HoverResponse
+-- @
+--
+-- 'Language.Haskell.LSP.Test.Session' is actually just a parser
+-- that operates on messages under the hood. This means that you
+-- can create and combine parsers to match speicifc sequences of
+-- messages that you expect.
+--
+-- For example, if you wanted to match either a definition or
+-- references request:
+--
+-- > defOrImpl = (message :: Session DefinitionRequest)
+-- > <|> (message :: Session ReferencesRequest)
+--
+-- If you wanted to match any number of telemetry
+-- notifications immediately followed by a response:
+--
+-- @
+-- logThenDiags =
+-- skipManyTill (message :: Session TelemetryNotification)
+-- anyResponse
+-- @
+
+-- | Consumes and returns the next message, if it satisfies the specified predicate.
+--
+-- @since 0.5.2.0
+satisfy :: (FromServerMessage -> Bool) -> Session FromServerMessage
+satisfy pred = satisfyMaybe (\msg -> if pred msg then Just msg else Nothing)
+
+-- | Consumes and returns the result of the specified predicate if it returns `Just`.
+--
+-- @since 0.6.1.0
+satisfyMaybe :: (FromServerMessage -> Maybe a) -> Session a
+satisfyMaybe pred = do
+
+ skipTimeout <- overridingTimeout <$> get
+ timeoutId <- getCurTimeoutId
+ unless skipTimeout $ do
+ chan <- asks messageChan
+ timeout <- asks (messageTimeout . config)
+ void $ liftIO $ forkIO $ do
+ threadDelay (timeout * 1000000)
+ writeChan chan (TimeoutMessage timeoutId)
+
+ x <- Session await
+
+ unless skipTimeout (bumpTimeoutId timeoutId)
+
+ modify $ \s -> s { lastReceivedMessage = Just x }
+
+ case pred x of
+ Just a -> do
+ logMsg LogServer x
+ return a
+ Nothing -> empty