@@ -1,3 +1,4 @@
1+{-# LANGUAGE LambdaCase #-}
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3-- | Helpers for formatting of comments. This is low-level code, use
4-- | Helpers for formatting of comments. This is low-level code, use
@@ -35,10 +36,10 @@ spitPrecedingComments ::
35spitPrecedingComments ref = do
36spitPrecedingComments ref = do
36 gotSome <- handleCommentSeries (spitPrecedingComment ref)
37 gotSome <- handleCommentSeries (spitPrecedingComment ref)
37 when gotSome $ do
38 when gotSome $ do
39+ lastMark <- getSpanMark
38 -- Insert a blank line between the preceding comments and the thing
40 -- Insert a blank line between the preceding comments and the thing
39 -- after them if there was a blank line in the input.
41 -- 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
43-- | Output all comments following an element at given location.
44-- | Output all comments following an element at given location.
44spitFollowingComments ::
45spitFollowingComments ::
@@ -73,14 +74,14 @@ spitPrecedingComment ::
73 -- | AST element to attach comments to
74 -- | AST element to attach comments to
74 RealLocated a ->
75 RealLocated a ->
75 -- | Location of last comment in the series
76 -- | Location of last comment in the series
76− Maybe RealSrcSpan ->
77+ Maybe SpanMark ->
77 -- | Are we done?
78 -- | Are we done?
79−spitPrecedingComment (L ref a) mlastSpn = do
80+spitPrecedingComment (L ref a) mlastMark = do
80 let p (L l _) = realSrcSpanEnd l <= realSrcSpanStart ref
81 let p (L l _) = realSrcSpanEnd l <= realSrcSpanStart ref
81 withPoppedComment p $ \l comment -> do
82 withPoppedComment p $ \l comment -> do
82 dirtyLine <-
83 dirtyLine <-
83− case mlastSpn of
84+ case mlastMark of
84 -- When the current line is dirty it means that something that can
85 -- When the current line is dirty it means that something that can
85 -- have comments attached to it is already on the line. To avoid
86 -- have comments attached to it is already on the line. To avoid
86 -- problems with idempotence we cannot output the first comment
87 -- problems with idempotence we cannot output the first comment
@@ -89,7 +90,7 @@ spitPrecedingComment (L ref a) mlastSpn = do
89 -- an extra 'newline' in this case.
90 -- an extra 'newline' in this case.
90 Nothing -> isLineDirty -- only for very first preceding comment
91 Nothing -> isLineDirty -- only for very first preceding comment
91 Just _ -> return False
92 Just _ -> return False
92− when (dirtyLine || needsNewlineBefore l mlastSpn) newline
93+ when (dirtyLine || needsNewlineBefore l mlastMark) newline
93 spitCommentNow l comment
94 spitCommentNow l comment
94 if theSameLinePre l ref && not (isModule a)
95 if theSameLinePre l ref && not (isModule a)
95 then space
96 then space
@@ -102,34 +103,34 @@ spitFollowingComment ::
102 -- | AST element to attach comments to
103 -- | AST element to attach comments to
103 RealLocated a ->
104 RealLocated a ->
104 -- | Location of last comment in the series
105 -- | Location of last comment in the series
105− Maybe RealSrcSpan ->
106+ Maybe SpanMark ->
106 -- | Are we done?
107 -- | Are we done?
108−spitFollowingComment (L ref a) mlastSpn = do
109+spitFollowingComment (L ref a) mlastMark = do
109 mnSpn <- nextEltSpan
110 mnSpn <- nextEltSpan
110 -- Get first enclosing span that is not equal to reference span, i.e. it's
111 -- Get first enclosing span that is not equal to reference span, i.e. it's
111 -- truly something enclosing the AST element.
112 -- truly something enclosing the AST element.
112 meSpn <- getEnclosingSpan (/= ref)
113 meSpn <- getEnclosingSpan (/= ref)
113− withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastSpn) $ \l comment ->
114+ withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastMark) $ \l comment ->
114 if theSameLinePost l ref && not (isModule a)
115 if theSameLinePost l ref && not (isModule a)
116 if isMultilineComment comment
117 if isMultilineComment comment
117 then space >> spitCommentNow l comment
118 then space >> spitCommentNow l comment
118 else spitCommentPending OnTheSameLine l comment
119 else spitCommentPending OnTheSameLine l comment
120− when (needsNewlineBefore l mlastSpn) $
121+ when (needsNewlineBefore l mlastMark) $
121 registerPendingCommentLine OnNextLine ""
122 registerPendingCommentLine OnNextLine ""
122 spitCommentPending OnNextLine l comment
123 spitCommentPending OnNextLine l comment
124-- | Output a single remaining comment from the comment stream.
125-- | Output a single remaining comment from the comment stream.
125spitRemainingComment ::
126spitRemainingComment ::
126 -- | Location of last comment in the series
127 -- | Location of last comment in the series
127− Maybe RealSrcSpan ->
128+ Maybe SpanMark ->
128 -- | Are we done?
129 -- | Are we done?
130−spitRemainingComment mlastSpn =
131+spitRemainingComment mlastMark =
131 withPoppedComment (const True) $ \l comment -> do
132 withPoppedComment (const True) $ \l comment -> do
132− when (needsNewlineBefore l mlastSpn) newline
133+ when (needsNewlineBefore l mlastMark) newline
133 spitCommentNow l comment
134 spitCommentNow l comment
@@ -140,13 +141,13 @@ spitRemainingComment mlastSpn =
140handleCommentSeries ::
141handleCommentSeries ::
141 -- | Given location of previous comment, output the next comment
142 -- | Given location of previous comment, output the next comment
142 -- returning 'True' if we're done
143 -- returning 'True' if we're done
143− (Maybe RealSrcSpan -> R Bool) ->
144+ (Maybe SpanMark -> R Bool) ->
144 -- | Whether we printed any comments
145 -- | Whether we printed any comments
146handleCommentSeries f = go False
147handleCommentSeries f = go False
148 go gotSome = do
149 go gotSome = do
149− done <- getLastCommentSpan >>= f . fmap snd
150+ done <- getSpanMark >>= f
151 then return gotSome
152 then return gotSome
152 else go True
153 else go True
@@ -172,13 +173,13 @@ needsNewlineBefore ::
172 -- | Current comment span
173 -- | Current comment span
173 RealSrcSpan ->
174 RealSrcSpan ->
174 -- | Last printed comment span
175 -- | Last printed comment span
175− Maybe RealSrcSpan ->
176+ Maybe SpanMark ->
177−needsNewlineBefore l mlastSpn =
178+needsNewlineBefore l mlastMark =
178− case mlastSpn of
179+ case spanMarkSpan <$> mlastMark of
179 Nothing -> False
180 Nothing -> False
180− Just lastSpn ->
181+ Just lastMark ->
181− srcSpanStartLine l > srcSpanEndLine lastSpn + 1
182+ srcSpanStartLine l > srcSpanEndLine lastMark + 1
183-- | Is the preceding comment and AST element are on the same line?
184-- | Is the preceding comment and AST element are on the same line?
184theSameLinePre ::
185theSameLinePre ::
@@ -209,11 +210,11 @@ commentFollowsElt ::
209 -- | Location of enclosing AST element
210 -- | Location of enclosing AST element
210 Maybe RealSrcSpan ->
211 Maybe RealSrcSpan ->
211 -- | Location of last comment in the series
212 -- | Location of last comment in the series
212− Maybe RealSrcSpan ->
213+ Maybe SpanMark ->
213 -- | Comment to test
214 -- | Comment to test
214 RealLocated Comment ->
215 RealLocated Comment ->
216−commentFollowsElt ref mnSpn meSpn mlastSpn (L l comment) =
217+commentFollowsElt ref mnSpn meSpn mlastMark (L l comment) =
217 -- A comment follows a AST element if all 4 conditions are satisfied:
218 -- A comment follows a AST element if all 4 conditions are satisfied:
218 goesAfter
219 goesAfter
219 && logicallyFollows
220 && logicallyFollows
@@ -255,9 +256,12 @@ commentFollowsElt ref mnSpn meSpn mlastSpn (L l comment) =
255 >= abs (startColumn ref - startColumn l)
256 >= abs (startColumn ref - startColumn l)
257 continuation =
258 continuation =
258− case mlastSpn of
259+ case mlastMark of
260+ Just (HaddockSpan _ spn) ->
261+ srcSpanEndLine spn + 1 == srcSpanStartLine l
262+ Just (CommentSpan spn) ->
263+ srcSpanEndLine spn + 1 == srcSpanStartLine l
259− Nothing -> False
264+ _ -> False
260− Just spn -> srcSpanEndLine spn + 1 == srcSpanStartLine l
261 lastInEnclosing =
265 lastInEnclosing =
262 case meSpn of
266 case meSpn of
263 -- When there is no enclosing element, return false
267 -- When there is no enclosing element, return false
@@ -281,7 +285,7 @@ spitCommentNow spn comment = do
281 . fmap (txt . T.pack)
285 . fmap (txt . T.pack)
283 $ comment
287 $ comment
284− setLastCommentSpan Nothing spn
288+ setSpanMark (CommentSpan spn)
286-- | Output a 'Comment' at the end of correct line or after it depending on
290-- | Output a 'Comment' at the end of correct line or after it depending on
287-- 'CommentPosition'. Used for comments that may potentially follow on the
291-- 'CommentPosition'. Used for comments that may potentially follow on the
@@ -297,4 +301,4 @@ spitCommentPending position spn comment = do
297 . fmap (registerPendingCommentLine position . T.pack)
301 . fmap (registerPendingCommentLine position . T.pack)
299 $ comment
303 $ comment
300− setLastCommentSpan Nothing spn
304+ setSpanMark (CommentSpan spn)