Skip to content

Commit

Permalink
Fix comment in empty export
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonchinn178 committed Jan 29, 2023
1 parent 63b6d4e commit 8735394
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Unreleased

* Fix comment in empty export list being moved out [Issue 906](https://github.com/tweag/ormolu/issues/906)

## Ormolu 0.5.3.0

* Stop making empty `let`s move comments. [Issue
Expand Down
4 changes: 4 additions & 0 deletions data/examples/module-header/multiline-empty-comment-out.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Foo
( -- test
)
where
2 changes: 2 additions & 0 deletions data/examples/module-header/multiline-empty-comment.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Foo ( -- test
) where
2 changes: 1 addition & 1 deletion src/Ormolu/Printer/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ located ::
located (L l' a) f = case loc' l' of
UnhelpfulSpan _ -> f a
RealSrcSpan l _ -> do
spitPrecedingComments l
spitPrecedingComments True l
withEnclosingSpan l $
switchLayout [RealSrcSpan l Strict.Nothing] (f a)
spitFollowingComments l
Expand Down
38 changes: 24 additions & 14 deletions src/Ormolu/Printer/Comments.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@ import Ormolu.Printer.Internal

-- | Output all preceding comments for an element at given location.
spitPrecedingComments ::
-- | Whether to output a newline after the last comment
Bool ->
-- | Span of the element to attach comments to
RealSrcSpan ->
R ()
spitPrecedingComments ref = do
spitPrecedingComments newlineAfter ref = do
comments <- handleCommentSeries (spitPrecedingComment ref)
when (not $ null comments) $ do

whenNonEmpty comments $ \(lastComment NE.:| _) -> do
when newlineAfter $ if theSameLinePre (getLoc lastComment) ref then space else newline

lastMark <- getSpanMark
-- Insert a blank line between the preceding comments and the thing
-- after them if there was a blank line in the input.
when (needsNewlineBefore ref lastMark) newline
where
whenNonEmpty xs f = maybe (return ()) f (NE.nonEmpty xs)

-- | Output all comments following an element at given location.
spitFollowingComments ::
Expand All @@ -42,15 +49,15 @@ spitFollowingComments ::
R ()
spitFollowingComments ref = do
trimSpanStream ref
void $ handleCommentSeries (spitFollowingComment ref)
void $ handleCommentSeries (\_ -> spitFollowingComment ref)

-- | Output all remaining comments in the comment stream.
spitRemainingComments :: R ()
spitRemainingComments = do
-- Make sure we have a blank a line between the last definition and the
-- trailing comments.
newline
void $ handleCommentSeries spitRemainingComment
void $ handleCommentSeries (\_ -> spitRemainingComment)

----------------------------------------------------------------------------
-- Single-comment functions
Expand All @@ -59,12 +66,18 @@ spitRemainingComments = do
spitPrecedingComment ::
-- | Span of the element to attach comments to
RealSrcSpan ->
-- | The last comment output, if any
Maybe LComment ->
-- | The comment that was output, if any
R (Maybe LComment)
spitPrecedingComment ref = do
spitPrecedingComment ref mLastComment = do
mlastMark <- getSpanMark
let p (L l _) = realSrcSpanEnd l <= realSrcSpanStart ref
withPoppedComment p $ \l comment -> do
case mLastComment of
Just lastComment -> if theSameLinePre (getLoc lastComment) ref then space else newline
Nothing -> return ()

lineSpans <- thisLineSpans
let thisCommentLine = srcLocLine (realSrcSpanStart l)
needsNewline =
Expand All @@ -73,9 +86,6 @@ spitPrecedingComment ref = do
Just spn -> srcLocLine (realSrcSpanEnd spn) /= thisCommentLine
when (needsNewline || needsNewlineBefore l mlastMark) newline
spitCommentNow l comment
if theSameLinePre l ref
then space
else newline

-- | Output a comment that follows element at given location immediately on
-- the same line, if there is any.
Expand Down Expand Up @@ -117,17 +127,17 @@ spitRemainingComment = do

-- | Output series of comments.
handleCommentSeries ::
-- | Output and return the next comment, if any
R (Maybe LComment) ->
-- | Given the last output comment, output and return the next comment, if any
(Maybe LComment -> R (Maybe LComment)) ->
-- | The comments outputted
R [LComment]
handleCommentSeries f = go
handleCommentSeries f = go Nothing
where
go = do
mComment <- f
go lastComment = do
mComment <- f lastComment
case mComment of
Nothing -> return []
Just comment -> (comment:) <$> go
Just comment -> (comment:) <$> go (Just comment)

-- | Try to pop a comment using given predicate and if there is a comment
-- matching the predicate, print it out.
Expand Down
8 changes: 8 additions & 0 deletions src/Ormolu/Printer/Meat/ImportExport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import GHC.Types.PkgQual
import GHC.Types.SrcLoc
import GHC.Unit.Types
import Ormolu.Printer.Combinators
import Ormolu.Printer.Comments (spitPrecedingComments)
import Ormolu.Printer.Meat.Common
import Ormolu.Utils (RelativePos (..), attachRelativePos)

Expand All @@ -29,6 +30,13 @@ p_hsmodExports lexports =
(\(p, l) -> sitcc (located l (p_lie layout p)))
(attachRelativePos exports)

-- if there are any more comments before the close parens,
-- output them now
case al_close . anns . ann . getLoc $ lexports of
Nothing -> return ()
Just (AddEpAnn _ closeParenLoc) -> do
spitPrecedingComments False $ epaLocationRealSrcSpan closeParenLoc

p_hsmodImport :: ImportDecl GhcPs -> R ()
p_hsmodImport ImportDecl {..} = do
useQualifiedPost <- isExtensionEnabled ImportQualifiedPost
Expand Down

0 comments on commit 8735394

Please sign in to comment.