forked from haskell/haskell-language-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LSP window message log recorder (haskell#2750)
* failing to set the unsafe dynflags is an error * makeLspRecorder * include link to the issue tracker * avoid double popup * catch another ghc lib dir error
- Loading branch information
1 parent
395faf3
commit 87d9cbc
Showing
11 changed files
with
110 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
ghcide/src/Development/IDE/Plugin/LSPWindowShowMessageRecorder.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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 | ||
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 () | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters