@@ -82,7 +82,9 @@ data LspCoreLog
8282 | ConfigurationNotSupported
8383 | BadConfigurationResponse ResponseError
8484 | WrongConfigSections [J. Value ]
85- deriving (Show )
85+ | forall m . CantRegister (SMethod m )
86+
87+ deriving instance (Show LspCoreLog )
8688
8789instance Pretty LspCoreLog where
8890 pretty (NewConfig config) = " LSP: set new config:" <+> prettyJSON config
@@ -96,6 +98,7 @@ instance Pretty LspCoreLog where
9698 ]
9799 pretty (BadConfigurationResponse err) = " LSP: error when requesting configuration: " <+> pretty err
98100 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
99102
100103newtype LspT config m a = LspT { unLspT :: ReaderT (LanguageContextEnv config ) m a }
101104 deriving (Functor , Applicative , Monad , MonadCatch , MonadIO , MonadMask , MonadThrow , MonadTrans , MonadUnliftIO , MonadFix )
@@ -550,30 +553,27 @@ getWorkspaceFolders = do
550553registerCapability ::
551554 forall f t (m :: Method ClientToServer t ) config .
552555 MonadLsp config f =>
556+ LogAction f (WithSeverity LspCoreLog ) ->
553557 SClientMethod m ->
554558 RegistrationOptions m ->
555559 Handler f m ->
556560 f (Maybe (RegistrationToken m ))
557- registerCapability method regOpts f = do
558- clientCaps <- resClientCapabilities <$> getLspEnv
561+ registerCapability logger method regOpts f = do
559562 handlers <- resHandlers <$> getLspEnv
560563 let alreadyStaticallyRegistered = case splitClientMethod method of
561564 IsClientNot -> SMethodMap. member method $ notHandlers handlers
562565 IsClientReq -> SMethodMap. member method $ reqHandlers handlers
563566 IsClientEither -> error " Cannot register capability for custom methods"
564- go clientCaps alreadyStaticallyRegistered
567+ go alreadyStaticallyRegistered
565568 where
566569 -- If the server has already registered statically, don't dynamically register
567570 -- 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
577577 ~ () <- case splitClientMethod method of
578578 IsClientNot -> modifyState resRegistrationsNot $ \ oldRegs ->
579579 let pair = Pair regId (ClientMessageHandler (unliftIO rio . f))
@@ -583,11 +583,33 @@ registerCapability method regOpts f = do
583583 in SMethodMap. insert method pair oldRegs
584584 IsClientEither -> error " Cannot register capability for custom methods"
585585
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
588588
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
591613
592614{- | Sends a @client/unregisterCapability@ request and removes the handler
593615 for that associated registration.
0 commit comments