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) $
57 castMsg <$> satisfy (isJust . parser)
59 -- | Matches if the message is a notification.
60 anyNotification :: Session FromServerMessage
61 anyNotification = named "Any notification" $ satisfy isServerNotification
63 -- | Matches if the message is a request.
64 anyRequest :: Session FromServerMessage
65 anyRequest = named "Any request" $ satisfy isServerRequest
67 -- | Matches if the message is a response.
68 anyResponse :: Session FromServerMessage
69 anyResponse = named "Any response" $ satisfy isServerResponse
71 responseForId :: forall a. FromJSON a => LspId -> Session (ResponseMessage a)
72 responseForId lid = named (T.pack $ "Response for id: " ++ show lid) $ do
73 let parser = decode . encodeMsg :: FromServerMessage -> Maybe (ResponseMessage a)
74 x <- satisfy (maybe False (\z -> z ^. LSP.id == responseId lid) . parser)
77 anyMessage :: Session FromServerMessage
78 anyMessage = satisfy (const True)
80 -- | A stupid method for getting out the inner message.
81 castMsg :: FromJSON a => FromServerMessage -> a
82 castMsg = fromMaybe (error "Failed casting a message") . decode . encodeMsg
84 -- | A version of encode that encodes FromServerMessages as if they
86 encodeMsg :: FromServerMessage -> B.ByteString
87 encodeMsg = encode . genericToJSON (defaultOptions { sumEncoding = UntaggedValue })
89 -- | Matches if the message is a log message notification or a show message notification/request.
90 loggingNotification :: Session FromServerMessage
91 loggingNotification = named "Logging notification" $ satisfy shouldSkip
93 shouldSkip (NotLogMessage _) = True
94 shouldSkip (NotShowMessage _) = True
95 shouldSkip (ReqShowMessage _) = True
98 publishDiagnosticsNotification :: Session PublishDiagnosticsNotification
99 publishDiagnosticsNotification = named "Publish diagnostics notification" $ do
100 NotPublishDiagnostics diags <- satisfy test
102 where test (NotPublishDiagnostics _) = True