From 5b0cca0353b95587def6139f016a3a7df2567c58 Mon Sep 17 00:00:00 2001 From: Nikolay Yakimov Date: Thu, 2 Jul 2015 21:42:48 +0300 Subject: [PATCH] Added withMappedFile utility function Works pretty much as `withTempFile`, except looks mapping up and uses temp. file only when necessary. --- Language/Haskell/GhcMod/FileMappingUtils.hs | 22 +++++++++++++++++++++ Language/Haskell/GhcMod/HomeModuleGraph.hs | 3 +-- ghc-mod.cabal | 1 + 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 Language/Haskell/GhcMod/FileMappingUtils.hs diff --git a/Language/Haskell/GhcMod/FileMappingUtils.hs b/Language/Haskell/GhcMod/FileMappingUtils.hs new file mode 100644 index 0000000..ee26417 --- /dev/null +++ b/Language/Haskell/GhcMod/FileMappingUtils.hs @@ -0,0 +1,22 @@ +module Language.Haskell.GhcMod.FileMappingUtils where + +import Language.Haskell.GhcMod.Types +import Language.Haskell.GhcMod.Monad.Types + +import System.IO +import System.FilePath +import System.Directory + +withMappedFile :: (IOish m, GmState m, GmEnv m) => forall a. FilePath -> (FilePath -> m a) -> m a +withMappedFile file action = lookupMMappedFile file >>= runWithFile + where + runWithFile (Just (RedirectedMapping to)) = action to + runWithFile (Just (MemoryMapping (Just src))) = do + crdl <- cradle + (fp,hndl) <- liftIO $ openTempFile (cradleTempDir crdl) (takeBaseName file) + liftIO $ hPutStr hndl src + liftIO $ hClose hndl + result <- action fp + liftIO $ removeFile fp + return result + runWithFile _ = action file diff --git a/Language/Haskell/GhcMod/HomeModuleGraph.hs b/Language/Haskell/GhcMod/HomeModuleGraph.hs index ce6fcc2..c3fa6dc 100644 --- a/Language/Haskell/GhcMod/HomeModuleGraph.hs +++ b/Language/Haskell/GhcMod/HomeModuleGraph.hs @@ -61,8 +61,7 @@ import Language.Haskell.GhcMod.Logger import Language.Haskell.GhcMod.Monad.Types import Language.Haskell.GhcMod.Types import Language.Haskell.GhcMod.Gap (parseModuleHeader) - -import System.IO +import Language.Haskell.GhcMod.FileMappingUtils -- | Turn module graph into a graphviz dot file -- diff --git a/ghc-mod.cabal b/ghc-mod.cabal index 8336665..5b5b547 100644 --- a/ghc-mod.cabal +++ b/ghc-mod.cabal @@ -106,6 +106,7 @@ Library Language.Haskell.GhcMod.DynFlags Language.Haskell.GhcMod.Error Language.Haskell.GhcMod.FileMapping + Language.Haskell.GhcMod.FileMappingUtils Language.Haskell.GhcMod.FillSig Language.Haskell.GhcMod.Find Language.Haskell.GhcMod.Flag