@@ -14,12 +14,10 @@ where
1414
1515import Control.Monad
1616import Data.Coerce (coerce)
17−import Data.Data (Data)
1817import qualified Data.List.NonEmpty as NE
1918import qualified Data.Text as T
2019import Ormolu.Parser.CommentStream
2120import Ormolu.Printer.Internal
22−import Ormolu.Utils (isModule)
2321import SrcLoc
2422
2523----------------------------------------------------------------------------
@@ -27,9 +25,8 @@ import SrcLoc
2725
2826-- | Output all preceding comments for an element at given location.
2927spitPrecedingComments ::
30− Data a =>
31− -- | AST element to attach comments to
32− RealLocated a ->
28+ -- | Span of the element to attach comments to
29+ RealSrcSpan ->
3330 R ()
3431spitPrecedingComments ref = do
3532 gotSome <- handleCommentSeries (spitPrecedingComment ref)
@@ -37,16 +34,15 @@ spitPrecedingComments ref = do
3734 lastMark <- getSpanMark
3835 -- Insert a blank line between the preceding comments and the thing
3936 -- after them if there was a blank line in the input.
40− when (needsNewlineBefore (getRealSrcSpan ref) lastMark) newline
37+ when (needsNewlineBefore ref lastMark) newline
4138
4239-- | Output all comments following an element at given location.
4340spitFollowingComments ::
44− Data a =>
45− -- | AST element of attach comments to
46− RealLocated a ->
41+ -- | Span of the element to attach comments to
42+ RealSrcSpan ->
4743 R ()
4844spitFollowingComments ref = do
49− trimSpanStream (getRealSrcSpan ref)
45+ trimSpanStream ref
5046 void $ handleCommentSeries (spitFollowingComment ref)
5147
5248-- | Output all remaining comments in the comment stream.
@@ -62,14 +58,13 @@ spitRemainingComments = do
6258
6359-- | Output a single preceding comment for an element at given location.
6460spitPrecedingComment ::
65− Data a =>
66− -- | AST element to attach comments to
67− RealLocated a ->
61+ -- | Span of the element to attach comments to
62+ RealSrcSpan ->
6863 -- | Location of last comment in the series
6964 Maybe SpanMark ->
7065 -- | Are we done?
7166 R Bool
72−spitPrecedingComment (L ref a) mlastMark = do
67+spitPrecedingComment ref mlastMark = do
7368 let p (L l _) = realSrcSpanEnd l <= realSrcSpanStart ref
7469 withPoppedComment p $ \l comment -> do
7570 dirtyLine <-
@@ -80,31 +75,33 @@ spitPrecedingComment (L ref a) mlastMark = do
8075 -- immediately because it'll be attached to the previous element (on
8176 -- the same line) on the next run, so we play safe here and output
8277 -- an extra 'newline' in this case.
83− Nothing -> isLineDirty -- only for very first preceding comment
78+ --
79+ -- We check 'mlastMark' to do this only for the very first preceding
80+ -- comment.
8481 Just _ -> return False
82+ _ -> isLineDirty
8583 when (dirtyLine || needsNewlineBefore l mlastMark) newline
8684 spitCommentNow l comment
87− if theSameLinePre l ref && not (isModule a)
85+ if theSameLinePre l ref
8886 then space
8987 else newline
9088
9189-- | Output a comment that follows element at given location immediately on
9290-- the same line, if there is any.
9391spitFollowingComment ::
94− Data a =>
9592 -- | AST element to attach comments to
96− RealLocated a ->
93+ RealSrcSpan ->
9794 -- | Location of last comment in the series
9895 Maybe SpanMark ->
9996 -- | Are we done?
10097 R Bool
101−spitFollowingComment (L ref a) mlastMark = do
98+spitFollowingComment ref mlastMark = do
10299 mnSpn <- nextEltSpan
103100 -- Get first enclosing span that is not equal to reference span, i.e. it's
104101 -- truly something enclosing the AST element.
105102 meSpn <- getEnclosingSpan (/= ref)
106103 withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastMark) $ \l comment ->
107− if theSameLinePost l ref && not (isModule a)
104+ if theSameLinePost l ref
108105 then
109106 if isMultilineComment comment
110107 then space >> spitCommentNow l comment