-- | Printing related functions that depend on session state (DynFlags)
module GHC.Driver.Ppr
   ( showSDoc
   , showSDocUnsafe
   , showSDocForUser
   , showPpr
   , showPprUnsafe
   , printForUser
   )
where

import GHC.Prelude

import GHC.Driver.Session
import GHC.Unit.State

import GHC.Utils.Outputable
import GHC.Utils.Ppr       ( Mode(..) )

import System.IO ( Handle )

-- | Show a SDoc as a String with the default user style
showSDoc :: DynFlags -> SDoc -> String
showSDoc :: DynFlags -> SDoc -> String
showSDoc DynFlags
dflags SDoc
sdoc = SDocContext -> SDoc -> String
renderWithContext (DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflags PprStyle
defaultUserStyle) SDoc
sdoc

showPpr :: Outputable a => DynFlags -> a -> String
showPpr :: forall a. Outputable a => DynFlags -> a -> String
showPpr DynFlags
dflags a
thing = DynFlags -> SDoc -> String
showSDoc DynFlags
dflags (a -> SDoc
forall a. Outputable a => a -> SDoc
ppr a
thing)

-- | Allows caller to specify the NamePprCtx to use
showSDocForUser :: DynFlags -> UnitState -> NamePprCtx -> SDoc -> String
showSDocForUser :: DynFlags -> UnitState -> NamePprCtx -> SDoc -> String
showSDocForUser DynFlags
dflags UnitState
unit_state NamePprCtx
name_ppr_ctx SDoc
doc = SDocContext -> SDoc -> String
renderWithContext (DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflags PprStyle
sty) SDoc
doc'
   where
      sty :: PprStyle
sty  = NamePprCtx -> Depth -> PprStyle
mkUserStyle NamePprCtx
name_ppr_ctx Depth
AllTheWay
      doc' :: SDoc
doc' = UnitState -> SDoc -> SDoc
pprWithUnitState UnitState
unit_state SDoc
doc

printForUser :: DynFlags -> Handle -> NamePprCtx -> Depth -> SDoc -> IO ()
printForUser :: DynFlags -> Handle -> NamePprCtx -> Depth -> SDoc -> IO ()
printForUser DynFlags
dflags Handle
handle NamePprCtx
name_ppr_ctx Depth
depth SDoc
doc
  = SDocContext -> Mode -> Handle -> SDoc -> IO ()
printSDocLn SDocContext
ctx (Bool -> Mode
PageMode Bool
False) Handle
handle SDoc
doc
    where ctx :: SDocContext
ctx = DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflags (NamePprCtx -> Depth -> PprStyle
mkUserStyle NamePprCtx
name_ppr_ctx Depth
depth)