{-# 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)

{-| Pretty print Hie AST stored in given .hie file -}
dump ::
    (NameCacheMonad m, MonadIO m)
    => DynFlags
    -> FilePath -- ^ Path to .hie file
    -> m ()
dump :: forall (m :: * -> *).
(NameCacheMonad m, MonadIO m) =>
DynFlags -> FilePath -> m ()
dump DynFlags
dynFlags FilePath
hieFilePath = do
  forall (m :: * -> *) a.
(NameCacheMonad m, MonadIO m) =>
FilePath -> (HieFile -> m a) -> m a
withHieFile FilePath
hieFilePath forall a b. (a -> b) -> a -> b
$ \HieFile{ HieASTs TypeIndex
hie_asts :: HieFile -> HieASTs TypeIndex
hie_asts :: HieASTs TypeIndex
hie_asts } -> do
    let (HiePath
_, HieAST TypeIndex
astRoot) = forall k a. Map k a -> (k, a)
Map.findMin forall a b. (a -> b) -> a -> b
$ forall a. HieASTs a -> Map HiePath (HieAST a)
HieTypes.getAsts HieASTs TypeIndex
hie_asts
    forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ FilePath -> IO ()
putStrLn forall a b. (a -> b) -> a -> b
$ DynFlags -> SDoc -> FilePath
showSDoc DynFlags
dynFlags forall a b. (a -> b) -> a -> b
$ forall a. Outputable a => HieAST a -> SDoc
HieDebug.ppHie HieAST TypeIndex
astRoot

{-| Get lines of original source code from given .hie file -}
sourceCode :: (NameCacheMonad m, MonadIO m) => FilePath -> m [Text]
sourceCode :: forall (m :: * -> *).
(NameCacheMonad m, MonadIO m) =>
FilePath -> m [Text]
sourceCode FilePath
hieFilePath =
  forall (m :: * -> *) a.
(NameCacheMonad m, MonadIO m) =>
FilePath -> (HieFile -> m a) -> m a
withHieFile FilePath
hieFilePath forall a b. (a -> b) -> a -> b
$ \HieFile {ByteString
hie_hs_src :: HieFile -> ByteString
hie_hs_src :: ByteString
hie_hs_src} ->
    forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ Text -> [Text]
T.lines forall a b. (a -> b) -> a -> b
$ ByteString -> Text
T.decodeUtf8 ByteString
hie_hs_src