Skip to content

Commit 62861d4

Browse files
committed
Bug fixes
1 parent 9f5d7b6 commit 62861d4

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/Ide/Plugin/Eval.hs

+17-12
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import PrelNames (pRELUDE)
8181
import System.FilePath
8282
import System.IO (hClose)
8383
import System.IO.Temp
84+
import Data.Maybe (catMaybes)
8485

8586
descriptor :: PluginId -> PluginDescriptor
8687
descriptor plId =
@@ -179,7 +180,10 @@ with all the dependencies. Unfortunately, the ModSummary objects that
179180
GhcSessionDeps puts in the GHC session are not suitable for reuse since they
180181
clear out the timestamps; this is done to avoid internal ghcide bugs and
181182
can probably be relaxed so that plugins like Eval can reuse them. Once that's
182-
done, we want to switch back to GhcSessionDeps
183+
done, we want to switch back to GhcSessionDeps:
184+
185+
-- https://github.com/digital-asset/ghcide/pull/694
186+
183187
-}
184188
session <-
185189
liftIO $
@@ -240,6 +244,7 @@ done, we want to switch back to GhcSessionDeps
240244
let eval (stmt, l)
241245
| isStmt df stmt = do
242246
-- set up a custom interactive print function
247+
liftIO $ writeFile temp ""
243248
ctxt <- getContext
244249
setContext [IIDecl (simpleImportDecl $ moduleName pRELUDE)]
245250
let printFun = "let ghcideCustomShow x = Prelude.writeFile " <> show temp <> " (Prelude.show x)"
@@ -256,29 +261,29 @@ done, we want to switch back to GhcSessionDeps
256261
execLineNumber = l
257262
}
258263
res <- execStmt stmt opts
259-
str <- case res of
260-
ExecComplete (Left err) _ -> pure $ pad $ show err
264+
case res of
265+
ExecComplete (Left err) _ -> return $ Just $ T.pack $ pad $ show err
261266
ExecComplete (Right _) _ -> do
262267
out <- liftIO $ pad <$> readFile temp
263-
let forceIt = length out
264-
return $! forceIt `seq` out
265-
ExecBreak {} -> pure $ pad "breakpoints are not supported"
268+
-- Important to take the length in order to read the file eagerly
269+
return $! if length out == 0 then Nothing else Just (T.pack out)
270+
ExecBreak {} -> return $ Just $ T.pack $ pad "breakpoints are not supported"
266271

267-
let changes = [TextEdit editTarget $ T.pack str]
268-
return changes
269272
| isImport df stmt = do
270273
ctxt <- getContext
271274
idecl <- parseImportDecl stmt
272275
setContext $ IIDecl idecl : ctxt
273-
return []
276+
return Nothing
274277
| otherwise = do
275278
void $ runDecls stmt
276-
return []
279+
return Nothing
277280

278281
edits <- liftIO $ evalGhcEnv hscEnv' $ traverse (eval . first T.unpack) statements
279282

280-
let workspaceEditsMap = Map.fromList [(_uri, List $ concat edits)]
281-
let workspaceEdits = WorkspaceEdit (Just workspaceEditsMap) Nothing
283+
284+
let workspaceEditsMap = Map.fromList [(_uri, List [evalEdit])]
285+
workspaceEdits = WorkspaceEdit (Just workspaceEditsMap) Nothing
286+
evalEdit = TextEdit editTarget (T.intercalate "\n" $ catMaybes edits)
282287

283288
return (WorkspaceApplyEdit, ApplyWorkspaceEditParams workspaceEdits)
284289

test/functional/Eval.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ tests =
4949
testCase "Evaluation of expressions" $ goldenTest "T1.hs",
5050
testCase "Reevaluation of expressions" $ goldenTest "T2.hs",
5151
testCase "Evaluation of expressions w/ imports" $ goldenTest "T3.hs",
52-
testCase "Refresh an evaluation" $ goldenTest "T5.hs"
5352
testCase "Evaluation of expressions w/ lets" $ goldenTest "T4.hs",
53+
testCase "Refresh an evaluation" $ goldenTest "T5.hs",
54+
testCase "Refresh an evaluation w/ lets" $ goldenTest "T6.hs",
55+
testCase "Refresh a multiline evaluation" $ goldenTest "T7.hs"
5456
]
5557

5658
goldenTest :: FilePath -> IO ()

0 commit comments

Comments
 (0)