1 {-# LANGUAGE MultiParamTypeClasses #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
3 {-# LANGUAGE RankNTypes #-}
4 {-# LANGUAGE OverloadedStrings #-}
5 module Language.Haskell.LSP.Test.Parsing where
7 import Control.Applicative
8 import Control.Concurrent
10 import Control.Monad.IO.Class
13 import qualified Data.ByteString.Lazy.Char8 as B
14 import Data.Conduit.Parser
16 import qualified Data.Text as T
18 import Language.Haskell.LSP.Messages
19 import Language.Haskell.LSP.Types as LSP hiding (error)
20 import Language.Haskell.LSP.Test.Messages
21 import Language.Haskell.LSP.Test.Session
22 import System.Console.ANSI
24 satisfy :: (FromServerMessage -> Bool) -> Session FromServerMessage
27 skipTimeout <- overridingTimeout <$> get
28 timeoutId <- curTimeoutId <$> get
29 unless skipTimeout $ do
30 chan <- asks messageChan
31 timeout <- asks (messageTimeout . config)
32 void $ liftIO $ forkIO $ do
33 threadDelay (timeout * 1000000)
34 writeChan chan (TimeoutMessage timeoutId)
39 modify $ \s -> s { curTimeoutId = timeoutId + 1 }
41 modify $ \s -> s { lastReceivedMessage = Just x }
46 setSGR [SetColor Foreground Vivid Magenta]
47 putStrLn $ "<-- " ++ B.unpack (encodeMsg x)
52 -- | Matches a message of type 'a'.
53 message :: forall a. (Typeable a, FromJSON a) => Session a
55 let parser = decode . encodeMsg :: FromServerMessage -> Maybe a
56 in named (T.pack $ show $ head $ snd $ splitTyConApp $ last $ typeRepArgs $ typeOf parser) $ do
57 x <- satisfy (isJust . parser)
60 -- | Matches if the message is a notification.
61 anyNotification :: Session FromServerMessage
62 anyNotification = named "Any notification" $ satisfy isServerNotification
64 -- | Matches if the message is a request.
65 anyRequest :: Session FromServerMessage
66 anyRequest = named "Any request" $ satisfy isServerRequest
68 -- | Matches if the message is a response.
69 anyResponse :: Session FromServerMessage
70 anyResponse = named "Any response" $ satisfy isServerResponse
72 responseForId :: forall a. FromJSON a => LspId -> Session (ResponseMessage a)
73 responseForId lid = named (T.pack $ "Response for id: " ++ show lid) $ do
74 let parser = decode . encodeMsg :: FromServerMessage -> Maybe (ResponseMessage a)
75 x <- satisfy (maybe False (\z -> z ^. LSP.id == responseId lid) . parser)
78 anyMessage :: Session FromServerMessage
79 anyMessage = satisfy (const True)
81 -- | A stupid method for getting out the inner message.
82 castMsg :: FromJSON a => FromServerMessage -> a
83 castMsg = fromMaybe (error "Failed casting a message") . decode . encodeMsg
85 -- | A version of encode that encodes FromServerMessages as if they
87 encodeMsg :: FromServerMessage -> B.ByteString
88 encodeMsg = encode . genericToJSON (defaultOptions { sumEncoding = UntaggedValue })
90 -- | Matches if the message is a log message notification or a show message notification/request.
91 loggingNotification :: Session FromServerMessage
92 loggingNotification = named "Logging notification" $ satisfy shouldSkip
94 shouldSkip (NotLogMessage _) = True
95 shouldSkip (NotShowMessage _) = True
96 shouldSkip (ReqShowMessage _) = True
99 publishDiagnosticsNotification :: Session PublishDiagnosticsNotification
100 publishDiagnosticsNotification = named "Publish diagnostics notification" $ do
101 NotPublishDiagnostics diags <- satisfy test
103 where test (NotPublishDiagnostics _) = True