{-# LANGUAGE CPP #-}
-- | Pretty printer utilities
module HIE.Bios.Ghc.Doc where


import GHC (DynFlags
#if __GLASGOW_HASKELL__ < 905
           , getPrintUnqual
#endif
           , pprCols, GhcMonad)
#if __GLASGOW_HASKELL__ >= 905
import GHC.Utils.Outputable
#endif

#if __GLASGOW_HASKELL__ >= 900
import GHC.Driver.Session (initSDocContext)
import GHC.Utils.Outputable (PprStyle, SDoc, runSDoc, neverQualify, )
import GHC.Utils.Ppr  (Mode(..), Doc, Style(..), renderStyle, style)
#else
import Outputable (PprStyle, SDoc, runSDoc, neverQualify, initSDocContext)
import Pretty (Mode(..), Doc, Style(..), renderStyle, style)
#endif

import HIE.Bios.Ghc.Gap (makeUserStyle, pageMode, oneLineMode)

#if __GLASGOW_HASKELL__ >= 905
getPrintUnqual :: Monad m => m NamePprCtx
getPrintUnqual = pure neverQualify
#endif

showPage :: DynFlags -> PprStyle -> SDoc -> String
showPage :: DynFlags -> PprStyle -> SDoc -> String
showPage DynFlags
dflag PprStyle
stl SDoc
sdoc = DynFlags -> Mode -> Doc -> String
showDocWith DynFlags
dflag Mode
pageMode forall a b. (a -> b) -> a -> b
$ SDoc -> SDocContext -> Doc
runSDoc SDoc
sdoc SDocContext
scontext
  where
    scontext :: SDocContext
scontext = DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflag PprStyle
stl

showOneLine :: DynFlags -> PprStyle -> SDoc -> String
showOneLine :: DynFlags -> PprStyle -> SDoc -> String
showOneLine DynFlags
dflag PprStyle
stl SDoc
sdoc = DynFlags -> Mode -> Doc -> String
showDocWith DynFlags
dflag Mode
oneLineMode forall a b. (a -> b) -> a -> b
$ SDoc -> SDocContext -> Doc
runSDoc SDoc
sdoc SDocContext
scontext
  where
    scontext :: SDocContext
scontext = DynFlags -> PprStyle -> SDocContext
initSDocContext DynFlags
dflag PprStyle
stl

getStyle :: (GhcMonad m) => DynFlags -> m PprStyle
getStyle :: forall (m :: * -> *). GhcMonad m => DynFlags -> m PprStyle
getStyle DynFlags
dflags = DynFlags -> PrintUnqualified -> PprStyle
makeUserStyle DynFlags
dflags forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). GhcMonad m => m PrintUnqualified
getPrintUnqual

styleUnqualified :: DynFlags -> PprStyle
styleUnqualified :: DynFlags -> PprStyle
styleUnqualified DynFlags
dflags = DynFlags -> PrintUnqualified -> PprStyle
makeUserStyle DynFlags
dflags PrintUnqualified
neverQualify

showDocWith :: DynFlags -> Mode -> Doc -> String
showDocWith :: DynFlags -> Mode -> Doc -> String
showDocWith DynFlags
dflags Mode
md = Style -> Doc -> String
renderStyle Style
mstyle
  where
    mstyle :: Style
mstyle = Style
style { mode :: Mode
mode = Mode
md, lineLength :: Int
lineLength = DynFlags -> Int
pprCols DynFlags
dflags }