Copyright | (c) Jaro Reinders 2017 |
---|---|
License | GPL-2 |
Maintainer | [email protected] |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
LSP.Client
Contents
Description
This module contains an implementation of a client for the Language Server Protocol. It uses the same data types as the haskell-lsp library.
This client is intended to be used by text editors written in haskell to provide the user with IDE-like features.
This module is intended to be imported qualified:
import qualified LSP.Client as Client
In the examples in this module it is assumed that the following modules are imported:
import qualified Language.Haskell.LSP.TH.DataTypesJSON as LSP
A complete example can be found in the github repository.
TODO:
- Implement proper exception handling.
- start :: Config -> IO (MVar ClientMessage)
- data Config = Config {}
- data RequestMessageHandler = RequestMessageHandler {
- handleWindowShowMessageRequest :: ShowMessageRequest -> IO ShowMessageResponse
- handleClientRegisterCapability :: RegisterCapabilityRequest -> IO ErrorResponse
- handleClientUnregisterCapability :: UnregisterCapabilityRequest -> IO ErrorResponse
- handleWorkspaceApplyEdit :: ApplyWorkspaceEditRequest -> IO ApplyWorkspaceEditResponse
- data NotificationMessageHandler = NotificationMessageHandler {}
- sendClientRequest :: forall params resp. (ToJSON params, ToJSON resp, FromJSON resp) => Proxy (RequestMessage ClientMethod params resp) -> MVar ClientMessage -> ClientMethod -> params -> IO (Maybe (Either ResponseError resp))
- sendClientNotification :: forall params. ToJSON params => MVar ClientMessage -> ClientMethod -> params -> IO ()
Initialization
start :: Config -> IO (MVar ClientMessage) Source #
Start the language server.
Example:
reqVar <- Client.start myConfig
Where inp
and out
are the Handle
s of the lsp client and
testHandleNotification
and testHandleRequestMessage
are NotificationMessageHandler
and
RequestMessageHandler
respectively.
reqVar
can be passed to the sendClientRequest
and sendClientNotification
functions.
The configuration of the Language Server Protocol client.
toServer
and fromServer
are the Handle
s which can be used
to send messages to and receive messages from the server.
Create this configuration and pass it to the start
function.
Example:
(Just inp, Just out, _, _) <- createProcess (proc "hie" ["--lsp"]) {std_in = CreatePipe, std_out = CreatePipe, std_err = CreatePipe} let myConfig = Config inp out testHandleNotificationMessage testHandleRequestMessage
This example will run hie --lsp
and combine the inp
and out
Handle
s
with the testHandleNotificationMessage
and testHandleRequestMessage
handlers to form the configuration of the client.
Constructors
Config | |
Fields |
Receiving
data RequestMessageHandler Source #
The handlers for request messages from the server.
Define these once and pass them via the Config
data type to the start
function.
Example:
testRequestMessageHandler :: Client.RequestMessageHandler testRequestMessageHandler = Client.RequestMessageHandler (m -> emptyResponse m <$ print m) (m -> emptyResponse m <$ print m) (m -> emptyResponse m <$ print m) (m -> emptyResponse m <$ print m) where toRspId (LSP.IdInt i) = LSP.IdRspInt i toRspId (LSP.IdString t) = LSP.IdRspString t emptyResponse :: LSP.RequestMessage m req resp -> LSP.ResponseMessage a emptyResponse m = LSP.ResponseMessage (m ^. LSP.jsonrpc) (toRspId (m ^. LSP.id)) Nothing Nothing
This example will print all request messages to and send back an empty response message.
Constructors
data NotificationMessageHandler Source #
The handlers for notification messages from the server.
Define these once and pass them via the Config
data type to the start
function.
Example:
testNotificationMessageHandler :: Client.NotificationMessageHandler testNotificationMessageHandler = Client.NotificationMessageHandler (T.putStrLn . view (LSP.params . LSP.message)) (T.putStrLn . view (LSP.params . LSP.message)) (print . view LSP.params) (mapM_ T.putStrLn . (^.. LSP.params . LSP.diagnostics . traverse . LSP.message))
This example will print the message content of each notification.
Constructors
NotificationMessageHandler | |
Fields |
Sending
sendClientRequest :: forall params resp. (ToJSON params, ToJSON resp, FromJSON resp) => Proxy (RequestMessage ClientMethod params resp) -> MVar ClientMessage -> ClientMethod -> params -> IO (Maybe (Either ResponseError resp)) Source #
Send a request message to the Language Server and wait for its response.
Example:
Client.sendClientRequest (Proxy :: Proxy LSP.InitializeRequest) reqVar LSP.Initialize initializeParams
Where reqVar
is the MVar
generated by the start
function and initializeParams
are the
parameters to the initialize request as specified in the Language Server Protocol and
the haskell-lsp package. Note that in this case the result is ignored.
sendClientNotification :: forall params. ToJSON params => MVar ClientMessage -> ClientMethod -> params -> IO () Source #
Send a notification message to the Language Server.
Example:
Client.sendClientNotification reqVar LSP.Initialized (Just LSP.InitializedParams)
Where reqVar
is the MVar
generated by the start
function.