@@ -242,13 +242,18 @@ commentFollowsElt ref mnSpn meSpn mlastSpn (L l comment) =
242242 Just spn -> srcSpanEndLine spn + 1 == srcSpanStartLine l
243243
244244 lastInEnclosing =
245− case (,) <$> meSpn <*> mnSpn of
245+ case meSpn of
246+ -- When there is no enclosing element, return false
246247 Nothing -> False
247− Just (espn, nspn) ->
248− -- Make sure that the comment is inside the parent
249− realSrcSpanEnd l <= realSrcSpanEnd espn
250− -- Check if the next element is outside of the parent
251− && realSrcSpanEnd espn < realSrcSpanStart nspn
248+ -- When there is an enclosing element,
249+ Just espn ->
250+ let -- Make sure that the comment is inside the enclosing element
251+ insideParent = realSrcSpanEnd l <= realSrcSpanEnd espn
252+ -- And check if the next element is outside of the parent
253+ nextOutsideParent = case mnSpn of
254+ Nothing -> True
255+ Just nspn -> realSrcSpanEnd espn < realSrcSpanStart nspn
256+ in insideParent && nextOutsideParent
252257
253258-- | Output a 'Comment'. This is a low-level printing function.
254259