Skip to content

Commit 0ca285e

Browse files
committed
Expose more functions
1 parent 87134a4 commit 0ca285e

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

lsp-test/src/Language/LSP/Test.hs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ module Language.LSP.Test
5252
-- ** Initialization
5353
, initializeResponse
5454
-- ** Config
55+
, modifyConfig
5556
, setConfig
57+
, modifyConfigSection
58+
, setConfigSection
5659
-- ** Documents
5760
, createDoc
5861
, openDoc
@@ -148,6 +151,7 @@ import System.Process (ProcessHandle, CreateProcess)
148151
import qualified System.FilePath.Glob as Glob
149152
import Control.Monad.State (execState)
150153
import Data.Traversable (for)
154+
import Data.String (fromString)
151155

152156
-- | Starts a new session.
153157
--
@@ -411,12 +415,14 @@ setIgnoringConfigurationRequests :: Bool -> Session ()
411415
setIgnoringConfigurationRequests value = do
412416
modify (\ss -> ss { ignoringConfigurationRequests = value })
413417

414-
-- | Set the client config. This will send a notification to the server that the
418+
-- | Modify the client config. This will send a notification to the server that the
415419
-- config has changed.
416-
setConfig :: Object
417-
-> Session ()
418-
setConfig newConfig = do
419-
modify (\ss -> ss { curLspConfig = newConfig})
420+
modifyConfig :: (Object -> Object) -> Session ()
421+
modifyConfig f = do
422+
oldConfig <- curLspConfig <$> get
423+
let newConfig = f oldConfig
424+
modify (\ss -> ss { curLspConfig = newConfig })
425+
420426
caps <- asks sessionCapabilities
421427
let supportsConfiguration = fromMaybe False $ caps ^? L.workspace . _Just . L.configuration . _Just
422428
-- TODO: make this configurable?
@@ -425,6 +431,21 @@ setConfig newConfig = do
425431
configToSend = if supportsConfiguration then J.Null else Object newConfig
426432
sendNotification SMethod_WorkspaceDidChangeConfiguration $ DidChangeConfigurationParams configToSend
427433

434+
-- | Set the client config. This will send a notification to the server that the
435+
-- config has changed.
436+
setConfig :: Object -> Session ()
437+
setConfig newConfig = modifyConfig (const newConfig)
438+
439+
-- | Modify a client config section (if already present, otherwise does nothing).
440+
-- This will send a notification to the server that the config has changed.
441+
modifyConfigSection :: String -> (Value -> Value) -> Session ()
442+
modifyConfigSection section f = modifyConfig (\o -> o & ix (fromString section) %~ f)
443+
444+
-- | Set a client config section. This will send a notification to the server that the
445+
-- config has changed.
446+
setConfigSection :: String -> Value -> Session ()
447+
setConfigSection section settings = modifyConfig (\o -> o & at(fromString section) ?~ settings)
448+
428449
-- | /Creates/ a new text document. This is different from 'openDoc'
429450
-- as it sends a workspace/didChangeWatchedFiles notification letting the server
430451
-- know that a file was created within the workspace, __provided that the server

lsp-test/src/Language/LSP/Test/Session.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ data SessionConfig = SessionConfig
115115
-- Can be overriden with the environment variable @LSP_TEST_LOG_MESSAGES@.
116116
, logColor :: Bool -- ^ Add ANSI color to the logged messages, defaults to True.
117117
, lspConfig :: Object
118-
-- ^ The initial LSP config as JSON value, defaults to the empty object.
118+
-- ^ The initial LSP config as JSON object, defaults to the empty object.
119119
-- This should include the config section for the server if it has one, i.e. if
120120
-- the server has a 'mylang' config section, then the config should be an object
121121
-- with a 'mylang' key whose value is the actual config for the server. You

lsp-test/test/Test.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import DummyServer
99
import Test.Hspec
1010
import Data.Aeson
1111
import qualified Data.Aeson as J
12-
import qualified Data.Aeson.KeyMap as J
1312
import Data.Default
1413
import qualified Data.Map.Strict as M
1514
import Data.Either
@@ -146,7 +145,7 @@ main = hspec $ around withDummyServer $ do
146145
c <- requestConfig
147146
-- from the server definition
148147
liftIO $ c `shouldBe` 1
149-
setConfig $ J.singleton "dummy" (toJSON @Int 2)
148+
setConfigSection "dummy" (toJSON @Int 2)
150149
-- ensure the configuration change has happened
151150
configurationRequest
152151
c <- requestConfig

0 commit comments

Comments
 (0)