Skip to content

Commit bccb44e

Browse files
committed
Move lspLogger to the ghcide library
1 parent c793e54 commit bccb44e

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

ghcide/ghcide.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ library
188188
Development.IDE.Plugin.CodeAction.ExactPrint
189189
Development.IDE.Plugin.HLS
190190
Development.IDE.Plugin.HLS.GhcIde
191+
Development.IDE.Plugin.LspLogger
191192
Development.IDE.Plugin.Test
192193
Development.IDE.Plugin.TypeLenses
193194

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Development.IDE.Plugin.LspLogger (lspLogger) where
2+
3+
import Control.Monad.Extra (whenJust)
4+
import Control.Monad.IO.Class
5+
import qualified Data.Aeson as A
6+
import Data.IORef
7+
import Development.IDE.Types.Logger
8+
import Ide.Types
9+
import qualified Language.LSP.Server as LSP
10+
import Language.LSP.Types
11+
12+
-- | A logger that sends messages to the LSP client
13+
lspLogger :: IO (Logger, PluginDescriptor a)
14+
lspLogger = do
15+
lspEnvRef <- newIORef Nothing
16+
let plugin = (defaultPluginDescriptor "lspLogging"){
17+
pluginNotificationHandlers =
18+
mkPluginNotificationHandler SInitialized $ \_ _ _ ->
19+
liftIO $ readIORef lspEnvRef >>= writeIORef lspEnvRef
20+
}
21+
logger = Logger $ \_p msg -> do
22+
env <- readIORef lspEnvRef
23+
whenJust env $ \env ->
24+
LSP.runLspT env (LSP.sendNotification (SCustomMethod "ghcide/log") (A.String msg))
25+
return (logger, plugin)

ghcide/test/exe/Main.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import Development.IDE.Core.Shake (Q (..))
3939
import qualified Development.IDE.Main as IDE
4040
import Development.IDE.GHC.Util
4141
import Development.IDE.Plugin.Completions.Types (extendImportCommandId)
42+
import Development.IDE.Plugin.LspLogger
4243
import Development.IDE.Plugin.TypeLenses (typeLensCommandId)
4344
import Development.IDE.Spans.Common
4445
import Development.IDE.Test (Cursor,
@@ -51,7 +52,7 @@ import Development.IDE.Test (Cursor,
5152
expectNoMoreDiagnostics,
5253
flushMessages,
5354
standardizeQuotes,
54-
waitForAction, lspLogger)
55+
waitForAction)
5556
import Development.IDE.Test.Runfiles
5657
import qualified Development.IDE.Types.Diagnostics as Diagnostics
5758
import Development.IDE.Types.Location

ghcide/test/src/Development/IDE/Test.hs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,19 @@ module Development.IDE.Test
2020
, standardizeQuotes
2121
, flushMessages
2222
, waitForAction
23-
, lspLogger
2423
) where
2524

2625
import Control.Applicative.Combinators
2726
import Control.Lens hiding (List)
2827
import Control.Monad
29-
import Control.Monad.Extra (whenJust)
3028
import Control.Monad.IO.Class
3129
import qualified Data.Aeson as A
3230
import Data.Bifunctor (second)
33-
import Data.IORef
3431
import qualified Data.Map.Strict as Map
3532
import Data.Maybe (fromJust)
3633
import qualified Data.Text as T
3734
import Development.IDE.Plugin.Test (TestRequest (..),
3835
WaitForIdeRuleResult)
39-
import Development.IDE.Types.Logger
40-
import Ide.Types
41-
import qualified Language.LSP.Server as LSP
4236
import Language.LSP.Test hiding (message)
4337
import qualified Language.LSP.Test as LspTest
4438
import Language.LSP.Types
@@ -206,18 +200,3 @@ waitForAction key TextDocumentIdentifier{_uri} = do
206200
case A.fromJSON e of
207201
A.Error e -> Left $ ResponseError InternalError (T.pack e) Nothing
208202
A.Success a -> pure a
209-
210-
-- lspLogger :: lspEnv a -> T.Text -> IO ()
211-
lspLogger :: IO (Logger, PluginDescriptor a)
212-
lspLogger = do
213-
lspEnvRef <- newIORef Nothing
214-
let plugin = (defaultPluginDescriptor "lspLogging"){
215-
pluginNotificationHandlers =
216-
mkPluginNotificationHandler SInitialized $ \_ _ _ ->
217-
liftIO $ readIORef lspEnvRef >>= writeIORef lspEnvRef
218-
}
219-
logger = Logger $ \_p msg -> do
220-
env <- readIORef lspEnvRef
221-
whenJust env $ \env ->
222-
LSP.runLspT env (LSP.sendNotification (SCustomMethod "ghcide/log") (A.String msg))
223-
return (logger, plugin)

0 commit comments

Comments
 (0)