1
1
mirror of https://github.com/tweag/ormolu.git synced 2024-10-05 17:37:11 +03:00

Don't let comments escape from empty export lists

This commit is contained in:
Alexander Esgen 2023-04-23 21:41:48 +02:00 committed by Mark Karpov
parent 683e2a2043
commit db1ebbba2f
5 changed files with 29 additions and 2 deletions

View File

@ -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

View File

@ -0,0 +1,4 @@
module Foo
( -- test
)
where

View File

@ -0,0 +1,2 @@
module Foo ( -- test
) where

View File

@ -23,6 +23,7 @@ module Ormolu.Printer.Combinators
inciIf,
askSourceType,
askFixityOverrides,
encloseLocated,
askFixityMap,
located,
located',
@ -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
@ -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) =>

View File

@ -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"