12ef1a6281547c4fc73dd4ad812cb71c529e95f5
[lsp-test.git] / src / Language / Haskell / LSP / Test / Parsing.hs
1 {-# LANGUAGE MultiParamTypeClasses #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
3 {-# LANGUAGE RankNTypes #-}
4 {-# LANGUAGE OverloadedStrings #-}
5
6 module Language.Haskell.LSP.Test.Parsing
7   ( -- $receiving
8     satisfy
9   , satisfyMaybe
10   , message
11   , anyRequest
12   , anyResponse
13   , anyNotification
14   , anyMessage
15   , loggingNotification
16   , publishDiagnosticsNotification
17   , responseForId
18   ) where
19
20 import Control.Applicative
21 import Control.Concurrent
22 import Control.Lens
23 import Control.Monad.IO.Class
24 import Control.Monad
25 import Data.Aeson
26 import qualified Data.ByteString.Lazy.Char8 as B
27 import Data.Conduit.Parser hiding (named)
28 import qualified Data.Conduit.Parser (named)
29 import qualified Data.Text as T
30 import Data.Typeable
31 import Language.Haskell.LSP.Messages
32 import Language.Haskell.LSP.Types
33 import qualified Language.Haskell.LSP.Types.Lens as LSP
34 import Language.Haskell.LSP.Test.Messages
35 import Language.Haskell.LSP.Test.Session
36
37 -- $receiving
38 -- To receive a message, just specify the type that expect:
39 --
40 -- @
41 -- msg1 <- message :: Session ApplyWorkspaceEditRequest
42 -- msg2 <- message :: Session HoverResponse
43 -- @
44 --
45 -- 'Language.Haskell.LSP.Test.Session' is actually just a parser
46 -- that operates on messages under the hood. This means that you
47 -- can create and combine parsers to match speicifc sequences of
48 -- messages that you expect.
49 --
50 -- For example, if you wanted to match either a definition or
51 -- references request:
52 --
53 -- > defOrImpl = (message :: Session DefinitionRequest)
54 -- >          <|> (message :: Session ReferencesRequest)
55 --
56 -- If you wanted to match any number of telemetry
57 -- notifications immediately followed by a response:
58 --
59 -- @
60 -- logThenDiags =
61 --  skipManyTill (message :: Session TelemetryNotification)
62 --               anyResponse
63 -- @
64
65 -- | Consumes and returns the next message, if it satisfies the specified predicate.
66 --
67 -- @since 0.5.2.0
68 satisfy :: (FromServerMessage -> Bool) -> Session FromServerMessage
69 satisfy pred = satisfyMaybe (\msg -> if pred msg then Just msg else Nothing)
70
71 -- | Consumes and returns the result of the specified predicate if it returns `Just`.
72 --
73 -- @since 0.6.1.0
74 satisfyMaybe :: (FromServerMessage -> Maybe a) -> Session a
75 satisfyMaybe pred = do
76
77   skipTimeout <- overridingTimeout <$> get
78   timeoutId <- getCurTimeoutId
79   unless skipTimeout $ do
80     chan <- asks messageChan
81     timeout <- asks (messageTimeout . config)
82     void $ liftIO $ forkIO $ do
83       threadDelay (timeout * 1000000)
84       writeChan chan (TimeoutMessage timeoutId)
85
86   x <- Session await
87
88   unless skipTimeout (bumpTimeoutId timeoutId)
89
90   modify $ \s -> s { lastReceivedMessage = Just x }
91
92   case pred x of
93     Just a -> do
94       logMsg LogServer x
95       return a
96     Nothing -> empty
97
98 named :: T.Text -> Session a -> Session a
99 named s (Session x) = Session (Data.Conduit.Parser.named s x)
100
101 -- | Matches a message of type @a@.
102 message :: forall a. (Typeable a, FromJSON a) => Session a
103 message =
104   let parser = decode . encodeMsg :: FromServerMessage -> Maybe a
105   in named (T.pack $ show $ head $ snd $ splitTyConApp $ last $ typeRepArgs $ typeOf parser) $
106      satisfyMaybe parser
107
108 -- | Matches if the message is a notification.
109 anyNotification :: Session FromServerMessage
110 anyNotification = named "Any notification" $ satisfy isServerNotification
111
112 -- | Matches if the message is a request.
113 anyRequest :: Session FromServerMessage
114 anyRequest = named "Any request" $ satisfy isServerRequest
115
116 -- | Matches if the message is a response.
117 anyResponse :: Session FromServerMessage
118 anyResponse = named "Any response" $ satisfy isServerResponse
119
120 -- | Matches a response for a specific id.
121 responseForId :: forall a. FromJSON a => LspId -> Session (ResponseMessage a)
122 responseForId lid = named (T.pack $ "Response for id: " ++ show lid) $ do
123   let parser = decode . encodeMsg :: FromServerMessage -> Maybe (ResponseMessage a)
124   satisfyMaybe $ \msg -> do
125     z <- parser msg
126     guard (z ^. LSP.id == responseId lid)
127     pure z
128
129 -- | Matches any type of message.
130 anyMessage :: Session FromServerMessage
131 anyMessage = satisfy (const True)
132
133 -- | A version of encode that encodes FromServerMessages as if they
134 -- weren't wrapped.
135 encodeMsg :: FromServerMessage -> B.ByteString
136 encodeMsg = encode . genericToJSON (defaultOptions { sumEncoding = UntaggedValue })
137
138 -- | Matches if the message is a log message notification or a show message notification/request.
139 loggingNotification :: Session FromServerMessage
140 loggingNotification = named "Logging notification" $ satisfy shouldSkip
141   where
142     shouldSkip (NotLogMessage _) = True
143     shouldSkip (NotShowMessage _) = True
144     shouldSkip (ReqShowMessage _) = True
145     shouldSkip _ = False
146
147 -- | Matches a 'Language.Haskell.LSP.Test.PublishDiagnosticsNotification'
148 -- (textDocument/publishDiagnostics) notification.
149 publishDiagnosticsNotification :: Session PublishDiagnosticsNotification
150 publishDiagnosticsNotification = named "Publish diagnostics notification" $
151   satisfyMaybe $ \msg -> case msg of
152     NotPublishDiagnostics diags -> Just diags
153     _ -> Nothing