{-# LANGUAGE OverloadedStrings #-}

-- | Helpers for formatting of comments. This is low-level code, use
-- "Ormolu.Printer.Combinators" unless you know what you are doing.
module Ormolu.Printer.Comments
  ( spitPrecedingComments,
    spitFollowingComments,
    spitRemainingComments,
    spitStackHeader,
  )
where

import Control.Monad
import Data.Char (isSpace)
import Data.Coerce (coerce)
import Data.Data (Data)
import Data.List (isPrefixOf)
import qualified Data.List.NonEmpty as NE
import Data.List.NonEmpty (NonEmpty (..))
import qualified Data.Text as T
import Ormolu.Parser.CommentStream
import Ormolu.Printer.Internal
import Ormolu.Utils (isModule)
import SrcLoc

----------------------------------------------------------------------------
-- Top-level

-- | Output all preceding comments for an element at given location.
spitPrecedingComments ::
  Data a =>
  -- | AST element to attach comments to
  RealLocated a ->
  R ()
spitPrecedingComments :: RealLocated a -> R ()
spitPrecedingComments ref :: RealLocated a
ref = do
  Bool
gotSome <- (Maybe RealSrcSpan -> R Bool) -> R Bool
handleCommentSeries (RealLocated a -> Maybe RealSrcSpan -> R Bool
forall a. Data a => RealLocated a -> Maybe RealSrcSpan -> R Bool
spitPrecedingComment RealLocated a
ref)
  Bool -> R () -> R ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
gotSome (R () -> R ()) -> R () -> R ()
forall a b. (a -> b) -> a -> b
$ do
    -- Insert a blank line between the preceding comments and the thing
    -- after them if there was a blank line in the input.
    Maybe RealSrcSpan
lastSpn <- ((Maybe HaddockStyle, RealSrcSpan) -> RealSrcSpan)
-> Maybe (Maybe HaddockStyle, RealSrcSpan) -> Maybe RealSrcSpan
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe HaddockStyle, RealSrcSpan) -> RealSrcSpan
forall a b. (a, b) -> b
snd (Maybe (Maybe HaddockStyle, RealSrcSpan) -> Maybe RealSrcSpan)
-> R (Maybe (Maybe HaddockStyle, RealSrcSpan))
-> R (Maybe RealSrcSpan)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> R (Maybe (Maybe HaddockStyle, RealSrcSpan))
getLastCommentSpan
    Bool -> R () -> R ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RealSrcSpan -> Maybe RealSrcSpan -> Bool
needsNewlineBefore (RealLocated a -> RealSrcSpan
forall a. RealLocated a -> RealSrcSpan
getRealSrcSpan RealLocated a
ref) Maybe RealSrcSpan
lastSpn) R ()
newline

-- | Output all comments following an element at given location.
spitFollowingComments ::
  Data a =>
  -- | AST element of attach comments to
  RealLocated a ->
  R ()
spitFollowingComments :: RealLocated a -> R ()
spitFollowingComments ref :: RealLocated a
ref = do
  RealSrcSpan -> R ()
trimSpanStream (RealLocated a -> RealSrcSpan
forall a. RealLocated a -> RealSrcSpan
getRealSrcSpan RealLocated a
ref)
  R Bool -> R ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (R Bool -> R ()) -> R Bool -> R ()
forall a b. (a -> b) -> a -> b
$ (Maybe RealSrcSpan -> R Bool) -> R Bool
handleCommentSeries (RealLocated a -> Maybe RealSrcSpan -> R Bool
forall a. Data a => RealLocated a -> Maybe RealSrcSpan -> R Bool
spitFollowingComment RealLocated a
ref)

-- | Output all remaining comments in the comment stream.
spitRemainingComments :: R ()
spitRemainingComments :: R ()
spitRemainingComments = R Bool -> R ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (R Bool -> R ()) -> R Bool -> R ()
forall a b. (a -> b) -> a -> b
$ (Maybe RealSrcSpan -> R Bool) -> R Bool
handleCommentSeries Maybe RealSrcSpan -> R Bool
spitRemainingComment

-- | If there is a stack header in the comment stream, print it.
spitStackHeader :: R ()
spitStackHeader :: R ()
spitStackHeader = do
  let isStackHeader :: Comment -> Bool
isStackHeader (Comment (x :: String
x :| _)) =
        "stack" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isPrefixOf` (Char -> Bool) -> String -> String
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace (Int -> String -> String
forall a. Int -> [a] -> [a]
drop 2 String
x)
  Maybe (RealLocated Comment)
mstackHeader <- (RealLocated Comment -> Bool) -> R (Maybe (RealLocated Comment))
popComment (Comment -> Bool
isStackHeader (Comment -> Bool)
-> (RealLocated Comment -> Comment) -> RealLocated Comment -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RealLocated Comment -> Comment
forall a. RealLocated a -> a
unRealSrcSpan)
  Maybe (RealLocated Comment)
-> (RealLocated Comment -> R ()) -> R ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe (RealLocated Comment)
mstackHeader ((RealLocated Comment -> R ()) -> R ())
-> (RealLocated Comment -> R ()) -> R ()
forall a b. (a -> b) -> a -> b
$ \(L spn :: RealSrcSpan
spn x :: Comment
x) -> do
    RealSrcSpan -> Comment -> R ()
spitCommentNow RealSrcSpan
spn Comment
x
    R ()
newline

----------------------------------------------------------------------------
-- Single-comment functions

-- | Output a single preceding comment for an element at given location.
spitPrecedingComment ::
  Data a =>
  -- | AST element to attach comments to
  RealLocated a ->
  -- | Location of last comment in the series
  Maybe RealSrcSpan ->
  -- | Are we done?
  R Bool
spitPrecedingComment :: RealLocated a -> Maybe RealSrcSpan -> R Bool
spitPrecedingComment (L ref :: RealSrcSpan
ref a :: a
a) mlastSpn :: Maybe RealSrcSpan
mlastSpn = do
  let p :: GenLocated RealSrcSpan e -> Bool
p (L l :: RealSrcSpan
l _) = RealSrcSpan -> RealSrcLoc
realSrcSpanEnd RealSrcSpan
l RealSrcLoc -> RealSrcLoc -> Bool
forall a. Ord a => a -> a -> Bool
<= RealSrcSpan -> RealSrcLoc
realSrcSpanStart RealSrcSpan
ref
  (RealLocated Comment -> Bool)
-> (RealSrcSpan -> Comment -> R ()) -> R Bool
withPoppedComment RealLocated Comment -> Bool
forall e. GenLocated RealSrcSpan e -> Bool
p ((RealSrcSpan -> Comment -> R ()) -> R Bool)
-> (RealSrcSpan -> Comment -> R ()) -> R Bool
forall a b. (a -> b) -> a -> b
$ \l :: RealSrcSpan
l comment :: Comment
comment -> do
    Bool
dirtyLine <-
      case Maybe RealSrcSpan
mlastSpn of
        -- When the current line is dirty it means that something that can
        -- have comments attached to it is already on the line. To avoid
        -- problems with idempotence we cannot output the first comment
        -- immediately because it'll be attached to the previous element (on
        -- the same line) on the next run, so we play safe here and output
        -- an extra 'newline' in this case.
        Nothing -> R Bool
isLineDirty -- only for very first preceding comment
        Just _ -> Bool -> R Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
    Bool -> R () -> R ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool
dirtyLine Bool -> Bool -> Bool
|| RealSrcSpan -> Maybe RealSrcSpan -> Bool
needsNewlineBefore RealSrcSpan
l Maybe RealSrcSpan
mlastSpn) R ()
newline
    RealSrcSpan -> Comment -> R ()
spitCommentNow RealSrcSpan
l Comment
comment
    if RealSrcSpan -> RealSrcSpan -> Bool
theSameLinePre RealSrcSpan
l RealSrcSpan
ref Bool -> Bool -> Bool
&& Bool -> Bool
not (a -> Bool
forall a. Data a => a -> Bool
isModule a
a)
      then R ()
space
      else R ()
newline

-- | Output a comment that follows element at given location immediately on
-- the same line, if there is any.
spitFollowingComment ::
  Data a =>
  -- | AST element to attach comments to
  RealLocated a ->
  -- | Location of last comment in the series
  Maybe RealSrcSpan ->
  -- | Are we done?
  R Bool
spitFollowingComment :: RealLocated a -> Maybe RealSrcSpan -> R Bool
spitFollowingComment (L ref :: RealSrcSpan
ref a :: a
a) mlastSpn :: Maybe RealSrcSpan
mlastSpn = do
  Maybe RealSrcSpan
mnSpn <- R (Maybe RealSrcSpan)
nextEltSpan
  -- Get first enclosing span that is not equal to reference span, i.e. it's
  -- truly something enclosing the AST element.
  Maybe RealSrcSpan
meSpn <- (RealSrcSpan -> Bool) -> R (Maybe RealSrcSpan)
getEnclosingSpan (RealSrcSpan -> RealSrcSpan -> Bool
forall a. Eq a => a -> a -> Bool
/= RealSrcSpan
ref)
  (RealLocated Comment -> Bool)
-> (RealSrcSpan -> Comment -> R ()) -> R Bool
withPoppedComment (RealSrcSpan
-> Maybe RealSrcSpan
-> Maybe RealSrcSpan
-> Maybe RealSrcSpan
-> RealLocated Comment
-> Bool
commentFollowsElt RealSrcSpan
ref Maybe RealSrcSpan
mnSpn Maybe RealSrcSpan
meSpn Maybe RealSrcSpan
mlastSpn) ((RealSrcSpan -> Comment -> R ()) -> R Bool)
-> (RealSrcSpan -> Comment -> R ()) -> R Bool
forall a b. (a -> b) -> a -> b
$ \l :: RealSrcSpan
l comment :: Comment
comment ->
    if RealSrcSpan -> RealSrcSpan -> Bool
theSameLinePost RealSrcSpan
l RealSrcSpan
ref Bool -> Bool -> Bool
&& Bool -> Bool
not (a -> Bool
forall a. Data a => a -> Bool
isModule a
a)
      then
        if Comment -> Bool
isMultilineComment Comment
comment
          then R ()
space R () -> R () -> R ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> RealSrcSpan -> Comment -> R ()
spitCommentNow RealSrcSpan
l Comment
comment
          else CommentPosition -> RealSrcSpan -> Comment -> R ()
spitCommentPending CommentPosition
OnTheSameLine RealSrcSpan
l Comment
comment
      else do
        Bool -> R () -> R ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RealSrcSpan -> Maybe RealSrcSpan -> Bool
needsNewlineBefore RealSrcSpan
l Maybe RealSrcSpan
mlastSpn) (R () -> R ()) -> R () -> R ()
forall a b. (a -> b) -> a -> b
$
          CommentPosition -> Text -> R ()
registerPendingCommentLine CommentPosition
OnNextLine ""
        CommentPosition -> RealSrcSpan -> Comment -> R ()
spitCommentPending CommentPosition
OnNextLine RealSrcSpan
l Comment
comment

-- | Output a single remaining comment from the comment stream.
spitRemainingComment ::
  -- | Location of last comment in the series
  Maybe RealSrcSpan ->
  -- | Are we done?
  R Bool
spitRemainingComment :: Maybe RealSrcSpan -> R Bool
spitRemainingComment mlastSpn :: Maybe RealSrcSpan
mlastSpn =
  (RealLocated Comment -> Bool)
-> (RealSrcSpan -> Comment -> R ()) -> R Bool
withPoppedComment (Bool -> RealLocated Comment -> Bool
forall a b. a -> b -> a
const Bool
True) ((RealSrcSpan -> Comment -> R ()) -> R Bool)
-> (RealSrcSpan -> Comment -> R ()) -> R Bool
forall a b. (a -> b) -> a -> b
$ \l :: RealSrcSpan
l comment :: Comment
comment -> do
    Bool -> R () -> R ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (RealSrcSpan -> Maybe RealSrcSpan -> Bool
needsNewlineBefore RealSrcSpan
l Maybe RealSrcSpan
mlastSpn) R ()
newline
    RealSrcSpan -> Comment -> R ()
spitCommentNow RealSrcSpan
l Comment
comment
    R ()
newline

----------------------------------------------------------------------------
-- Helpers

-- | Output series of comments.
handleCommentSeries ::
  -- | Given location of previous comment, output the next comment
  -- returning 'True' if we're done
  (Maybe RealSrcSpan -> R Bool) ->
  -- | Whether we printed any comments
  R Bool
handleCommentSeries :: (Maybe RealSrcSpan -> R Bool) -> R Bool
handleCommentSeries f :: Maybe RealSrcSpan -> R Bool
f = Bool -> R Bool
go Bool
False
  where
    go :: Bool -> R Bool
go gotSome :: Bool
gotSome = do
      Bool
done <- R (Maybe (Maybe HaddockStyle, RealSrcSpan))
getLastCommentSpan R (Maybe (Maybe HaddockStyle, RealSrcSpan))
-> (Maybe (Maybe HaddockStyle, RealSrcSpan) -> R Bool) -> R Bool
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Maybe RealSrcSpan -> R Bool
f (Maybe RealSrcSpan -> R Bool)
-> (Maybe (Maybe HaddockStyle, RealSrcSpan) -> Maybe RealSrcSpan)
-> Maybe (Maybe HaddockStyle, RealSrcSpan)
-> R Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Maybe HaddockStyle, RealSrcSpan) -> RealSrcSpan)
-> Maybe (Maybe HaddockStyle, RealSrcSpan) -> Maybe RealSrcSpan
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe HaddockStyle, RealSrcSpan) -> RealSrcSpan
forall a b. (a, b) -> b
snd
      if Bool
done
        then Bool -> R Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
gotSome
        else Bool -> R Bool
go Bool
True

-- | Try to pop a comment using given predicate and if there is a comment
-- matching the predicate, print it out.
withPoppedComment ::
  -- | Comment predicate
  (RealLocated Comment -> Bool) ->
  -- | Printing function
  (RealSrcSpan -> Comment -> R ()) ->
  -- | Are we done?
  R Bool
withPoppedComment :: (RealLocated Comment -> Bool)
-> (RealSrcSpan -> Comment -> R ()) -> R Bool
withPoppedComment p :: RealLocated Comment -> Bool
p f :: RealSrcSpan -> Comment -> R ()
f = do
  Maybe (RealLocated Comment)
r <- (RealLocated Comment -> Bool) -> R (Maybe (RealLocated Comment))
popComment RealLocated Comment -> Bool
p
  case Maybe (RealLocated Comment)
r of
    Nothing -> Bool -> R Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
    Just (L l :: RealSrcSpan
l comment :: Comment
comment) -> Bool
False Bool -> R () -> R Bool
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ RealSrcSpan -> Comment -> R ()
f RealSrcSpan
l Comment
comment

-- | Determine if we need to insert a newline between current comment and
-- last printed comment.
needsNewlineBefore ::
  -- | Current comment span
  RealSrcSpan ->
  -- | Last printed comment span
  Maybe RealSrcSpan ->
  Bool
needsNewlineBefore :: RealSrcSpan -> Maybe RealSrcSpan -> Bool
needsNewlineBefore l :: RealSrcSpan
l mlastSpn :: Maybe RealSrcSpan
mlastSpn =
  case Maybe RealSrcSpan
mlastSpn of
    Nothing -> Bool
False
    Just lastSpn :: RealSrcSpan
lastSpn ->
      RealSrcSpan -> Int
srcSpanStartLine RealSrcSpan
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> RealSrcSpan -> Int
srcSpanEndLine RealSrcSpan
lastSpn Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1

-- | Is the preceding comment and AST element are on the same line?
theSameLinePre ::
  -- | Current comment span
  RealSrcSpan ->
  -- | AST element location
  RealSrcSpan ->
  Bool
theSameLinePre :: RealSrcSpan -> RealSrcSpan -> Bool
theSameLinePre l :: RealSrcSpan
l ref :: RealSrcSpan
ref =
  RealSrcSpan -> Int
srcSpanEndLine RealSrcSpan
l Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== RealSrcSpan -> Int
srcSpanStartLine RealSrcSpan
ref

-- | Is the following comment and AST element are on the same line?
theSameLinePost ::
  -- | Current comment span
  RealSrcSpan ->
  -- | AST element location
  RealSrcSpan ->
  Bool
theSameLinePost :: RealSrcSpan -> RealSrcSpan -> Bool
theSameLinePost l :: RealSrcSpan
l ref :: RealSrcSpan
ref =
  RealSrcSpan -> Int
srcSpanStartLine RealSrcSpan
l Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== RealSrcSpan -> Int
srcSpanEndLine RealSrcSpan
ref

-- | Determine if given comment follows AST element.
commentFollowsElt ::
  -- | Location of AST element
  RealSrcSpan ->
  -- | Location of next AST element
  Maybe RealSrcSpan ->
  -- | Location of enclosing AST element
  Maybe RealSrcSpan ->
  -- | Location of last comment in the series
  Maybe RealSrcSpan ->
  -- | Comment to test
  RealLocated Comment ->
  Bool
commentFollowsElt :: RealSrcSpan
-> Maybe RealSrcSpan
-> Maybe RealSrcSpan
-> Maybe RealSrcSpan
-> RealLocated Comment
-> Bool
commentFollowsElt ref :: RealSrcSpan
ref mnSpn :: Maybe RealSrcSpan
mnSpn meSpn :: Maybe RealSrcSpan
meSpn mlastSpn :: Maybe RealSrcSpan
mlastSpn (L l :: RealSrcSpan
l comment :: Comment
comment) =
  -- A comment follows a AST element if all 4 conditions are satisfied:
  Bool
goesAfter
    Bool -> Bool -> Bool
&& Bool
logicallyFollows
    Bool -> Bool -> Bool
&& Bool
noEltBetween
    Bool -> Bool -> Bool
&& (Bool
continuation Bool -> Bool -> Bool
|| Bool
lastInEnclosing Bool -> Bool -> Bool
|| Bool
supersedesParentElt)
  where
    -- 1) The comment starts after end of the AST element:
    goesAfter :: Bool
goesAfter =
      RealSrcSpan -> RealSrcLoc
realSrcSpanStart RealSrcSpan
l RealSrcLoc -> RealSrcLoc -> Bool
forall a. Ord a => a -> a -> Bool
>= RealSrcSpan -> RealSrcLoc
realSrcSpanEnd RealSrcSpan
ref
    -- 2) The comment logically belongs to the element, four cases:
    logicallyFollows :: Bool
logicallyFollows =
      RealSrcSpan -> RealSrcSpan -> Bool
theSameLinePost RealSrcSpan
l RealSrcSpan
ref -- a) it's on the same line
        Bool -> Bool -> Bool
|| Comment -> Bool
isPrevHaddock Comment
comment -- b) it's a Haddock string starting with -- ^
        Bool -> Bool -> Bool
|| Bool
continuation -- c) it's a continuation of a comment block
        Bool -> Bool -> Bool
|| Bool
lastInEnclosing -- d) it's the last element in the enclosing construct

    -- 3) There is no other AST element between this element and the comment:
    noEltBetween :: Bool
noEltBetween =
      case Maybe RealSrcSpan
mnSpn of
        Nothing -> Bool
True
        Just nspn :: RealSrcSpan
nspn ->
          RealSrcSpan -> RealSrcLoc
realSrcSpanStart RealSrcSpan
nspn RealSrcLoc -> RealSrcLoc -> Bool
forall a. Ord a => a -> a -> Bool
>= RealSrcSpan -> RealSrcLoc
realSrcSpanEnd RealSrcSpan
l
    -- 4) Less obvious: if column of comment is closer to the start of
    -- enclosing element, it probably related to that parent element, not to
    -- the current child element. This rule is important because otherwise
    -- all comments would end up assigned to closest inner elements, and
    -- parent elements won't have a chance to get any comments assigned to
    -- them. This is not OK because comments will get indented according to
    -- the AST elements they are attached to.
    --
    -- Skip this rule if the comment is a continuation of a comment block.
    supersedesParentElt :: Bool
supersedesParentElt =
      case Maybe RealSrcSpan
meSpn of
        Nothing -> Bool
True
        Just espn :: RealSrcSpan
espn ->
          let startColumn :: RealSrcSpan -> Int
startColumn = RealSrcLoc -> Int
srcLocCol (RealSrcLoc -> Int)
-> (RealSrcSpan -> RealSrcLoc) -> RealSrcSpan -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RealSrcSpan -> RealSrcLoc
realSrcSpanStart
           in RealSrcSpan -> Int
startColumn RealSrcSpan
espn Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> RealSrcSpan -> Int
startColumn RealSrcSpan
ref
                Bool -> Bool -> Bool
|| ( Int -> Int
forall a. Num a => a -> a
abs (RealSrcSpan -> Int
startColumn RealSrcSpan
espn Int -> Int -> Int
forall a. Num a => a -> a -> a
- RealSrcSpan -> Int
startColumn RealSrcSpan
l)
                       Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int -> Int
forall a. Num a => a -> a
abs (RealSrcSpan -> Int
startColumn RealSrcSpan
ref Int -> Int -> Int
forall a. Num a => a -> a -> a
- RealSrcSpan -> Int
startColumn RealSrcSpan
l)
                   )
    continuation :: Bool
continuation =
      case Maybe RealSrcSpan
mlastSpn of
        Nothing -> Bool
False
        Just spn :: RealSrcSpan
spn -> RealSrcSpan -> Int
srcSpanEndLine RealSrcSpan
spn Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== RealSrcSpan -> Int
srcSpanStartLine RealSrcSpan
l
    lastInEnclosing :: Bool
lastInEnclosing =
      case Maybe RealSrcSpan
meSpn of
        -- When there is no enclosing element, return false
        Nothing -> Bool
False
        -- When there is an enclosing element,
        Just espn :: RealSrcSpan
espn ->
          let -- Make sure that the comment is inside the enclosing element
              insideParent :: Bool
insideParent = RealSrcSpan -> RealSrcLoc
realSrcSpanEnd RealSrcSpan
l RealSrcLoc -> RealSrcLoc -> Bool
forall a. Ord a => a -> a -> Bool
<= RealSrcSpan -> RealSrcLoc
realSrcSpanEnd RealSrcSpan
espn
              -- And check if the next element is outside of the parent
              nextOutsideParent :: Bool
nextOutsideParent = case Maybe RealSrcSpan
mnSpn of
                Nothing -> Bool
True
                Just nspn :: RealSrcSpan
nspn -> RealSrcSpan -> RealSrcLoc
realSrcSpanEnd RealSrcSpan
espn RealSrcLoc -> RealSrcLoc -> Bool
forall a. Ord a => a -> a -> Bool
< RealSrcSpan -> RealSrcLoc
realSrcSpanStart RealSrcSpan
nspn
           in Bool
insideParent Bool -> Bool -> Bool
&& Bool
nextOutsideParent

-- | Output a 'Comment' immediately. This is a low-level printing function.
spitCommentNow :: RealSrcSpan -> Comment -> R ()
spitCommentNow :: RealSrcSpan -> Comment -> R ()
spitCommentNow spn :: RealSrcSpan
spn comment :: Comment
comment = do
  R () -> R ()
sitcc
    (R () -> R ()) -> (Comment -> R ()) -> Comment -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (R ()) -> R ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_
    (NonEmpty (R ()) -> R ())
-> (Comment -> NonEmpty (R ())) -> Comment -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. R () -> NonEmpty (R ()) -> NonEmpty (R ())
forall a. a -> NonEmpty a -> NonEmpty a
NE.intersperse R ()
newline
    (NonEmpty (R ()) -> NonEmpty (R ()))
-> (Comment -> NonEmpty (R ())) -> Comment -> NonEmpty (R ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> R ()) -> NonEmpty String -> NonEmpty (R ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> R ()
txt (Text -> R ()) -> (String -> Text) -> String -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack)
    (NonEmpty String -> NonEmpty (R ()))
-> (Comment -> NonEmpty String) -> Comment -> NonEmpty (R ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Comment -> NonEmpty String
forall a b. Coercible a b => a -> b
coerce
    (Comment -> R ()) -> Comment -> R ()
forall a b. (a -> b) -> a -> b
$ Comment
comment
  Maybe HaddockStyle -> RealSrcSpan -> R ()
setLastCommentSpan Maybe HaddockStyle
forall a. Maybe a
Nothing RealSrcSpan
spn

-- | Output a 'Comment' at the end of correct line or after it depending on
-- 'CommentPosition'. Used for comments that may potentially follow on the
-- same line as something we just rendered, but not immediately after it.
spitCommentPending :: CommentPosition -> RealSrcSpan -> Comment -> R ()
spitCommentPending :: CommentPosition -> RealSrcSpan -> Comment -> R ()
spitCommentPending position :: CommentPosition
position spn :: RealSrcSpan
spn comment :: Comment
comment = do
  let wrapper :: R () -> R ()
wrapper = case CommentPosition
position of
        OnTheSameLine -> R () -> R ()
sitcc
        OnNextLine -> R () -> R ()
forall a. a -> a
id
  R () -> R ()
wrapper
    (R () -> R ()) -> (Comment -> R ()) -> Comment -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [R ()] -> R ()
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, Monad m) =>
t (m a) -> m ()
sequence_
    ([R ()] -> R ()) -> (Comment -> [R ()]) -> Comment -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (R ()) -> [R ()]
forall a. NonEmpty a -> [a]
NE.toList
    (NonEmpty (R ()) -> [R ()])
-> (Comment -> NonEmpty (R ())) -> Comment -> [R ()]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> R ()) -> NonEmpty String -> NonEmpty (R ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (CommentPosition -> Text -> R ()
registerPendingCommentLine CommentPosition
position (Text -> R ()) -> (String -> Text) -> String -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack)
    (NonEmpty String -> NonEmpty (R ()))
-> (Comment -> NonEmpty String) -> Comment -> NonEmpty (R ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Comment -> NonEmpty String
forall a b. Coercible a b => a -> b
coerce
    (Comment -> R ()) -> Comment -> R ()
forall a b. (a -> b) -> a -> b
$ Comment
comment
  Maybe HaddockStyle -> RealSrcSpan -> R ()
setLastCommentSpan Maybe HaddockStyle
forall a. Maybe a
Nothing RealSrcSpan
spn