From: Luke Lau Date: Mon, 11 Jun 2018 14:31:29 +0000 (-0400) Subject: Merge branch 'master' of https://github.com/Bubba/haskell-lsp-test X-Git-Tag: 0.1.0.0~76 X-Git-Url: https://git.lukelau.me/?a=commitdiff_plain;h=bc52b000bf018360efbfa0fcd289329c70d2c77e;hp=4eb97ef8a3d4b3908fa9b5bbbd5ae77cfa95cecc;p=lsp-test.git Merge branch 'master' of https://github.com/Bubba/haskell-lsp-test --- diff --git a/example/Main.hs b/example/Main.hs index 0c8ae9f..fc453db 100644 --- a/example/Main.hs +++ b/example/Main.hs @@ -11,8 +11,8 @@ main = runSession "hie" "test/recordings/renamePass" $ do sendNotification TextDocumentDidOpen (DidOpenTextDocumentParams docItem) - sendRequest (Proxy :: Proxy DocumentSymbolRequest) TextDocumentDocumentSymbol (DocumentSymbolParams docId) + sendRequest TextDocumentDocumentSymbol (DocumentSymbolParams docId) skipMany loggingNotification - response >>= liftIO . print + anyResponse >>= liftIO . print diff --git a/src/Language/Haskell/LSP/Test.hs b/src/Language/Haskell/LSP/Test.hs index 087143f..79b7b1e 100644 --- a/src/Language/Haskell/LSP/Test.hs +++ b/src/Language/Haskell/LSP/Test.hs @@ -24,8 +24,11 @@ module Language.Haskell.LSP.Test , sendNotification' , sendResponse -- * Receving + , anyRequest , request + , anyResponse , response + , anyNotification , notification , loggingNotification , publishDiagnosticsNotification @@ -61,17 +64,15 @@ import Control.Applicative.Combinators import Control.Monad import Control.Monad.IO.Class import Control.Concurrent -import Control.Lens +import Control.Lens hiding ((.=)) import qualified Data.Text as T import qualified Data.Text.IO as T import Data.Aeson import qualified Data.ByteString.Lazy.Char8 as B import Data.Default -import Data.Proxy import System.Process import Language.Haskell.LSP.Types -import qualified Language.Haskell.LSP.Types as LSP (error) -import Language.Haskell.LSP.Messages +import qualified Language.Haskell.LSP.Types as LSP (error, id) import Language.Haskell.LSP.Test.Compat import System.IO import System.Directory @@ -98,8 +99,8 @@ runSession serverExe rootDir session = do runSessionWithHandler listenServer serverExe rootDir $ do -- Wrap the session around initialize and shutdown calls - sendRequest (Proxy :: Proxy InitializeRequest) Initialize initializeParams - RspInitialize initRsp <- response + sendRequest Initialize initializeParams + initRsp <- response :: Session InitializeResponse liftIO $ maybe (return ()) (putStrLn . ("Error while initializing: " ++) . show ) (initRsp ^. LSP.error) sendNotification Initialized InitializedParams @@ -133,10 +134,11 @@ runSessionWithHandler serverHandler serverExe rootDir session = do let context = SessionContext serverIn absRootDir messageChan reqMap initState = SessionState (IdInt 9) - forkIO $ void $ runSession' meaninglessChan context initState (serverHandler serverOut) + threadId <- forkIO $ void $ runSession' meaninglessChan context initState (serverHandler serverOut) (result, _) <- runSession' messageChan context initState session terminateProcess serverProc + killThread threadId return result @@ -161,29 +163,45 @@ listenServer serverOut = do -- (DocumentSymbolParams docId) -- @ sendRequest - :: forall params resp. (ToJSON params, ToJSON resp, FromJSON resp) - => Proxy (RequestMessage ClientMethod params resp) -- ^ A proxy to provide more type information about the request. - -> ClientMethod -- ^ The request method. + :: (ToJSON params) + => --Proxy (RequestMessage ClientMethod params resp) -- ^ A proxy to provide more type information about the request. + ClientMethod -- ^ The request method. -> params -- ^ The request parameters. -> Session LspId -- ^ The id of the request that was sent. -sendRequest _ method params = do +sendRequest method params = do id <- curReqId <$> get modify $ \c -> c { curReqId = nextId id } - let req = RequestMessage "2.0" id method params :: RequestMessage ClientMethod params resp + let req = RequestMessage' "2.0" id method params - sendRequest' req + -- Update the request map + reqMap <- requestMap <$> ask + liftIO $ modifyMVar_ reqMap $ + \r -> return $ updateRequestMap r id method + + sendMessage req return id where nextId (IdInt i) = IdInt (i + 1) nextId (IdString s) = IdString $ T.pack $ show $ read (T.unpack s) + 1 +-- | A custom type for request message that doesn't +-- need a response type, allows us to infer the request +-- message type without using proxies. +data RequestMessage' a = RequestMessage' T.Text LspId ClientMethod a + +instance ToJSON a => ToJSON (RequestMessage' a) where + toJSON (RequestMessage' rpc id method params) = + object ["jsonrpc" .= rpc, "id" .= id, "method" .= method, "params" .= params] + + sendRequest' :: (ToJSON a, ToJSON b) => RequestMessage ClientMethod a b -> Session () sendRequest' req = do -- Update the request map reqMap <- requestMap <$> ask - liftIO $ modifyMVar_ reqMap (return . flip updateRequestMap req) + liftIO $ modifyMVar_ reqMap $ + \r -> return $ updateRequestMap r (req ^. LSP.id) (req ^. method) sendMessage req diff --git a/src/Language/Haskell/LSP/Test/Compat.hs b/src/Language/Haskell/LSP/Test/Compat.hs index 00f2c0b..e9b2c4c 100644 --- a/src/Language/Haskell/LSP/Test/Compat.hs +++ b/src/Language/Haskell/LSP/Test/Compat.hs @@ -1,7 +1,13 @@ {-# LANGUAGE CPP #-} - +-- For some reason ghc warns about not using +-- Control.Monad.IO.Class but it's needed for +-- MonadIO +{-# OPTIONS_GHC -Wunused-imports #-} module Language.Haskell.LSP.Test.Compat where +import Control.Concurrent.Chan +import Control.Monad.IO.Class +import Data.Conduit #ifdef mingw32_HOST_OS @@ -16,3 +22,13 @@ getProcessID :: IO Int getProcessID = fromIntegral <$> P.getProcessID #endif + +#if MIN_VERSION_conduit(1,3,0) +chanSource :: MonadIO m => Chan o -> ConduitT i o m b +#else +chanSource :: MonadIO m => Chan o -> ConduitM i o m b +#endif +chanSource c = do + x <- liftIO $ readChan c + yield x + chanSource c diff --git a/src/Language/Haskell/LSP/Test/Decoding.hs b/src/Language/Haskell/LSP/Test/Decoding.hs index f71a52f..f8d6306 100644 --- a/src/Language/Haskell/LSP/Test/Decoding.hs +++ b/src/Language/Haskell/LSP/Test/Decoding.hs @@ -51,8 +51,8 @@ type RequestMap = HM.HashMap LspId ClientMethod newRequestMap :: RequestMap newRequestMap = HM.empty -updateRequestMap :: RequestMap -> RequestMessage ClientMethod a b -> RequestMap -updateRequestMap reqMap msg = HM.insert (msg ^. id) (msg ^. method) reqMap +updateRequestMap :: RequestMap -> LspId -> ClientMethod -> RequestMap +updateRequestMap reqMap id method = HM.insert id method reqMap getRequestMap :: [FromClientMessage] -> RequestMap getRequestMap = foldl helper HM.empty diff --git a/src/Language/Haskell/LSP/Test/Files.hs b/src/Language/Haskell/LSP/Test/Files.hs index f82df65..deb89e8 100644 --- a/src/Language/Haskell/LSP/Test/Files.hs +++ b/src/Language/Haskell/LSP/Test/Files.hs @@ -50,7 +50,7 @@ mapUris f event = fromClientMsg (NotWillSaveTextDocument n) = NotWillSaveTextDocument $ swapUri (params . textDocument) n fromClientMsg (NotDidSaveTextDocument n) = NotDidSaveTextDocument $ swapUri (params . textDocument) n fromClientMsg (NotDidCloseTextDocument n) = NotDidCloseTextDocument $ swapUri (params . textDocument) n - fromClientMsg (ReqInitialize r) = ReqInitialize $ params .~ (transformInit (r ^. params)) $ r + fromClientMsg (ReqInitialize r) = ReqInitialize $ params .~ transformInit (r ^. params) $ r fromClientMsg (ReqDocumentSymbols r) = ReqDocumentSymbols $ swapUri (params . textDocument) r fromClientMsg (ReqRename r) = ReqRename $ swapUri (params . textDocument) r fromClientMsg x = x diff --git a/src/Language/Haskell/LSP/Test/Parsing.hs b/src/Language/Haskell/LSP/Test/Parsing.hs index fdd01c2..49c24c9 100644 --- a/src/Language/Haskell/LSP/Test/Parsing.hs +++ b/src/Language/Haskell/LSP/Test/Parsing.hs @@ -1,21 +1,27 @@ {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE RankNTypes #-} module Language.Haskell.LSP.Test.Parsing where import Control.Applicative +import Control.Concurrent.Chan +import Control.Concurrent.MVar import Control.Monad.Trans.Class import Control.Monad.IO.Class import Control.Monad.Trans.Reader import Control.Monad.Trans.State +import Data.Aeson +import qualified Data.ByteString.Lazy.Char8 as B +import Data.Conduit hiding (await) +import Data.Conduit.Parser +import Data.Maybe import Language.Haskell.LSP.Messages -import Language.Haskell.LSP.Types hiding (error) -import Language.Haskell.LSP.Test.Messages +import Language.Haskell.LSP.Types +import Language.Haskell.LSP.Test.Compat import Language.Haskell.LSP.Test.Decoding +import Language.Haskell.LSP.Test.Messages import System.IO -import Control.Concurrent.Chan -import Control.Concurrent.MVar -import Data.Conduit hiding (await) -import Data.Conduit.Parser data SessionContext = SessionContext { @@ -46,16 +52,39 @@ type ParserStateReader a s r m = ConduitParser a (StateT s (ReaderT r m)) type Session = ParserStateReader FromServerMessage SessionState SessionContext IO -- | Matches if the message is a notification. -notification :: Monad m => ConduitParser FromServerMessage m FromServerMessage -notification = satisfy isServerNotification +anyNotification :: Monad m => ConduitParser FromServerMessage m FromServerMessage +anyNotification = satisfy isServerNotification + +notification :: forall m a. (Monad m, FromJSON a) => ConduitParser FromServerMessage m (NotificationMessage ServerMethod a) +notification = do + let parser = decode . encodeMsg :: FromServerMessage -> Maybe (NotificationMessage ServerMethod a) + x <- satisfy (isJust . parser) + return $ fromJust $ decode $ encodeMsg x -- | Matches if the message is a request. -request :: Monad m => ConduitParser FromServerMessage m FromServerMessage -request = satisfy isServerRequest +anyRequest :: Monad m => ConduitParser FromServerMessage m FromServerMessage +anyRequest = satisfy isServerRequest + +request :: forall m a b. (Monad m, FromJSON a, FromJSON b) => ConduitParser FromServerMessage m (RequestMessage ServerMethod a b) +request = do + let parser = decode . encodeMsg :: FromServerMessage -> Maybe (RequestMessage ServerMethod a b) + x <- satisfy (isJust . parser) + return $ fromJust $ decode $ encodeMsg x -- | Matches if the message is a response. -response :: Monad m => ConduitParser FromServerMessage m FromServerMessage -response = satisfy isServerResponse +anyResponse :: Monad m => ConduitParser FromServerMessage m FromServerMessage +anyResponse = satisfy isServerResponse + +response :: forall m a. (Monad m, FromJSON a) => ConduitParser FromServerMessage m (ResponseMessage a) +response = do + let parser = decode . encodeMsg :: FromServerMessage -> Maybe (ResponseMessage a) + x <- satisfy (isJust . parser) + return $ fromJust $ decode $ encodeMsg x + +-- | A version of encode that encodes FromServerMessages as if they +-- weren't wrapped. +encodeMsg :: FromServerMessage -> B.ByteString +encodeMsg = encode . genericToJSON (defaultOptions { sumEncoding = UntaggedValue }) -- | Matches if the message is a log message notification or a show message notification/request. loggingNotification :: Monad m => ConduitParser FromServerMessage m FromServerMessage @@ -80,12 +109,6 @@ satisfy pred = do then return x else empty -chanSource :: MonadIO m => Chan o -> ConduitT i o m b -chanSource c = do - x <- liftIO $ readChan c - yield x - chanSource c - runSession' :: Chan FromServerMessage -> SessionContext -> SessionState -> Session a -> IO (a, SessionState) runSession' chan context state session = runReaderT (runStateT conduit state) context where conduit = runConduit $ chanSource chan .| runConduitParser session diff --git a/src/Language/Haskell/LSP/Test/Replay.hs b/src/Language/Haskell/LSP/Test/Replay.hs index 4802c9a..dfa364b 100644 --- a/src/Language/Haskell/LSP/Test/Replay.hs +++ b/src/Language/Haskell/LSP/Test/Replay.hs @@ -51,10 +51,15 @@ replaySession serverExe sessionDir = do rspSema <- newEmptyMVar passVar <- newEmptyMVar :: IO (MVar Bool) - forkIO $ runSessionWithHandler (listenServer serverMsgs requestMap reqSema rspSema passVar) serverExe sessionDir $ - sendMessages clientMsgs reqSema rspSema - - takeMVar passVar + threadId <- forkIO $ + runSessionWithHandler (listenServer serverMsgs requestMap reqSema rspSema passVar) + serverExe + sessionDir + (sendMessages clientMsgs reqSema rspSema) + + result <- takeMVar passVar + killThread threadId + return result where isClientMsg (FromClient _ _) = True diff --git a/test/ParsingTests.hs b/test/ParsingTests.hs index 7824ef3..c8977b5 100644 --- a/test/ParsingTests.hs +++ b/test/ParsingTests.hs @@ -18,10 +18,9 @@ parsingSpec = (PublishDiagnosticsParams (Uri "foo") (List []))) it "get picked up" $ do - let - source = yield testDiag + let source = yield testDiag session = do - diags <- publishDiagnosticsNotification + diags <- publishDiagnosticsNotification :: ConduitParser FromServerMessage IO PublishDiagnosticsNotification return $ diags ^. params . uri runConduit (source .| runConduitParser session) `shouldReturn` Uri "foo" it "get picked up after skipping others before" $ do @@ -33,6 +32,6 @@ parsingSpec = notTestDiag = NotLogMessage (NotificationMessage "2.0" WindowLogMessage (LogMessageParams MtLog "foo")) source = yield notTestDiag >> yield testDiag session = do - diags <- skipManyTill notification publishDiagnosticsNotification + diags <- skipManyTill anyNotification notification :: ConduitParser FromServerMessage IO PublishDiagnosticsNotification return $ diags ^. params . uri runConduit (source .| runConduitParser session) `shouldReturn` Uri "foo" diff --git a/test/Test.hs b/test/Test.hs index c7e6713..426dd6b 100644 --- a/test/Test.hs +++ b/test/Test.hs @@ -2,32 +2,28 @@ {-# LANGUAGE OverloadedStrings #-} import Test.Hspec import Data.Maybe -import Data.Proxy import Control.Monad.IO.Class import Control.Lens hiding (List) import Language.Haskell.LSP.Test import Language.Haskell.LSP.Test.Replay import Language.Haskell.LSP.Types -import Language.Haskell.LSP.Messages import ParsingTests main = hspec $ do - describe "manual session validation" $ do + describe "manual session" $ do it "passes a test" $ runSession "hie" "test/recordings/renamePass" $ do doc <- openDoc "Desktop/simple.hs" "haskell" skipMany loggingNotification - NotPublishDiagnostics diagsNot <- notification + diagsNot <- notification :: Session PublishDiagnosticsNotification liftIO $ diagsNot ^. params . diagnostics `shouldBe` List [] - sendRequest (Proxy :: Proxy DocumentSymbolRequest) - TextDocumentDocumentSymbol - (DocumentSymbolParams doc) + sendRequest TextDocumentDocumentSymbol (DocumentSymbolParams doc) - RspDocumentSymbols rspSymbols <- response + rspSymbols <- response :: Session DocumentSymbolsResponse liftIO $ do let (List symbols) = fromJust (rspSymbols ^. result) @@ -42,7 +38,7 @@ main = hspec $ do let session = runSession "hie" "test/recordings/renamePass" $ do openDoc "Desktop/simple.hs" "haskell" skipMany loggingNotification - request + anyRequest in session `shouldThrow` anyException describe "replay session" $ do