1 {-# LANGUAGE MultiParamTypeClasses #-}
2 {-# LANGUAGE EmptyCase #-}
3 {-# LANGUAGE TypeOperators #-}
4 {-# LANGUAGE ScopedTypeVariables #-}
5 {-# LANGUAGE PolyKinds #-}
6 {-# LANGUAGE KindSignatures #-}
7 {-# LANGUAGE DataKinds #-}
9 {-# LANGUAGE LambdaCase #-}
10 {-# LANGUAGE RankNTypes #-}
11 {-# LANGUAGE OverloadedStrings #-}
13 module Language.Haskell.LSP.Test.Parsing
26 , publishDiagnosticsNotification
29 import Control.Applicative
30 import Control.Concurrent
32 import Control.Monad.IO.Class
34 import Data.Conduit.Parser hiding (named)
35 import qualified Data.Conduit.Parser (named)
36 import Data.GADT.Compare
37 import qualified Data.Text as T
39 import Language.Haskell.LSP.Types
40 import Language.Haskell.LSP.Test.Session
43 -- To receive a message, specify the method of the message to expect:
46 -- msg1 <- message SWorkspaceApplyEdit
47 -- msg2 <- message STextDocumentHover
50 -- 'Language.Haskell.LSP.Test.Session' is actually just a parser
51 -- that operates on messages under the hood. This means that you
52 -- can create and combine parsers to match speicifc sequences of
53 -- messages that you expect.
55 -- For example, if you wanted to match either a definition or
56 -- references request:
58 -- > defOrImpl = message STextDocumentDefinition
59 -- > <|> message STextDocumentReferences
61 -- If you wanted to match any number of telemetry
62 -- notifications immediately followed by a response:
66 -- skipManyTill (message STelemetryEvent)
70 -- | Consumes and returns the next message, if it satisfies the specified predicate.
73 satisfy :: (FromServerMessage -> Bool) -> Session FromServerMessage
74 satisfy pred = satisfyMaybe (\msg -> if pred msg then Just msg else Nothing)
76 -- | Consumes and returns the result of the specified predicate if it returns `Just`.
79 satisfyMaybe :: (FromServerMessage -> Maybe a) -> Session a
80 satisfyMaybe pred = satisfyMaybeM (pure . pred)
82 satisfyMaybeM :: (FromServerMessage -> Session (Maybe a)) -> Session a
83 satisfyMaybeM pred = do
85 skipTimeout <- overridingTimeout <$> get
86 timeoutId <- getCurTimeoutId
87 unless skipTimeout $ do
88 chan <- asks messageChan
89 timeout <- asks (messageTimeout . config)
90 void $ liftIO $ forkIO $ do
91 threadDelay (timeout * 1000000)
92 writeChan chan (TimeoutMessage timeoutId)
96 unless skipTimeout (bumpTimeoutId timeoutId)
98 modify $ \s -> s { lastReceivedMessage = Just x }
108 named :: T.Text -> Session a -> Session a
109 named s (Session x) = Session (Data.Conduit.Parser.named s x)
111 mEq :: SServerMethod m1 -> SServerMethod m2 -> Maybe (m1 :~~: m2)
112 mEq m1 m2 = case (splitServerMethod m1, splitServerMethod m2) of
113 (IsServerNot, IsServerNot) -> do
116 (IsServerReq, IsServerReq) -> do
121 mEqClient :: SClientMethod m1 -> SClientMethod m2 -> Maybe (m1 :~~: m2)
122 mEqClient m1 m2 = case (splitClientMethod m1, splitClientMethod m2) of
123 (IsClientNot, IsClientNot) -> do
126 (IsClientReq, IsClientReq) -> do
131 -- | Matches non-custom messages
132 message :: SServerMethod m -> Session (ServerMessage m)
133 message m1 = named (T.pack $ show m1) $ satisfyMaybe $ \case
134 FromServerMess m2 msg -> do
139 customRequest :: T.Text -> Session (ServerMessage (CustomMethod :: Method FromServer Request))
140 customRequest m = named m $ satisfyMaybe $ \case
141 FromServerMess m1 msg -> case splitServerMethod m1 of
142 IsServerEither -> case msg of
143 ReqMess _ | m1 == SCustomMethod m -> Just msg
148 customNotification :: T.Text -> Session (ServerMessage (CustomMethod :: Method FromServer Notification))
149 customNotification m = named m $ satisfyMaybe $ \case
150 FromServerMess m1 msg -> case splitServerMethod m1 of
151 IsServerEither -> case msg of
152 NotMess _ | m1 == SCustomMethod m -> Just msg
157 -- | Matches if the message is a notification.
158 anyNotification :: Session FromServerMessage
159 anyNotification = named "Any notification" $ satisfy $ \case
160 FromServerMess m msg -> case splitServerMethod m of
162 IsServerEither -> case msg of
166 FromServerRsp _ _ -> False
168 -- | Matches if the message is a request.
169 anyRequest :: Session FromServerMessage
170 anyRequest = named "Any request" $ satisfy $ \case
171 FromServerMess m _ -> case splitServerMethod m of
174 FromServerRsp _ _ -> False
176 -- | Matches if the message is a response.
177 anyResponse :: Session FromServerMessage
178 anyResponse = named "Any response" $ satisfy $ \case
179 FromServerMess _ _ -> False
180 FromServerRsp _ _ -> True
182 -- | Matches a response for a specific id.
183 responseForId :: SMethod (m :: Method FromClient Request) -> LspId m -> Session (ResponseMessage m)
184 responseForId m lid = named (T.pack $ "Response for id: " ++ show lid) $ do
185 satisfyMaybe $ \msg -> do
187 FromServerMess _ _ -> Nothing
188 FromServerRsp m' rspMsg@(ResponseMessage _ lid' _) ->
189 case mEqClient m m' of
191 guard (lid' == Just lid)
194 | SCustomMethod tm <- m
195 , SCustomMethod tm' <- m'
197 , lid' == Just lid -> pure rspMsg
200 -- | Matches any type of message.
201 anyMessage :: Session FromServerMessage
202 anyMessage = satisfy (const True)
204 -- | Matches if the message is a log message notification or a show message notification/request.
205 loggingNotification :: Session FromServerMessage
206 loggingNotification = named "Logging notification" $ satisfy shouldSkip
208 shouldSkip (FromServerMess SWindowLogMessage _) = True
209 shouldSkip (FromServerMess SWindowShowMessage _) = True
210 shouldSkip (FromServerMess SWindowShowMessageRequest _) = True
213 -- | Matches a 'Language.Haskell.LSP.Test.PublishDiagnosticsNotification'
214 -- (textDocument/publishDiagnostics) notification.
215 publishDiagnosticsNotification :: Session PublishDiagnosticsNotification
216 publishDiagnosticsNotification = named "Publish diagnostics notification" $
217 satisfyMaybe $ \msg -> case msg of
218 FromServerMess STextDocumentPublishDiagnostics diags -> Just diags