{-# LANGUAGE RecordWildCards #-}

-- | Pretty-printer for Haskell AST.
module Ormolu.Printer
  ( printModule,
    PrinterOpts (..),
  )
where

import Data.Text (Text)
import Ormolu.Config
import Ormolu.Parser.Result
import Ormolu.Printer.Combinators
import Ormolu.Printer.Meat.Module
import Ormolu.Printer.SpanStream
import Ormolu.Processing.Postprocess (postprocess)

-- | Render a module.
printModule ::
  -- | Result of parsing
  ParseResult ->
  PrinterOpts ->
  -- | Resulting rendition
  Text
printModule :: ParseResult -> PrinterOpts -> Text
printModule ParseResult {Bool
[([RealLocated Comment], Pragma)]
[Shebang]
Maybe (RealLocated Comment)
Text
HsModule GhcPs
Anns
CommentStream
prLiteralSuffix :: ParseResult -> Text
prLiteralPrefix :: ParseResult -> Text
prImportQualifiedPost :: ParseResult -> Bool
prUseRecordDot :: ParseResult -> Bool
prCommentStream :: ParseResult -> CommentStream
prPragmas :: ParseResult -> [([RealLocated Comment], Pragma)]
prShebangs :: ParseResult -> [Shebang]
prStackHeader :: ParseResult -> Maybe (RealLocated Comment)
prAnns :: ParseResult -> Anns
prParsedSource :: ParseResult -> HsModule GhcPs
prLiteralSuffix :: Text
prLiteralPrefix :: Text
prImportQualifiedPost :: Bool
prUseRecordDot :: Bool
prCommentStream :: CommentStream
prPragmas :: [([RealLocated Comment], Pragma)]
prShebangs :: [Shebang]
prStackHeader :: Maybe (RealLocated Comment)
prAnns :: Anns
prParsedSource :: HsModule GhcPs
..} PrinterOpts
printerOpts =
  Text
prLiteralPrefix Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
region Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
prLiteralSuffix
  where
    region :: Text
region =
      Text -> Text
postprocess (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$
        R ()
-> SpanStream
-> CommentStream
-> Anns
-> PrinterOpts
-> Bool
-> Text
runR
          ( Maybe (RealLocated Comment)
-> [Shebang]
-> [([RealLocated Comment], Pragma)]
-> Bool
-> HsModule GhcPs
-> R ()
p_hsModule
              Maybe (RealLocated Comment)
prStackHeader
              [Shebang]
prShebangs
              [([RealLocated Comment], Pragma)]
prPragmas
              Bool
prImportQualifiedPost
              HsModule GhcPs
prParsedSource
          )
          (HsModule GhcPs -> SpanStream
forall a. Data a => a -> SpanStream
mkSpanStream HsModule GhcPs
prParsedSource)
          CommentStream
prCommentStream
          Anns
prAnns
          PrinterOpts
printerOpts
          Bool
prUseRecordDot