22
22
module Ide.Plugin.Eval where
23
23
24
24
import Control.Monad (void )
25
- import Control.Monad.Catch (finally )
25
+ import Control.Monad.Catch (MonadMask , bracket )
26
26
import Control.Monad.IO.Class (MonadIO (liftIO ))
27
27
import Control.Monad.Trans.Class (MonadTrans (lift ))
28
28
import Control.Monad.Trans.Except (ExceptT (.. ), runExceptT ,
@@ -31,7 +31,6 @@ import Data.Aeson (FromJSON, ToJSON, Value (Null),
31
31
toJSON )
32
32
import Data.Bifunctor (Bifunctor (first ))
33
33
import qualified Data.HashMap.Strict as Map
34
- import qualified Data.Rope.UTF16 as Rope
35
34
import Data.String (IsString (fromString ))
36
35
import Data.Text (Text )
37
36
import qualified Data.Text as T
@@ -58,9 +57,9 @@ import Ide.Plugin
58
57
import Ide.Types
59
58
import Language.Haskell.LSP.Core (LspFuncs (getVirtualFileFunc ))
60
59
import Language.Haskell.LSP.Types
61
- import Language.Haskell.LSP.VFS (VirtualFile ( .. ) )
60
+ import Language.Haskell.LSP.VFS (virtualFileText )
62
61
import PrelNames (pRELUDE )
63
- import System.IO (IOMode (WriteMode ), hClose , openFile )
62
+ import System.IO (Handle , IOMode (WriteMode ), hClose , openFile )
64
63
import System.IO.Extra (newTempFile )
65
64
66
65
descriptor :: PluginId -> PluginDescriptor
@@ -104,7 +103,7 @@ provider :: CodeLensProvider
104
103
provider lsp _state plId CodeLensParams {_textDocument} = response $ do
105
104
let TextDocumentIdentifier uri = _textDocument
106
105
contents <- liftIO $ getVirtualFileFunc lsp $ toNormalizedUri uri
107
- let text = Rope. toText . (_text :: VirtualFile -> Rope. Rope ) <$> contents
106
+ let text = virtualFileText <$> contents
108
107
let matches = extractMatches text
109
108
110
109
cmd <- liftIO $ mkLspCommand plId evalCommandName " Evaluate..." (Just [] )
@@ -147,7 +146,7 @@ runEvalCmd lsp state EvalParams {..} = response' $ do
147
146
let TextDocumentIdentifier {_uri} = module_
148
147
fp <- handleMaybe " uri" $ uriToFilePath' _uri
149
148
contents <- liftIO $ getVirtualFileFunc lsp $ toNormalizedUri _uri
150
- text <- handleMaybe " contents" $ Rope. toText . (_text :: VirtualFile -> Rope. Rope ) <$> contents
149
+ text <- handleMaybe " contents" $ virtualFileText <$> contents
151
150
152
151
session <-
153
152
liftIO
@@ -165,10 +164,7 @@ runEvalCmd lsp state EvalParams {..} = response' $ do
165
164
166
165
now <- liftIO getCurrentTime
167
166
168
- (temp, clean) <- liftIO newTempFile
169
- (tempLog, cleanLog) <- liftIO newTempFile
170
- hLog <- liftIO $ openFile tempLog WriteMode
171
- flip finally (liftIO $ hClose hLog >> cleanLog >> clean) $ do
167
+ withTempFile $ \ temp -> withTempFile $ \ tempLog -> withFile tempLog WriteMode $ \ hLog -> do
172
168
let modName = moduleName $ ms_mod ms
173
169
thisModuleTarget = Target (TargetFile fp Nothing ) False (Just (textToStringBuffer text, now))
174
170
@@ -297,3 +293,16 @@ setupDynFlagsForGHCiLike env dflags = do
297
293
`gopt_set` Opt_IgnoreOptimChanges
298
294
`gopt_set` Opt_IgnoreHpcChanges
299
295
initializePlugins env dflags4
296
+
297
+
298
+ withTempFile :: (MonadIO m , MonadMask m ) => (FilePath -> m a ) -> m a
299
+ withTempFile k = bracket alloc release (k . fst )
300
+ where
301
+ alloc = liftIO newTempFile
302
+ release = liftIO . snd
303
+
304
+ withFile :: (MonadMask m , MonadIO m ) => FilePath -> IOMode -> (Handle -> m b ) -> m b
305
+ withFile f mode = bracket alloc release
306
+ where
307
+ alloc = liftIO $ openFile f mode
308
+ release = liftIO . hClose
0 commit comments