@@ -1,3 +1,4 @@
1+{-# LANGUAGE LambdaCase #-}
12{-# LANGUAGE OverloadedStrings #-}
23
34-- | Helpers for formatting of comments. This is low-level code, use
@@ -35,10 +36,10 @@ spitPrecedingComments ::
3536spitPrecedingComments ref = do
3637 gotSome <- handleCommentSeries (spitPrecedingComment ref)
3738 when gotSome $ do
39+ lastMark <- getSpanMark
3840 -- Insert a blank line between the preceding comments and the thing
3941 -- after them if there was a blank line in the input.
40− lastSpn <- fmap snd <$> getLastCommentSpan
41− when (needsNewlineBefore (getRealSrcSpan ref) lastSpn) newline
42+ when (needsNewlineBefore (getRealSrcSpan ref) lastMark) newline
4243
4344-- | Output all comments following an element at given location.
4445spitFollowingComments ::
@@ -73,14 +74,14 @@ spitPrecedingComment ::
7374 -- | AST element to attach comments to
7475 RealLocated a ->
7576 -- | Location of last comment in the series
76− Maybe RealSrcSpan ->
77+ Maybe SpanMark ->
7778 -- | Are we done?
7879 R Bool
79−spitPrecedingComment (L ref a) mlastSpn = do
80+spitPrecedingComment (L ref a) mlastMark = do
8081 let p (L l _) = realSrcSpanEnd l <= realSrcSpanStart ref
8182 withPoppedComment p $ \l comment -> do
8283 dirtyLine <-
83− case mlastSpn of
84+ case mlastMark of
8485 -- When the current line is dirty it means that something that can
8586 -- have comments attached to it is already on the line. To avoid
8687 -- problems with idempotence we cannot output the first comment
@@ -89,7 +90,7 @@ spitPrecedingComment (L ref a) mlastSpn = do
8990 -- an extra 'newline' in this case.
9091 Nothing -> isLineDirty -- only for very first preceding comment
9192 Just _ -> return False
92− when (dirtyLine || needsNewlineBefore l mlastSpn) newline
93+ when (dirtyLine || needsNewlineBefore l mlastMark) newline
9394 spitCommentNow l comment
9495 if theSameLinePre l ref && not (isModule a)
9596 then space
@@ -102,34 +103,34 @@ spitFollowingComment ::
102103 -- | AST element to attach comments to
103104 RealLocated a ->
104105 -- | Location of last comment in the series
105− Maybe RealSrcSpan ->
106+ Maybe SpanMark ->
106107 -- | Are we done?
107108 R Bool
108−spitFollowingComment (L ref a) mlastSpn = do
109+spitFollowingComment (L ref a) mlastMark = do
109110 mnSpn <- nextEltSpan
110111 -- Get first enclosing span that is not equal to reference span, i.e. it's
111112 -- truly something enclosing the AST element.
112113 meSpn <- getEnclosingSpan (/= ref)
113− withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastSpn) $ \l comment ->
114+ withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastMark) $ \l comment ->
114115 if theSameLinePost l ref && not (isModule a)
115116 then
116117 if isMultilineComment comment
117118 then space >> spitCommentNow l comment
118119 else spitCommentPending OnTheSameLine l comment
119120 else do
120− when (needsNewlineBefore l mlastSpn) $
121+ when (needsNewlineBefore l mlastMark) $
121122 registerPendingCommentLine OnNextLine ""
122123 spitCommentPending OnNextLine l comment
123124
124125-- | Output a single remaining comment from the comment stream.
125126spitRemainingComment ::
126127 -- | Location of last comment in the series
127− Maybe RealSrcSpan ->
128+ Maybe SpanMark ->
128129 -- | Are we done?
129130 R Bool
130−spitRemainingComment mlastSpn =
131+spitRemainingComment mlastMark =
131132 withPoppedComment (const True) $ \l comment -> do
132− when (needsNewlineBefore l mlastSpn) newline
133+ when (needsNewlineBefore l mlastMark) newline
133134 spitCommentNow l comment
134135 newline
135136
@@ -140,13 +141,13 @@ spitRemainingComment mlastSpn =
140141handleCommentSeries ::
141142 -- | Given location of previous comment, output the next comment
142143 -- returning 'True' if we're done
143− (Maybe RealSrcSpan -> R Bool) ->
144+ (Maybe SpanMark -> R Bool) ->
144145 -- | Whether we printed any comments
145146 R Bool
146147handleCommentSeries f = go False
147148 where
148149 go gotSome = do
149− done <- getLastCommentSpan >>= f . fmap snd
150+ done <- getSpanMark >>= f
150151 if done
151152 then return gotSome
152153 else go True
@@ -172,13 +173,13 @@ needsNewlineBefore ::
172173 -- | Current comment span
173174 RealSrcSpan ->
174175 -- | Last printed comment span
175− Maybe RealSrcSpan ->
176+ Maybe SpanMark ->
176177 Bool
177−needsNewlineBefore l mlastSpn =
178− case mlastSpn of
178+needsNewlineBefore l mlastMark =
179+ case spanMarkSpan <$> mlastMark of
179180 Nothing -> False
180− Just lastSpn ->
181− srcSpanStartLine l > srcSpanEndLine lastSpn + 1
181+ Just lastMark ->
182+ srcSpanStartLine l > srcSpanEndLine lastMark + 1
182183
183184-- | Is the preceding comment and AST element are on the same line?
184185theSameLinePre ::
@@ -209,11 +210,11 @@ commentFollowsElt ::
209210 -- | Location of enclosing AST element
210211 Maybe RealSrcSpan ->
211212 -- | Location of last comment in the series
212− Maybe RealSrcSpan ->
213+ Maybe SpanMark ->
213214 -- | Comment to test
214215 RealLocated Comment ->
215216 Bool
216−commentFollowsElt ref mnSpn meSpn mlastSpn (L l comment) =
217+commentFollowsElt ref mnSpn meSpn mlastMark (L l comment) =
217218 -- A comment follows a AST element if all 4 conditions are satisfied:
218219 goesAfter
219220 && logicallyFollows
@@ -255,9 +256,12 @@ commentFollowsElt ref mnSpn meSpn mlastSpn (L l comment) =
255256 >= abs (startColumn ref - startColumn l)
256257 )
257258 continuation =
258− case mlastSpn of
259− Nothing -> False
260− Just spn -> srcSpanEndLine spn + 1 == srcSpanStartLine l
259+ case mlastMark of
260+ Just (HaddockSpan _ spn) ->
261+ srcSpanEndLine spn + 1 == srcSpanStartLine l
262+ Just (CommentSpan spn) ->
263+ srcSpanEndLine spn + 1 == srcSpanStartLine l
264+ _ -> False
261265 lastInEnclosing =
262266 case meSpn of
263267 -- When there is no enclosing element, return false
@@ -281,7 +285,7 @@ spitCommentNow spn comment = do
281285 . fmap (txt . T.pack)
282286 . coerce
283287 $ comment
284− setLastCommentSpan Nothing spn
288+ setSpanMark (CommentSpan spn)
285289
286290-- | Output a 'Comment' at the end of correct line or after it depending on
287291-- 'CommentPosition'. Used for comments that may potentially follow on the
@@ -297,4 +301,4 @@ spitCommentPending position spn comment = do
297301 . fmap (registerPendingCommentLine position . T.pack)
298302 . coerce
299303 $ comment
300− setLastCommentSpan Nothing spn
304+ setSpanMark (CommentSpan spn)