@@ -14,12 +14,10 @@ where
15import Control.Monad
15import Control.Monad
16import Data.Coerce (coerce)
16import Data.Coerce (coerce)
17−import Data.Data (Data)
18import qualified Data.List.NonEmpty as NE
17import qualified Data.List.NonEmpty as NE
19import qualified Data.Text as T
18import qualified Data.Text as T
20import Ormolu.Parser.CommentStream
19import Ormolu.Parser.CommentStream
21import Ormolu.Printer.Internal
20import Ormolu.Printer.Internal
22−import Ormolu.Utils (isModule)
23import SrcLoc
21import SrcLoc
25----------------------------------------------------------------------------
23----------------------------------------------------------------------------
@@ -27,9 +25,8 @@ import SrcLoc
28-- | Output all preceding comments for an element at given location.
26-- | Output all preceding comments for an element at given location.
29spitPrecedingComments ::
27spitPrecedingComments ::
31− -- | AST element to attach comments to
28+ -- | Span of the element to attach comments to
32− RealLocated a ->
29+ RealSrcSpan ->
34spitPrecedingComments ref = do
31spitPrecedingComments ref = do
35 gotSome <- handleCommentSeries (spitPrecedingComment ref)
32 gotSome <- handleCommentSeries (spitPrecedingComment ref)
@@ -37,16 +34,15 @@ spitPrecedingComments ref = do
37 lastMark <- getSpanMark
34 lastMark <- getSpanMark
38 -- Insert a blank line between the preceding comments and the thing
35 -- Insert a blank line between the preceding comments and the thing
39 -- after them if there was a blank line in the input.
36 -- after them if there was a blank line in the input.
40− when (needsNewlineBefore (getRealSrcSpan ref) lastMark) newline
37+ when (needsNewlineBefore ref lastMark) newline
42-- | Output all comments following an element at given location.
39-- | Output all comments following an element at given location.
43spitFollowingComments ::
40spitFollowingComments ::
45− -- | AST element of attach comments to
41+ -- | Span of the element to attach comments to
46− RealLocated a ->
42+ RealSrcSpan ->
48spitFollowingComments ref = do
44spitFollowingComments ref = do
49− trimSpanStream (getRealSrcSpan ref)
45+ trimSpanStream ref
50 void $ handleCommentSeries (spitFollowingComment ref)
46 void $ handleCommentSeries (spitFollowingComment ref)
52-- | Output all remaining comments in the comment stream.
48-- | Output all remaining comments in the comment stream.
@@ -62,14 +58,13 @@ spitRemainingComments = do
63-- | Output a single preceding comment for an element at given location.
59-- | Output a single preceding comment for an element at given location.
64spitPrecedingComment ::
60spitPrecedingComment ::
66− -- | AST element to attach comments to
61+ -- | Span of the element to attach comments to
67− RealLocated a ->
62+ RealSrcSpan ->
68 -- | Location of last comment in the series
63 -- | Location of last comment in the series
69 Maybe SpanMark ->
64 Maybe SpanMark ->
70 -- | Are we done?
65 -- | Are we done?
72−spitPrecedingComment (L ref a) mlastMark = do
67+spitPrecedingComment ref mlastMark = do
73 let p (L l _) = realSrcSpanEnd l <= realSrcSpanStart ref
68 let p (L l _) = realSrcSpanEnd l <= realSrcSpanStart ref
74 withPoppedComment p $ \l comment -> do
69 withPoppedComment p $ \l comment -> do
75 dirtyLine <-
70 dirtyLine <-
@@ -80,31 +75,33 @@ spitPrecedingComment (L ref a) mlastMark = do
80 -- immediately because it'll be attached to the previous element (on
75 -- immediately because it'll be attached to the previous element (on
81 -- the same line) on the next run, so we play safe here and output
76 -- the same line) on the next run, so we play safe here and output
82 -- an extra 'newline' in this case.
77 -- an extra 'newline' in this case.
83− Nothing -> isLineDirty -- only for very first preceding comment
79+ -- We check 'mlastMark' to do this only for the very first preceding
84 Just _ -> return False
81 Just _ -> return False
85 when (dirtyLine || needsNewlineBefore l mlastMark) newline
83 when (dirtyLine || needsNewlineBefore l mlastMark) newline
86 spitCommentNow l comment
84 spitCommentNow l comment
87− if theSameLinePre l ref && not (isModule a)
85+ if theSameLinePre l ref
88 then space
86 then space
89 else newline
87 else newline
91-- | Output a comment that follows element at given location immediately on
89-- | Output a comment that follows element at given location immediately on
92-- the same line, if there is any.
90-- the same line, if there is any.
93spitFollowingComment ::
91spitFollowingComment ::
95 -- | AST element to attach comments to
92 -- | AST element to attach comments to
96− RealLocated a ->
93+ RealSrcSpan ->
97 -- | Location of last comment in the series
94 -- | Location of last comment in the series
98 Maybe SpanMark ->
95 Maybe SpanMark ->
99 -- | Are we done?
96 -- | Are we done?
101−spitFollowingComment (L ref a) mlastMark = do
98+spitFollowingComment ref mlastMark = do
102 mnSpn <- nextEltSpan
99 mnSpn <- nextEltSpan
103 -- Get first enclosing span that is not equal to reference span, i.e. it's
100 -- Get first enclosing span that is not equal to reference span, i.e. it's
104 -- truly something enclosing the AST element.
101 -- truly something enclosing the AST element.
105 meSpn <- getEnclosingSpan (/= ref)
102 meSpn <- getEnclosingSpan (/= ref)
106 withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastMark) $ \l comment ->
103 withPoppedComment (commentFollowsElt ref mnSpn meSpn mlastMark) $ \l comment ->
107− if theSameLinePost l ref && not (isModule a)
104+ if theSameLinePost l ref
109 if isMultilineComment comment
106 if isMultilineComment comment
110 then space >> spitCommentNow l comment
107 then space >> spitCommentNow l comment