----------------------------------------------------------------------------
-- |
-- Module      :  Prettyprinter.Combinators.Basic
-- Copyright   :  (c) Sergey Vinokurov 2018
-- License     :  Apache-2.0 (see LICENSE)
-- Maintainer  :  serg.foo@gmail.com
----------------------------------------------------------------------------

{-# LANGUAGE OverloadedStrings #-}

module Prettyprinter.Combinators.Basic
  ( (##)
  , ppListWithDelimSep
  ) where

import Data.Foldable
import Data.Semigroup as Semigroup
import Prettyprinter as PP

infixr 6 ##

(##) :: Doc ann -> Doc ann -> Doc ann
## :: forall ann. Doc ann -> Doc ann -> Doc ann
(##) Doc ann
x Doc ann
y = forall ann. Int -> Doc ann -> Doc ann
PP.nest Int
2 forall a b. (a -> b) -> a -> b
$ Doc ann
x forall a. Semigroup a => a -> a -> a
Semigroup.<> forall ann. Doc ann
PP.line forall a. Semigroup a => a -> a -> a
<> Doc ann
y

{-# INLINABLE ppListWithDelimSep #-}
ppListWithDelimSep
  :: forall f ann. Foldable f
  => Doc ann
  -> Doc ann
  -> Doc ann
  -> f (Doc ann)
  -> Doc ann
ppListWithDelimSep :: forall (f :: * -> *) ann.
Foldable f =>
Doc ann -> Doc ann -> Doc ann -> f (Doc ann) -> Doc ann
ppListWithDelimSep Doc ann
separator Doc ann
left Doc ann
right f (Doc ann)
xs =
  case forall (t :: * -> *) a. Foldable t => t a -> [a]
toList f (Doc ann)
xs of
    []   -> Doc ann
left forall a. Semigroup a => a -> a -> a
<> Doc ann
right
    [Doc ann
y]  -> forall ann. Doc ann -> Doc ann -> Doc ann
PP.flatAlt (Doc ann
left forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
y forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
right) (Doc ann
left forall a. Semigroup a => a -> a -> a
<> Doc ann
y forall a. Semigroup a => a -> a -> a
<> Doc ann
right)
    Doc ann
y:[Doc ann]
ys ->
      forall ann. Doc ann -> Doc ann
PP.align forall a b. (a -> b) -> a -> b
$
        forall ann. Doc ann -> Doc ann
PP.group forall a b. (a -> b) -> a -> b
$
          forall ann. Doc ann -> Doc ann -> Doc ann
PP.flatAlt
            (Doc ann
left forall ann. Doc ann -> Doc ann -> Doc ann
PP.<+> Doc ann
y forall a. Semigroup a => a -> a -> a
<> forall ann. Doc ann
PP.line' forall a. Semigroup a => a -> a -> a
<>
             forall ann. [Doc ann] -> Doc ann
PP.vcat [Doc ann]
fields forall a. Semigroup a => a -> a -> a
<> forall ann. Doc ann
PP.line forall a. Semigroup a => a -> a -> a
<>
             Doc ann
right)
            (Doc ann
left forall a. Semigroup a => a -> a -> a
PP.<> Doc ann
y forall a. Semigroup a => a -> a -> a
<> forall ann. [Doc ann] -> Doc ann
PP.vcat [Doc ann]
fields forall a. Semigroup a => a -> a -> a
<> Doc ann
right)
      where
        fields :: [Doc ann]
        fields :: [Doc ann]
fields = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\Doc ann
x -> Doc ann
separator forall ann. Doc ann -> Doc ann -> Doc ann
PP.<+> Doc ann
x) [Doc ann]
ys