{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE FlexibleInstances #-}
module Language.Haskell.LSP.Test.Session
( Session
- , SessionState(..)
+ , SessionConfig(..)
, SessionContext(..)
+ , SessionState(..)
+ , MonadSessionConfig(..)
, runSessionWithHandles
, get
, put
import Data.Aeson
import Data.Conduit
import Data.Conduit.Parser
+import Data.Default
import Data.Foldable
import Data.List
import qualified Data.HashMap.Strict as HashMap
import Language.Haskell.LSP.Messages
+import Language.Haskell.LSP.TH.ClientCapabilities
import Language.Haskell.LSP.Types
import Language.Haskell.LSP.VFS
import Language.Haskell.LSP.Test.Compat
import System.Directory
import System.IO
+-- | A session representing one instance of launching and connecting to a server.
+--
+-- You can send and receive messages to the server within 'Session' via 'getMessage',
+-- 'sendRequest' and 'sendNotification'.
+--
+-- @
+-- runSession \"path\/to\/root\/dir\" $ do
+-- docItem <- getDocItem "Desktop/simple.hs" "haskell"
+-- sendNotification TextDocumentDidOpen (DidOpenTextDocumentParams docItem)
+-- diagnostics <- getMessage :: Session PublishDiagnosticsNotification
+-- @
+type Session = ParserStateReader FromServerMessage SessionState SessionContext IO
+
+-- | Stuff you can configure for a 'Session'.
+data SessionConfig = SessionConfig
+ {
+ capabilities :: ClientCapabilities, -- ^ Specific capabilities the client should advertise.
+ timeout :: Int -- ^ Maximum time to wait for a request in seconds.
+ }
+
+instance Default SessionConfig where
+ def = SessionConfig def 60
+
+class Monad m => MonadSessionConfig m where
+ sessionConfig :: m SessionConfig
+
+instance Monad m => MonadSessionConfig (StateT SessionState (ReaderT SessionContext m)) where
+ sessionConfig = config <$> lift Reader.ask
+
data SessionContext = SessionContext
{
serverIn :: Handle
, messageChan :: Chan FromServerMessage
, requestMap :: MVar RequestMap
, initRsp :: MVar InitializeResponse
+ , config :: SessionConfig
}
data SessionState = SessionState
type ParserStateReader a s r m = ConduitParser a (StateT s (ReaderT r m))
--- | A session representing one instance of launching and connecting to a server.
---
--- You can send and receive messages to the server within 'Session' via 'getMessage',
--- 'sendRequest' and 'sendNotification'.
---
--- @
--- runSession \"path\/to\/root\/dir\" $ do
--- docItem <- getDocItem "Desktop/simple.hs" "haskell"
--- sendNotification TextDocumentDidOpen (DidOpenTextDocumentParams docItem)
--- diagnostics <- getMessage :: Session PublishDiagnosticsNotification
--- @
-type Session = ParserStateReader FromServerMessage SessionState SessionContext IO
-
type SessionProcessor = ConduitT FromServerMessage FromServerMessage (StateT SessionState (ReaderT SessionContext IO))
-runSession' :: Chan FromServerMessage -> SessionProcessor () -> SessionContext -> SessionState -> Session a -> IO (a, SessionState)
-runSession' chan preprocessor context state session = runReaderT (runStateT conduit state) context
+runSession :: Chan FromServerMessage -> SessionProcessor () -> SessionContext -> SessionState -> Session a -> IO (a, SessionState)
+runSession chan preprocessor context state session = runReaderT (runStateT conduit state) context
where conduit = runConduit $ chanSource chan .| preprocessor .| runConduitParser session
get :: Monad m => ParserStateReader a s r m s
runSessionWithHandles :: Handle -- ^ Server in
-> Handle -- ^ Server out
-> (Handle -> Session ()) -- ^ Server listener
+ -> SessionConfig
-> FilePath
-> Session a
-> IO a
-runSessionWithHandles serverIn serverOut serverHandler rootDir session = do
+runSessionWithHandles serverIn serverOut serverHandler config rootDir session = do
absRootDir <- canonicalizePath rootDir
hSetBuffering serverIn NoBuffering
meaninglessChan <- newChan
initRsp <- newEmptyMVar
- let context = SessionContext serverIn absRootDir messageChan reqMap initRsp
+ let context = SessionContext serverIn absRootDir messageChan reqMap initRsp config
initState = SessionState (IdInt 0) mempty
- threadId <- forkIO $ void $ runSession' meaninglessChan processor context initState (serverHandler serverOut)
- (result, _) <- runSession' messageChan processor context initState session
+ threadId <- forkIO $ void $ runSession meaninglessChan processor context initState (serverHandler serverOut)
+ (result, _) <- runSession messageChan processor context initState session
killThread threadId