Skip to content

Commit

Permalink
Don't let comments escape from empty export lists
Browse files Browse the repository at this point in the history
  • Loading branch information
amesgen authored and mrkkrp committed Apr 24, 2023
1 parent 683e2a2 commit 5f70b3d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* Support the (deprecated) `DatatypeContexts` extension to avoid surprises.
[Issue 1012](https://github.com/tweag/ormolu/issues/1012).

* Don't let comments escape from empty export lists. [Issue
906](https://github.com/tweag/ormolu/issues/906).

## Ormolu 0.6.0.1

* Fix false positives in AST diffing related to `UnicodeSyntax`. [PR
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
20 changes: 19 additions & 1 deletion src/Ormolu/Printer/Combinators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Ormolu.Printer.Combinators
inciIf,
askSourceType,
askFixityOverrides,
encloseLocated,
askFixityMap,
located,
located',
Expand Down Expand Up @@ -77,7 +78,7 @@ import GHC.Data.Strict qualified as Strict
import GHC.Types.SrcLoc
import Ormolu.Printer.Comments
import Ormolu.Printer.Internal
import Ormolu.Utils (HasSrcSpan (..))
import Ormolu.Utils (HasSrcSpan (..), getLoc')

----------------------------------------------------------------------------
-- Basic
Expand Down Expand Up @@ -111,6 +112,23 @@ located (L l' a) f = case loc' l' of
switchLayout [RealSrcSpan l Strict.Nothing] (f a)
spitFollowingComments l

-- | Similar to 'located', but when the "payload" is an empty list, print
-- virtual elements at the start and end of the source span to prevent comments
-- from "floating out".
encloseLocated ::
(HasSrcSpan l) =>
GenLocated l [a] ->
([a] -> R ()) ->
R ()
encloseLocated la f = located la $ \a -> do
when (null a) $ located (L startSpan ()) pure
f a
when (null a) $ located (L endSpan ()) pure
where
l = getLoc' la
(startLoc, endLoc) = (srcSpanStart l, srcSpanEnd l)
(startSpan, endSpan) = (mkSrcSpan startLoc startLoc, mkSrcSpan endLoc endLoc)

-- | A version of 'located' with arguments flipped.
located' ::
(HasSrcSpan l) =>
Expand Down
2 changes: 1 addition & 1 deletion src/Ormolu/Printer/Meat/Module.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ p_hsModule mstackHeader pragmas HsModule {..} = do
case hsmodExports of
Nothing -> return ()
Just l -> do
located l $ \exports -> do
encloseLocated l $ \exports -> do
inci (p_hsmodExports exports)
breakpoint
txt "where"
Expand Down

0 comments on commit 5f70b3d

Please sign in to comment.