-
-
Notifications
You must be signed in to change notification settings - Fork 390
LSP window message log recorder #2750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d176e6e
2feed19
3afc19b
a2bbc75
49baf24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{-# LANGUAGE GADTs #-} | ||
|
||
module Development.IDE.Plugin.LSPWindowShowMessageRecorder (makeLspShowMessageRecorder) where | ||
|
||
import Control.Monad.IO.Class | ||
import Control.Monad.IO.Unlift (MonadUnliftIO) | ||
import Data.Foldable (for_) | ||
import Data.IORef | ||
import Data.IORef.Extra (atomicModifyIORef'_) | ||
import Data.Text (Text) | ||
import Development.IDE.Types.Logger | ||
import Ide.Types (PluginDescriptor (pluginNotificationHandlers), defaultPluginDescriptor, mkPluginNotificationHandler) | ||
import Language.LSP.Server (LanguageContextEnv, getLspEnv) | ||
import qualified Language.LSP.Server as LSP | ||
import Language.LSP.Types (MessageType (..), SMethod (SInitialized, SWindowShowMessage), ShowMessageParams (..)) | ||
|
||
-- | Creates a recorder that logs to the LSP stream via WindowShowMessage notifications. | ||
-- The recorder won't attempt to send messages until the LSP stream is initialized. | ||
makeLspShowMessageRecorder :: | ||
IO (Recorder (WithPriority Text), PluginDescriptor c) | ||
makeLspShowMessageRecorder = do | ||
envRef <- newIORef Nothing | ||
-- messages logged before the LSP stream is initialized will be sent when it is | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is neat, my version just didn't record until it gets setup when the LSP server is ready. |
||
backLogRef <- newIORef [] | ||
let recorder = Recorder $ \it -> do | ||
mbenv <- liftIO $ readIORef envRef | ||
case mbenv of | ||
Nothing -> liftIO $ atomicModifyIORef'_ backLogRef (it :) | ||
Just env -> sendMsg env it | ||
-- the plugin captures the language context, so it can be used to send messages | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really like this approach. It's extremely indirect. My branch just has a function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could relatively easily adapt this to just pass the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please send a PR! I suspect that the plugin solution is more modular, as it abstracts away the passing of the mutable state around. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but it does so by using a mechanism that really isn't designed for that. Now the recorder mechanism is intertwined with the plugin mechanism, which it should have nothing to do with that. We shouldn't have to fix the loggers if we change how plugins work; that seems anti-modular to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll take a look. |
||
plugin = | ||
(defaultPluginDescriptor "LSPWindowShowMessageRecorder") | ||
{ pluginNotificationHandlers = mkPluginNotificationHandler SInitialized $ \_ _ _ -> do | ||
env <- getLspEnv | ||
liftIO $ writeIORef envRef $ Just env | ||
-- flush the backlog | ||
backLog <- liftIO $ atomicModifyIORef' backLogRef ([],) | ||
for_ (reverse backLog) $ sendMsg env | ||
} | ||
return (recorder, plugin) | ||
|
||
sendMsg :: MonadUnliftIO m => LanguageContextEnv config -> WithPriority Text -> m () | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't actually need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch - I'll drop the instance before @wz1000 gets a chance to abuse it :) |
||
sendMsg env WithPriority {..} = | ||
LSP.runLspT env $ | ||
LSP.sendNotification | ||
SWindowShowMessage | ||
ShowMessageParams | ||
{ _xtype = priorityToLsp priority, | ||
_message = payload | ||
} | ||
|
||
priorityToLsp :: Priority -> MessageType | ||
priorityToLsp = | ||
\case | ||
Debug -> MtLog | ||
Info -> MtInfo | ||
Warning -> MtWarning | ||
Error -> MtError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not necessarily an exception, right? It could be any other error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I copied this message over from an earlier exception site - will change to error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And indeed, since we send it as an error message, we don't need to tell the user again that it's an error. Including the issue link is maybe still helpful.