Skip to content

Commit

Permalink
Pass the ClientCapabilities to the static handlers in ServerDefinition
Browse files Browse the repository at this point in the history
See changelog.
  • Loading branch information
michaelpj committed Jul 2, 2023
1 parent e9d2cff commit c8c9364
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ main :: IO Int
main = runServer $ ServerDefinition
{ onConfigurationChange = const $ pure $ Right ()
, doInitialize = \env _req -> pure $ Right env
, staticHandlers = handlers
, staticHandlers = \_caps -> handlers
, interpretHandler = \env -> Iso (runLspT env) liftIO
, options = defaultOptions
}
Expand Down
2 changes: 1 addition & 1 deletion lsp-test/bench/SimpleBench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ server = ServerDefinition
{ onConfigurationChange = const $ const $ Right ()
, defaultConfig = ()
, doInitialize = \env _req -> pure $ Right env
, staticHandlers = handlers
, staticHandlers = \_caps -> handlers
, interpretHandler = \env -> Iso (runLspT env) liftIO
, options = defaultOptions
}
Expand Down
4 changes: 2 additions & 2 deletions lsp-test/func-test/FuncTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ main = hspec $ do
{ onConfigurationChange = const $ const $ Right ()
, defaultConfig = ()
, doInitialize = \env _req -> pure $ Right env
, staticHandlers = handlers killVar
, staticHandlers = \_caps -> handlers killVar
, interpretHandler = \env -> Iso (runLspT env) liftIO
, options = defaultOptions
}
Expand Down Expand Up @@ -82,7 +82,7 @@ main = hspec $ do
{ onConfigurationChange = const $ const $ Right ()
, defaultConfig = ()
, doInitialize = \env _req -> pure $ Right env
, staticHandlers = handlers
, staticHandlers = \_caps -> handlers
, interpretHandler = \env -> Iso (runLspT env) liftIO
, options = defaultOptions
}
Expand Down
2 changes: 1 addition & 1 deletion lsp-test/test/DummyServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ withDummyServer f = do
{ doInitialize = \env _req -> pure $ Right env
, defaultConfig = ()
, onConfigurationChange = const $ pure $ Right ()
, staticHandlers = handlers
, staticHandlers = \_caps -> handlers
, interpretHandler = \env ->
Iso (\m -> runLspT env (runReaderT m handlerEnv)) liftIO
, options = defaultOptions {optExecuteCommandCommands = Just ["doAnEdit"]}
Expand Down
7 changes: 7 additions & 0 deletions lsp/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Revision history for lsp

## 2.1.0.0

* `staticHandlers` now takes the client capabilities as an argument.
These are static across the lifecycle of the server, so this allows
a server to decide at construction e.g. whether to provide handlers
for resolve methods depending on whether the client supports it.

## 2.0.0.0

* Support `lsp-types-2.0.0.0`.
Expand Down
2 changes: 1 addition & 1 deletion lsp/example/Reactor.hs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ run = flip E.catches handlers $ do
J.Success cfg -> Right cfg
, doInitialize = \env _ -> forkIO (reactor stderrLogger rin) >> pure (Right env)
-- Handlers log to both the client and stderr
, staticHandlers = lspHandlers dualLogger rin
, staticHandlers = \_caps -> lspHandlers dualLogger rin
, interpretHandler = \env -> Iso (runLspT env) liftIO
, options = lspOptions
}
Expand Down
2 changes: 1 addition & 1 deletion lsp/example/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ main = runServer $ ServerDefinition
{ onConfigurationChange = const $ const $ Right ()
, defaultConfig = ()
, doInitialize = \env _req -> pure $ Right env
, staticHandlers = handlers
, staticHandlers = \_caps -> handlers
, interpretHandler = \env -> Iso (runLspT env) liftIO
, options = defaultOptions
}
2 changes: 1 addition & 1 deletion lsp/lsp.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 2.2
name: lsp
version: 2.0.0.0
version: 2.1.0.0
synopsis: Haskell library for the Microsoft Language Server Protocol
description:
An implementation of the types, and basic message server to
Expand Down
4 changes: 3 additions & 1 deletion lsp/src/Language/LSP/Server/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,12 @@ data ServerDefinition config = forall m a.
-- language server implementation the chance to create any processes or
-- start new threads that may be necessary for the server lifecycle. It can
-- also return an error in the initialization if necessary.
, staticHandlers :: Handlers m
, staticHandlers :: ClientCapabilities -> Handlers m
-- ^ Handlers for any methods you want to statically support.
-- The handlers here cannot be unregistered during the server's lifetime
-- and will be registered statically in the initialize request.
-- The handlers provided can depend on the client capabilities, which
-- are static across the lifetime of the server.
, interpretHandler :: a -> (m <~> IO)
-- ^ How to run the handlers in your own monad of choice, @m@.
-- It is passed the result of 'doInitialize', so typically you will want
Expand Down
5 changes: 3 additions & 2 deletions lsp/src/Language/LSP/Server/Processing.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ initializeRequestHandler ServerDefinition{..} vfs sendFunc req = do
let p = req ^. L.params
rootDir = getFirst $ foldMap First [ p ^? L.rootUri . _L >>= uriToFilePath
, p ^? L.rootPath . _Just . _L <&> T.unpack ]
clientCaps = (p ^. L.capabilities)

let initialWfs = case p ^. L.workspaceFolders of
Just (InL xs) -> xs
Expand All @@ -152,11 +153,11 @@ initializeRequestHandler ServerDefinition{..} vfs sendFunc req = do

-- Call the 'duringInitialization' callback to let the server kick stuff up
let env = LanguageContextEnv handlers onConfigurationChange sendFunc stateVars (p ^. L.capabilities) rootDir
handlers = transmuteHandlers interpreter staticHandlers
handlers = transmuteHandlers interpreter (staticHandlers clientCaps)
interpreter = interpretHandler initializationResult
initializationResult <- ExceptT $ doInitialize env req

let serverCaps = inferServerCapabilities (p ^. L.capabilities) options handlers
let serverCaps = inferServerCapabilities clientCaps options handlers
liftIO $ sendResp $ makeResponseMessage (req ^. L.id) (InitializeResult serverCaps (optServerInfo options))
pure env
where
Expand Down

0 comments on commit c8c9364

Please sign in to comment.