{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}

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

import Data.Text (Text)
import Data.Text qualified as T
import Ormolu.Parser.Result
import Ormolu.Printer.Combinators
import Ormolu.Printer.Meat.Module
import Ormolu.Printer.SpanStream
import Ormolu.Processing.Common

-- | Render several source snippets.
printSnippets ::
  -- | Whether to print out debug information during printing
  Bool ->
  -- | Result of parsing
  [SourceSnippet] ->
  -- | Resulting rendition
  Text
printSnippets :: Bool -> [SourceSnippet] -> Text
printSnippets Bool
debug = [Text] -> Text
T.concat ([Text] -> Text)
-> ([SourceSnippet] -> [Text]) -> [SourceSnippet] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SourceSnippet -> Text) -> [SourceSnippet] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SourceSnippet -> Text
printSnippet
  where
    printSnippet :: SourceSnippet -> Text
printSnippet = \case
      ParsedSnippet ParseResult {Int
[([LComment], Pragma)]
Maybe LComment
EnumSet Extension
HsModule GhcPs
CommentStream
ModuleFixityMap
SourceType
prParsedSource :: HsModule GhcPs
prSourceType :: SourceType
prStackHeader :: Maybe LComment
prPragmas :: [([LComment], Pragma)]
prCommentStream :: CommentStream
prExtensions :: EnumSet Extension
prModuleFixityMap :: ModuleFixityMap
prIndent :: Int
prParsedSource :: ParseResult -> HsModule GhcPs
prSourceType :: ParseResult -> SourceType
prStackHeader :: ParseResult -> Maybe LComment
prPragmas :: ParseResult -> [([LComment], Pragma)]
prCommentStream :: ParseResult -> CommentStream
prExtensions :: ParseResult -> EnumSet Extension
prModuleFixityMap :: ParseResult -> ModuleFixityMap
prIndent :: ParseResult -> Int
..} ->
        Int -> Text -> Text
reindent Int
prIndent (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$
          R ()
-> SpanStream
-> CommentStream
-> SourceType
-> EnumSet Extension
-> ModuleFixityMap
-> Bool
-> Text
runR
            ( Maybe LComment -> [([LComment], Pragma)] -> HsModule GhcPs -> R ()
p_hsModule
                Maybe LComment
prStackHeader
                [([LComment], Pragma)]
prPragmas
                HsModule GhcPs
prParsedSource
            )
            (HsModule GhcPs -> SpanStream
forall a. Data a => a -> SpanStream
mkSpanStream HsModule GhcPs
prParsedSource)
            CommentStream
prCommentStream
            SourceType
prSourceType
            EnumSet Extension
prExtensions
            ModuleFixityMap
prModuleFixityMap
            Bool
debug
      RawSnippet Text
r -> Text
r