Skip to content

Commit 87134a4

Browse files
committed
Config as Object
1 parent 5892575 commit 87134a4

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ runSessionWithHandles' serverProc serverIn serverOut config' caps rootDir sessio
230230
(InL $ filePathToUri absRootDir)
231231
caps
232232
-- TODO: make this configurable?
233-
(Just $ lspConfig config')
233+
(Just $ Object $ lspConfig config')
234234
(Just TraceValues_Off)
235235
(fmap InL $ initialWorkspaceFolders config)
236236
runSession' serverIn serverOut serverProc listenServer config caps rootDir exitServer $ do
@@ -413,7 +413,7 @@ setIgnoringConfigurationRequests value = do
413413

414414
-- | Set the client config. This will send a notification to the server that the
415415
-- config has changed.
416-
setConfig :: Value
416+
setConfig :: Object
417417
-> Session ()
418418
setConfig newConfig = do
419419
modify (\ss -> ss { curLspConfig = newConfig})
@@ -422,7 +422,7 @@ setConfig newConfig = do
422422
-- TODO: make this configurable?
423423
-- if they support workspace/configuration then be annoying and don't send the full config so
424424
-- they have to request it
425-
configToSend = if supportsConfiguration then J.Null else newConfig
425+
configToSend = if supportsConfiguration then J.Null else Object newConfig
426426
sendNotification SMethod_WorkspaceDidChangeConfiguration $ DidChangeConfigurationParams configToSend
427427

428428
-- | /Creates/ a new text document. This is different from 'openDoc'

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

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import Control.Monad.Trans.State (StateT, runStateT, execState)
5353
import qualified Control.Monad.Trans.State as State
5454
import qualified Data.ByteString.Lazy.Char8 as B
5555
import Data.Aeson hiding (Error, Null)
56-
import qualified Data.Aeson as J
5756
import Data.Aeson.Encode.Pretty
5857
import Data.Aeson.Lens ()
5958
import Data.Conduit as Conduit
@@ -115,8 +114,8 @@ data SessionConfig = SessionConfig
115114
-- ^ Trace the messages sent and received to stdout, defaults to False.
116115
-- Can be overriden with the environment variable @LSP_TEST_LOG_MESSAGES@.
117116
, logColor :: Bool -- ^ Add ANSI color to the logged messages, defaults to True.
118-
, lspConfig :: Value
119-
-- ^ The initial LSP config as JSON value, defaults to Null.
117+
, lspConfig :: Object
118+
-- ^ The initial LSP config as JSON value, defaults to the empty object.
120119
-- This should include the config section for the server if it has one, i.e. if
121120
-- the server has a 'mylang' config section, then the config should be an object
122121
-- with a 'mylang' key whose value is the actual config for the server. You
@@ -134,7 +133,7 @@ data SessionConfig = SessionConfig
134133

135134
-- | The configuration used in 'Language.LSP.Test.runSession'.
136135
defaultConfig :: SessionConfig
137-
defaultConfig = SessionConfig 60 False False True J.Null True True Nothing
136+
defaultConfig = SessionConfig 60 False False True mempty True True Nothing
138137

139138
instance Default SessionConfig where
140139
def = defaultConfig
@@ -188,9 +187,9 @@ data SessionState = SessionState
188187
-- Used for providing exception information
189188
, lastReceivedMessage :: !(Maybe FromServerMessage)
190189
, curDynCaps :: !(Map.Map T.Text SomeRegistration)
191-
, curLspConfig :: Value
192190
-- ^ The capabilities that the server has dynamically registered with us so
193191
-- far
192+
, curLspConfig :: Object
194193
, curProgressSessions :: !(Set.Set ProgressToken)
195194
, ignoringLogNotifications :: Bool
196195
, ignoringConfigurationRequests :: Bool
@@ -317,24 +316,20 @@ updateStateC = awaitForever $ \msg -> do
317316
sendMessage $ TResponseMessage "2.0" (Just $ r ^. L.id) (Right $ ApplyWorkspaceEditResult True Nothing Nothing)
318317
FromServerMess SMethod_WorkspaceConfiguration r -> do
319318
let requestedSections = mapMaybe (\i -> i ^? L.section . _Just) $ r ^. L.params . L.items
320-
c <- curLspConfig <$> get @SessionState
321-
case c of
322-
Object o -> do
323-
-- check for each requested section whether we have it
324-
let configsOrErrs = (flip fmap) requestedSections $ \section ->
325-
case o ^. at (fromString $ T.unpack section) of
326-
Just config -> Right config
327-
Nothing -> Left section
328-
329-
let (errs, configs) = partitionEithers configsOrErrs
330-
331-
-- we have to return exactly the number of sections requested, so if we can't find all of them then that's an error
332-
if null errs
333-
then sendMessage $ TResponseMessage "2.0" (Just $ r ^. L.id) (Right configs)
334-
else sendMessage @_ @(TResponseError Method_WorkspaceConfiguration) $
335-
TResponseError (InL LSPErrorCodes_RequestFailed) ("No configuration for requested sections: " <> (T.pack $ show errs)) Nothing
336-
337-
_ -> sendMessage @_ @(TResponseError Method_WorkspaceConfiguration) $ TResponseError (InL LSPErrorCodes_RequestFailed) "No configuration" Nothing
319+
o <- curLspConfig <$> get @SessionState
320+
-- check for each requested section whether we have it
321+
let configsOrErrs = (flip fmap) requestedSections $ \section ->
322+
case o ^. at (fromString $ T.unpack section) of
323+
Just config -> Right config
324+
Nothing -> Left section
325+
326+
let (errs, configs) = partitionEithers configsOrErrs
327+
328+
-- we have to return exactly the number of sections requested, so if we can't find all of them then that's an error
329+
if null errs
330+
then sendMessage $ TResponseMessage "2.0" (Just $ r ^. L.id) (Right configs)
331+
else sendMessage @_ @(TResponseError Method_WorkspaceConfiguration) $
332+
TResponseError (InL LSPErrorCodes_RequestFailed) ("No configuration for requested sections: " <> (T.pack $ show errs)) Nothing
338333
_ -> pure ()
339334
unless ((ignoringLogNotifications state && isLogNotification msg) || (ignoringConfigurationRequests state && isConfigRequest msg)) $
340335
yield msg

lsp-test/test/Test.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ 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
1213
import Data.Default
1314
import qualified Data.Map.Strict as M
1415
import Data.Either
@@ -145,7 +146,7 @@ main = hspec $ around withDummyServer $ do
145146
c <- requestConfig
146147
-- from the server definition
147148
liftIO $ c `shouldBe` 1
148-
setConfig $ J.object ["dummy" J..= toJSON @Int 2]
149+
setConfig $ J.singleton "dummy" (toJSON @Int 2)
149150
-- ensure the configuration change has happened
150151
configurationRequest
151152
c <- requestConfig

0 commit comments

Comments
 (0)