@@ -82,7 +82,9 @@ data LspCoreLog
82
82
| ConfigurationNotSupported
83
83
| BadConfigurationResponse ResponseError
84
84
| WrongConfigSections [J. Value ]
85
- deriving (Show )
85
+ | forall m . CantRegister (SMethod m )
86
+
87
+ deriving instance (Show LspCoreLog )
86
88
87
89
instance Pretty LspCoreLog where
88
90
pretty (NewConfig config) = " LSP: set new config:" <+> prettyJSON config
@@ -96,6 +98,7 @@ instance Pretty LspCoreLog where
96
98
]
97
99
pretty (BadConfigurationResponse err) = " LSP: error when requesting configuration: " <+> pretty err
98
100
pretty (WrongConfigSections sections) = " LSP: expected only one configuration section, got: " <+> (prettyJSON $ J. toJSON sections)
101
+ pretty (CantRegister m) = " LSP: can't register dynamically for:" <+> pretty m
99
102
100
103
newtype LspT config m a = LspT { unLspT :: ReaderT (LanguageContextEnv config ) m a }
101
104
deriving (Functor , Applicative , Monad , MonadCatch , MonadIO , MonadMask , MonadThrow , MonadTrans , MonadUnliftIO , MonadFix )
@@ -550,30 +553,27 @@ getWorkspaceFolders = do
550
553
registerCapability ::
551
554
forall f t (m :: Method ClientToServer t ) config .
552
555
MonadLsp config f =>
556
+ LogAction f (WithSeverity LspCoreLog ) ->
553
557
SClientMethod m ->
554
558
RegistrationOptions m ->
555
559
Handler f m ->
556
560
f (Maybe (RegistrationToken m ))
557
- registerCapability method regOpts f = do
558
- clientCaps <- resClientCapabilities <$> getLspEnv
561
+ registerCapability logger method regOpts f = do
559
562
handlers <- resHandlers <$> getLspEnv
560
563
let alreadyStaticallyRegistered = case splitClientMethod method of
561
564
IsClientNot -> SMethodMap. member method $ notHandlers handlers
562
565
IsClientReq -> SMethodMap. member method $ reqHandlers handlers
563
566
IsClientEither -> error " Cannot register capability for custom methods"
564
- go clientCaps alreadyStaticallyRegistered
567
+ go alreadyStaticallyRegistered
565
568
where
566
569
-- If the server has already registered statically, don't dynamically register
567
570
-- as per the spec
568
- go _clientCaps True = pure Nothing
569
- go clientCaps False
570
- -- First, check to see if the client supports dynamic registration on this method
571
- | dynamicRegistrationSupported method clientCaps = do
572
- uuid <- liftIO $ UUID. toText <$> getStdRandom random
573
- let registration = L. TRegistration uuid method (Just regOpts)
574
- params = L. RegistrationParams [toUntypedRegistration registration]
575
- regId = RegistrationId uuid
576
- rio <- askUnliftIO
571
+ go True = pure Nothing
572
+ go False = do
573
+ rio <- askUnliftIO
574
+ mtoken <- trySendRegistration logger method regOpts
575
+ case mtoken of
576
+ Just token@ (RegistrationToken _ regId) -> do
577
577
~ () <- case splitClientMethod method of
578
578
IsClientNot -> modifyState resRegistrationsNot $ \ oldRegs ->
579
579
let pair = Pair regId (ClientMessageHandler (unliftIO rio . f))
@@ -583,11 +583,33 @@ registerCapability method regOpts f = do
583
583
in SMethodMap. insert method pair oldRegs
584
584
IsClientEither -> error " Cannot register capability for custom methods"
585
585
586
- -- TODO: handle the scenario where this returns an error
587
- _ <- sendRequest SMethod_ClientRegisterCapability params $ \ _res - > pure ()
586
+ pure $ Just token
587
+ Nothing - > pure Nothing
588
588
589
- pure (Just (RegistrationToken method regId))
590
- | otherwise = pure Nothing
589
+ trySendRegistration ::
590
+ forall f t (m :: Method ClientToServer t ) config .
591
+ MonadLsp config f =>
592
+ LogAction f (WithSeverity LspCoreLog ) ->
593
+ SClientMethod m ->
594
+ RegistrationOptions m ->
595
+ f (Maybe (RegistrationToken m ))
596
+ trySendRegistration logger method regOpts = do
597
+ clientCaps <- resClientCapabilities <$> getLspEnv
598
+ -- First, check to see if the client supports dynamic registration on this method
599
+ if dynamicRegistrationSupported method clientCaps
600
+ then do
601
+ uuid <- liftIO $ UUID. toText <$> getStdRandom random
602
+ let registration = L. TRegistration uuid method (Just regOpts)
603
+ params = L. RegistrationParams [toUntypedRegistration registration]
604
+ regId = RegistrationId uuid
605
+
606
+ -- TODO: handle the scenario where this returns an error
607
+ _ <- sendRequest SMethod_ClientRegisterCapability params $ \ _res -> pure ()
608
+
609
+ pure (Just $ RegistrationToken method regId)
610
+ else do
611
+ logger <& (CantRegister SMethod_WorkspaceDidChangeConfiguration ) `WithSeverity ` Warning
612
+ pure Nothing
591
613
592
614
{- | Sends a @client/unregisterCapability@ request and removes the handler
593
615
for that associated registration.
0 commit comments