module CabalGild.Unstable.Action.Render where

import qualified CabalGild.Unstable.Extra.FieldLine as FieldLine
import qualified CabalGild.Unstable.Extra.Name as Name
import qualified CabalGild.Unstable.Extra.SectionArg as SectionArg
import qualified CabalGild.Unstable.Type.Block as Block
import qualified CabalGild.Unstable.Type.Chunk as Chunk
import qualified CabalGild.Unstable.Type.Comment as Comment
import qualified CabalGild.Unstable.Type.Line as Line
import qualified Data.ByteString as ByteString
import qualified Data.Function as Function
import qualified Distribution.Compat.Lens as Lens
import qualified Distribution.Fields as Fields
import qualified Distribution.Parsec.Position as Position

-- | A wrapper around 'toByteString' to allow this to be composed with other
-- actions.
run ::
  (Applicative m) =>
  ([Fields.Field (Position.Position, [Comment.Comment p])], [Comment.Comment p]) ->
  m ByteString.ByteString
run :: forall (m :: * -> *) p.
Applicative m =>
([Field (Position, [Comment p])], [Comment p]) -> m ByteString
run = ByteString -> m ByteString
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> m ByteString)
-> (([Field (Position, [Comment p])], [Comment p]) -> ByteString)
-> ([Field (Position, [Comment p])], [Comment p])
-> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Field (Position, [Comment p])] -> [Comment p] -> ByteString)
-> ([Field (Position, [Comment p])], [Comment p]) -> ByteString
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry [Field (Position, [Comment p])] -> [Comment p] -> ByteString
forall p.
[Field (Position, [Comment p])] -> [Comment p] -> ByteString
toByteString

-- | Renders the given fields and comments to a byte string.
toByteString ::
  [Fields.Field (Position.Position, [Comment.Comment p])] ->
  [Comment.Comment p] ->
  ByteString.ByteString
toByteString :: forall p.
[Field (Position, [Comment p])] -> [Comment p] -> ByteString
toByteString [Field (Position, [Comment p])]
fs [Comment p]
cs =
  let i :: Int
i = Int
0 :: Int
   in Block -> ByteString
Block.toByteString
        (Block -> ByteString) -> (Block -> Block) -> Block -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter Block Block Bool Bool -> Bool -> Block -> Block
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Block Block Bool Bool
Lens' Block Bool
Block.lineBeforeLens Bool
False
        (Block -> Block) -> (Block -> Block) -> Block -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter Block Block Bool Bool -> Bool -> Block -> Block
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Block Block Bool Bool
Lens' Block Bool
Block.lineAfterLens Bool
True
        (Block -> ByteString) -> Block -> ByteString
forall a b. (a -> b) -> a -> b
$ Int -> [Field (Position, [Comment p])] -> Block
forall p. Int -> [Field (Position, [Comment p])] -> Block
fields Int
i [Field (Position, [Comment p])]
fs Block -> Block -> Block
forall a. Semigroup a => a -> a -> a
<> Int -> [Comment p] -> Block
forall a. Int -> [Comment a] -> Block
comments Int
i [Comment p]
cs

-- | Renders the given fields to a block at the given indentation level.
fields :: Int -> [Fields.Field (Position.Position, [Comment.Comment p])] -> Block.Block
fields :: forall p. Int -> [Field (Position, [Comment p])] -> Block
fields = (Field (Position, [Comment p]) -> Block)
-> [Field (Position, [Comment p])] -> Block
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((Field (Position, [Comment p]) -> Block)
 -> [Field (Position, [Comment p])] -> Block)
-> (Int -> Field (Position, [Comment p]) -> Block)
-> Int
-> [Field (Position, [Comment p])]
-> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Field (Position, [Comment p]) -> Block
forall p. Int -> Field (Position, [Comment p]) -> Block
field

-- | Renders the given field to a block at the given indentation level.
--
-- If a field only has one line and no comments, then it can be rendered all on
-- one line.
field :: Int -> Fields.Field (Position.Position, [Comment.Comment p]) -> Block.Block
field :: forall p. Int -> Field (Position, [Comment p]) -> Block
field Int
i Field (Position, [Comment p])
f = case Field (Position, [Comment p])
f of
  Fields.Field Name (Position, [Comment p])
n [FieldLine (Position, [Comment p])]
fls -> case [FieldLine (Position, [Comment p])]
fls of
    [FieldLine (Position, [Comment p])
fl]
      | [Comment p] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Comment p] -> Bool)
-> ((Position, [Comment p]) -> [Comment p])
-> (Position, [Comment p])
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Position, [Comment p]) -> [Comment p]
forall a b. (a, b) -> b
snd ((Position, [Comment p]) -> Bool)
-> (Position, [Comment p]) -> Bool
forall a b. (a -> b) -> a -> b
$ FieldLine (Position, [Comment p]) -> (Position, [Comment p])
forall a. FieldLine a -> a
FieldLine.annotation FieldLine (Position, [Comment p])
fl,
        (Position, [Comment p]) -> (Position, [Comment p]) -> Bool
forall cs. (Position, cs) -> (Position, cs) -> Bool
sameRow (Name (Position, [Comment p]) -> (Position, [Comment p])
forall a. Name a -> a
Name.annotation Name (Position, [Comment p])
n) (FieldLine (Position, [Comment p]) -> (Position, [Comment p])
forall a. FieldLine a -> a
FieldLine.annotation FieldLine (Position, [Comment p])
fl) ->
          Int -> [Comment p] -> Block
forall a. Int -> [Comment a] -> Block
comments Int
i ((Position, [Comment p]) -> [Comment p]
forall a b. (a, b) -> b
snd ((Position, [Comment p]) -> [Comment p])
-> (Position, [Comment p]) -> [Comment p]
forall a b. (a -> b) -> a -> b
$ Name (Position, [Comment p]) -> (Position, [Comment p])
forall a. Name a -> a
Name.annotation Name (Position, [Comment p])
n)
            Block -> Block -> Block
forall a. Semigroup a => a -> a -> a
<> ( Line -> Block
Block.fromLine
                   (Line -> Block) -> (Line -> Line) -> Line -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter Line Line Chunk Chunk -> (Chunk -> Chunk) -> Line -> Line
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
Lens.over ASetter Line Line Chunk Chunk
Lens' Line Chunk
Line.chunkLens (Chunk -> Chunk -> Chunk
forall a. Monoid a => a -> a -> a
mappend (Chunk -> Chunk -> Chunk) -> Chunk -> Chunk -> Chunk
forall a b. (a -> b) -> a -> b
$ Name (Position, [Comment p]) -> Chunk
forall a. Name a -> Chunk
name Name (Position, [Comment p])
n Chunk -> Chunk -> Chunk
forall a. Semigroup a => a -> a -> a
<> Chunk
Chunk.colon)
                   (Line -> Block) -> Line -> Block
forall a b. (a -> b) -> a -> b
$ Int -> FieldLine (Position, [Comment p]) -> Line
forall a. Int -> FieldLine a -> Line
fieldLine Int
i FieldLine (Position, [Comment p])
fl
               )
    [FieldLine (Position, [Comment p])]
_ ->
      ASetter Block Block Bool Bool -> Bool -> Block -> Block
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Block Block Bool Bool
Lens' Block Bool
Block.lineAfterLens (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ [FieldLine (Position, [Comment p])] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [FieldLine (Position, [Comment p])]
fls) (Block -> Block) -> Block -> Block
forall a b. (a -> b) -> a -> b
$
        Int -> [Comment p] -> Block
forall a. Int -> [Comment a] -> Block
comments Int
i ((Position, [Comment p]) -> [Comment p]
forall a b. (a, b) -> b
snd ((Position, [Comment p]) -> [Comment p])
-> (Position, [Comment p]) -> [Comment p]
forall a b. (a -> b) -> a -> b
$ Name (Position, [Comment p]) -> (Position, [Comment p])
forall a. Name a -> a
Name.annotation Name (Position, [Comment p])
n)
          Block -> Block -> Block
forall a. Semigroup a => a -> a -> a
<> Line -> Block
Block.fromLine
            Line.Line
              { indent :: Int
Line.indent = Int
i,
                chunk :: Chunk
Line.chunk = Name (Position, [Comment p]) -> Chunk
forall a. Name a -> Chunk
name Name (Position, [Comment p])
n Chunk -> Chunk -> Chunk
forall a. Semigroup a => a -> a -> a
<> Chunk
Chunk.colon
              }
          Block -> Block -> Block
forall a. Semigroup a => a -> a -> a
<> Int -> [FieldLine (Position, [Comment p])] -> Block
forall p q. Int -> [FieldLine (p, [Comment q])] -> Block
fieldLines (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [FieldLine (Position, [Comment p])]
fls
  Fields.Section Name (Position, [Comment p])
n [SectionArg (Position, [Comment p])]
sas [Field (Position, [Comment p])]
fs ->
    ASetter Block Block Bool Bool -> Bool -> Block -> Block
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Block Block Bool Bool
Lens' Block Bool
Block.lineBeforeLens (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Name (Position, [Comment p]) -> Bool
forall a. Name a -> Bool
Name.isElif Name (Position, [Comment p])
n Bool -> Bool -> Bool
|| Name (Position, [Comment p]) -> Bool
forall a. Name a -> Bool
Name.isElse Name (Position, [Comment p])
n)
      (Block -> Block) -> (Block -> Block) -> Block -> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter Block Block Bool Bool -> Bool -> Block -> Block
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Block Block Bool Bool
Lens' Block Bool
Block.lineAfterLens (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Name (Position, [Comment p]) -> Bool
forall a. Name a -> Bool
Name.isIf Name (Position, [Comment p])
n Bool -> Bool -> Bool
|| Name (Position, [Comment p]) -> Bool
forall a. Name a -> Bool
Name.isElif Name (Position, [Comment p])
n)
      (Block -> Block) -> Block -> Block
forall a b. (a -> b) -> a -> b
$ Int -> [Comment p] -> Block
forall a. Int -> [Comment a] -> Block
comments Int
i ((Position, [Comment p]) -> [Comment p]
forall a b. (a, b) -> b
snd ((Position, [Comment p]) -> [Comment p])
-> (Position, [Comment p]) -> [Comment p]
forall a b. (a -> b) -> a -> b
$ Name (Position, [Comment p]) -> (Position, [Comment p])
forall a. Name a -> a
Name.annotation Name (Position, [Comment p])
n)
        Block -> Block -> Block
forall a. Semigroup a => a -> a -> a
<> Int -> [Comment p] -> Block
forall a. Int -> [Comment a] -> Block
comments Int
i ((SectionArg (Position, [Comment p]) -> [Comment p])
-> [SectionArg (Position, [Comment p])] -> [Comment p]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ((Position, [Comment p]) -> [Comment p]
forall a b. (a, b) -> b
snd ((Position, [Comment p]) -> [Comment p])
-> (SectionArg (Position, [Comment p]) -> (Position, [Comment p]))
-> SectionArg (Position, [Comment p])
-> [Comment p]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SectionArg (Position, [Comment p]) -> (Position, [Comment p])
forall a. SectionArg a -> a
SectionArg.annotation) [SectionArg (Position, [Comment p])]
sas)
        Block -> Block -> Block
forall a. Semigroup a => a -> a -> a
<> Line -> Block
Block.fromLine
          Line.Line
            { indent :: Int
Line.indent = Int
i,
              chunk :: Chunk
Line.chunk = ASetter Chunk Chunk Bool Bool -> Bool -> Chunk -> Chunk
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Chunk Chunk Bool Bool
Lens' Chunk Bool
Chunk.spaceAfterLens Bool
True (Name (Position, [Comment p]) -> Chunk
forall a. Name a -> Chunk
name Name (Position, [Comment p])
n) Chunk -> Chunk -> Chunk
forall a. Semigroup a => a -> a -> a
<> [SectionArg (Position, [Comment p])] -> Chunk
forall a. [SectionArg a] -> Chunk
sectionArgs [SectionArg (Position, [Comment p])]
sas
            }
        Block -> Block -> Block
forall a. Semigroup a => a -> a -> a
<> ASetter Block Block Bool Bool -> Bool -> Block -> Block
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Block Block Bool Bool
Lens' Block Bool
Block.lineBeforeLens Bool
False (Int -> [Field (Position, [Comment p])] -> Block
forall p. Int -> [Field (Position, [Comment p])] -> Block
fields (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) [Field (Position, [Comment p])]
fs)

-- | Returns true if the two positions are on the same row.
sameRow :: (Position.Position, cs) -> (Position.Position, cs) -> Bool
sameRow :: forall cs. (Position, cs) -> (Position, cs) -> Bool
sameRow = (Int -> Int -> Bool)
-> ((Position, cs) -> Int)
-> (Position, cs)
-> (Position, cs)
-> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
Function.on Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
(==) (((Position, cs) -> Int)
 -> (Position, cs) -> (Position, cs) -> Bool)
-> ((Position, cs) -> Int)
-> (Position, cs)
-> (Position, cs)
-> Bool
forall a b. (a -> b) -> a -> b
$ Position -> Int
Position.positionRow (Position -> Int)
-> ((Position, cs) -> Position) -> (Position, cs) -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Position, cs) -> Position
forall a b. (a, b) -> a
fst

-- | Renders the given name to a chunk.
name :: Fields.Name a -> Chunk.Chunk
name :: forall a. Name a -> Chunk
name = ByteString -> Chunk
Chunk.fromByteString (ByteString -> Chunk) -> (Name a -> ByteString) -> Name a -> Chunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name a -> ByteString
forall a. Name a -> ByteString
Name.value

-- | Renders the given field lines to a block at the given indentation level.
fieldLines :: Int -> [Fields.FieldLine (p, [Comment.Comment q])] -> Block.Block
fieldLines :: forall p q. Int -> [FieldLine (p, [Comment q])] -> Block
fieldLines = (FieldLine (p, [Comment q]) -> Block)
-> [FieldLine (p, [Comment q])] -> Block
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((FieldLine (p, [Comment q]) -> Block)
 -> [FieldLine (p, [Comment q])] -> Block)
-> (Int -> FieldLine (p, [Comment q]) -> Block)
-> Int
-> [FieldLine (p, [Comment q])]
-> Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> FieldLine (p, [Comment q]) -> Block
forall p q. Int -> FieldLine (p, [Comment q]) -> Block
fieldLineC

-- | Renders the given field line and its comments to a block at the given
-- indentation level.
fieldLineC :: Int -> Fields.FieldLine (p, [Comment.Comment q]) -> Block.Block
fieldLineC :: forall p q. Int -> FieldLine (p, [Comment q]) -> Block
fieldLineC Int
i FieldLine (p, [Comment q])
fl =
  Int -> [Comment q] -> Block
forall a. Int -> [Comment a] -> Block
comments Int
i ((p, [Comment q]) -> [Comment q]
forall a b. (a, b) -> b
snd ((p, [Comment q]) -> [Comment q])
-> (p, [Comment q]) -> [Comment q]
forall a b. (a -> b) -> a -> b
$ FieldLine (p, [Comment q]) -> (p, [Comment q])
forall a. FieldLine a -> a
FieldLine.annotation FieldLine (p, [Comment q])
fl)
    Block -> Block -> Block
forall a. Semigroup a => a -> a -> a
<> Line -> Block
Block.fromLine (Int -> FieldLine (p, [Comment q]) -> Line
forall a. Int -> FieldLine a -> Line
fieldLine Int
i FieldLine (p, [Comment q])
fl)

-- | Renders the given field line to a line at the given indentation level.
fieldLine :: Int -> Fields.FieldLine a -> Line.Line
fieldLine :: forall a. Int -> FieldLine a -> Line
fieldLine Int
i =
  Int -> Chunk -> Line
Line.Line Int
i
    (Chunk -> Line) -> (FieldLine a -> Chunk) -> FieldLine a -> Line
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter Chunk Chunk Bool Bool -> Bool -> Chunk -> Chunk
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Chunk Chunk Bool Bool
Lens' Chunk Bool
Chunk.spaceBeforeLens Bool
True
    (Chunk -> Chunk) -> (FieldLine a -> Chunk) -> FieldLine a -> Chunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Chunk
Chunk.fromByteString
    (ByteString -> Chunk)
-> (FieldLine a -> ByteString) -> FieldLine a -> Chunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FieldLine a -> ByteString
forall a. FieldLine a -> ByteString
FieldLine.value

-- | Renders the given section arguments to a chunk. Note that comments are
-- ignored. In practice this isn't a problem because section arguments can't
-- have comments attached anyway.
sectionArgs :: [Fields.SectionArg a] -> Chunk.Chunk
sectionArgs :: forall a. [SectionArg a] -> Chunk
sectionArgs = ASetter Chunk Chunk Bool Bool -> Bool -> Chunk -> Chunk
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Chunk Chunk Bool Bool
Lens' Chunk Bool
Chunk.spaceBeforeLens Bool
True (Chunk -> Chunk)
-> ([SectionArg a] -> Chunk) -> [SectionArg a] -> Chunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (SectionArg a -> Chunk) -> [SectionArg a] -> Chunk
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap SectionArg a -> Chunk
forall a. SectionArg a -> Chunk
sectionArg

-- | Renders the given section argument to a chunk.
sectionArg :: Fields.SectionArg a -> Chunk.Chunk
sectionArg :: forall a. SectionArg a -> Chunk
sectionArg SectionArg a
sa = case SectionArg a
sa of
  Fields.SecArgName a
_ ByteString
bs ->
    ASetter Chunk Chunk Bool Bool -> Bool -> Chunk -> Chunk
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Chunk Chunk Bool Bool
Lens' Chunk Bool
Chunk.spaceBeforeLens Bool
True
      (Chunk -> Chunk) -> (Chunk -> Chunk) -> Chunk -> Chunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter Chunk Chunk Bool Bool -> Bool -> Chunk -> Chunk
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Chunk Chunk Bool Bool
Lens' Chunk Bool
Chunk.spaceAfterLens Bool
True
      (Chunk -> Chunk) -> Chunk -> Chunk
forall a b. (a -> b) -> a -> b
$ ByteString -> Chunk
Chunk.fromByteString ByteString
bs
  Fields.SecArgStr a
_ ByteString
bs ->
    ASetter Chunk Chunk Bool Bool -> Bool -> Chunk -> Chunk
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Chunk Chunk Bool Bool
Lens' Chunk Bool
Chunk.spaceBeforeLens Bool
True
      (Chunk -> Chunk) -> (ByteString -> Chunk) -> ByteString -> Chunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter Chunk Chunk Bool Bool -> Bool -> Chunk -> Chunk
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Chunk Chunk Bool Bool
Lens' Chunk Bool
Chunk.spaceAfterLens Bool
True
      (Chunk -> Chunk) -> (ByteString -> Chunk) -> ByteString -> Chunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Chunk
Chunk.fromByteString
      (ByteString -> Chunk)
-> (ByteString -> ByteString) -> ByteString -> Chunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> Word8 -> ByteString)
-> Word8 -> ByteString -> ByteString
forall a b c. (a -> b -> c) -> b -> a -> c
flip ByteString -> Word8 -> ByteString
ByteString.snoc Word8
0x22
      (ByteString -> Chunk) -> ByteString -> Chunk
forall a b. (a -> b) -> a -> b
$ Word8 -> ByteString -> ByteString
ByteString.cons Word8
0x22 ByteString
bs
  Fields.SecArgOther a
_ ByteString
bs ->
    let b :: Bool
b =
          ByteString
bs ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8 -> ByteString
ByteString.singleton Word8
0x21 -- !
            Bool -> Bool -> Bool
&& ByteString
bs ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8 -> ByteString
ByteString.singleton Word8
0x28 -- (
            Bool -> Bool -> Bool
&& ByteString
bs ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
/= Word8 -> ByteString
ByteString.singleton Word8
0x29 -- )
     in ASetter Chunk Chunk Bool Bool -> Bool -> Chunk -> Chunk
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Chunk Chunk Bool Bool
Lens' Chunk Bool
Chunk.spaceBeforeLens Bool
b
          (Chunk -> Chunk) -> (Chunk -> Chunk) -> Chunk -> Chunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ASetter Chunk Chunk Bool Bool -> Bool -> Chunk -> Chunk
forall s t a b. ASetter s t a b -> b -> s -> t
Lens.set ASetter Chunk Chunk Bool Bool
Lens' Chunk Bool
Chunk.spaceAfterLens Bool
b
          (Chunk -> Chunk) -> Chunk -> Chunk
forall a b. (a -> b) -> a -> b
$ ByteString -> Chunk
Chunk.fromByteString ByteString
bs

-- | Renders the given comments to a block at the given indentation level.
comments :: Int -> [Comment.Comment a] -> Block.Block
comments :: forall a. Int -> [Comment a] -> Block
comments Int
i [Comment a]
cs = Block
forall a. Monoid a => a
mempty {Block.lines = fmap (comment i) cs}

-- | Renders the given comment to a line at the given indentation level.
comment :: Int -> Comment.Comment a -> Line.Line
comment :: forall a. Int -> Comment a -> Line
comment Int
i =
  Int -> Chunk -> Line
Line.Line Int
i
    (Chunk -> Line) -> (Comment a -> Chunk) -> Comment a -> Line
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Chunk
Chunk.fromByteString
    (ByteString -> Chunk)
-> (Comment a -> ByteString) -> Comment a -> Chunk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString -> ByteString
forall a. Monoid a => a -> a -> a
mappend ByteString
Comment.delimiter
    (ByteString -> ByteString)
-> (Comment a -> ByteString) -> Comment a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Comment a -> ByteString
forall a. Comment a -> ByteString
Comment.value