{-# LANGUAGE NamedFieldPuns #-}
module HieDb.Dump where
import qualified Compat.HieDebug as HieDebug
import qualified Compat.HieTypes as HieTypes
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import Compat.HieTypes (HieFile (..))
import Control.Monad.IO.Class (MonadIO, liftIO)
import Data.Text (Text)
import HieDb.Compat
import HieDb.Types (NameCacheMonad)
import HieDb.Utils (withHieFile)
dump ::
(NameCacheMonad m, MonadIO m)
=> DynFlags
-> FilePath
-> m ()
dump :: forall (m :: * -> *).
(NameCacheMonad m, MonadIO m) =>
DynFlags -> FilePath -> m ()
dump DynFlags
dynFlags FilePath
hieFilePath = do
FilePath -> (HieFile -> m ()) -> m ()
forall (m :: * -> *) a.
(NameCacheMonad m, MonadIO m) =>
FilePath -> (HieFile -> m a) -> m a
withHieFile FilePath
hieFilePath ((HieFile -> m ()) -> m ()) -> (HieFile -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \HieFile{ HieASTs TypeIndex
hie_asts :: HieASTs TypeIndex
hie_asts :: HieFile -> HieASTs TypeIndex
hie_asts } -> do
let (HiePath
_, HieAST TypeIndex
astRoot) = Map HiePath (HieAST TypeIndex) -> (HiePath, HieAST TypeIndex)
forall k a. Map k a -> (k, a)
Map.findMin (Map HiePath (HieAST TypeIndex) -> (HiePath, HieAST TypeIndex))
-> Map HiePath (HieAST TypeIndex) -> (HiePath, HieAST TypeIndex)
forall a b. (a -> b) -> a -> b
$ HieASTs TypeIndex -> Map HiePath (HieAST TypeIndex)
forall a. HieASTs a -> Map HiePath (HieAST a)
HieTypes.getAsts HieASTs TypeIndex
hie_asts
IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ FilePath -> IO ()
putStrLn (FilePath -> IO ()) -> FilePath -> IO ()
forall a b. (a -> b) -> a -> b
$ DynFlags -> SDoc -> FilePath
showSDoc DynFlags
dynFlags (SDoc -> FilePath) -> SDoc -> FilePath
forall a b. (a -> b) -> a -> b
$ HieAST TypeIndex -> SDoc
forall a. Outputable a => HieAST a -> SDoc
HieDebug.ppHie HieAST TypeIndex
astRoot
sourceCode :: (NameCacheMonad m, MonadIO m) => FilePath -> m [Text]
sourceCode :: forall (m :: * -> *).
(NameCacheMonad m, MonadIO m) =>
FilePath -> m [Text]
sourceCode FilePath
hieFilePath =
FilePath -> (HieFile -> m [Text]) -> m [Text]
forall (m :: * -> *) a.
(NameCacheMonad m, MonadIO m) =>
FilePath -> (HieFile -> m a) -> m a
withHieFile FilePath
hieFilePath ((HieFile -> m [Text]) -> m [Text])
-> (HieFile -> m [Text]) -> m [Text]
forall a b. (a -> b) -> a -> b
$ \HieFile {ByteString
hie_hs_src :: ByteString
hie_hs_src :: HieFile -> ByteString
hie_hs_src} ->
[Text] -> m [Text]
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Text] -> m [Text]) -> [Text] -> m [Text]
forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines (Text -> [Text]) -> Text -> [Text]
forall a b. (a -> b) -> a -> b
$ ByteString -> Text
T.decodeUtf8 ByteString
hie_hs_src