@@ -81,6 +81,7 @@ import PrelNames (pRELUDE)
8181import System.FilePath
8282import System.IO (hClose )
8383import System.IO.Temp
84+ import Data.Maybe (catMaybes )
8485
8586descriptor :: PluginId -> PluginDescriptor
8687descriptor plId =
@@ -179,7 +180,10 @@ with all the dependencies. Unfortunately, the ModSummary objects that
179180GhcSessionDeps puts in the GHC session are not suitable for reuse since they
180181clear out the timestamps; this is done to avoid internal ghcide bugs and
181182can 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
0 commit comments