diff --git a/data/examples/other/following-comment-last-0-out.hs b/data/examples/other/following-comment-last-0-out.hs new file mode 100644 index 0000000..7345298 --- /dev/null +++ b/data/examples/other/following-comment-last-0-out.hs @@ -0,0 +1,3 @@ +-- | Another datatype... +data D' +-- ^ ...with two docstrings. diff --git a/data/examples/other/following-comment-last-0.hs b/data/examples/other/following-comment-last-0.hs new file mode 100644 index 0000000..7345298 --- /dev/null +++ b/data/examples/other/following-comment-last-0.hs @@ -0,0 +1,3 @@ +-- | Another datatype... +data D' +-- ^ ...with two docstrings. diff --git a/data/examples/other/following-comment-last-1-out.hs b/data/examples/other/following-comment-last-1-out.hs new file mode 100644 index 0000000..7a3ce72 --- /dev/null +++ b/data/examples/other/following-comment-last-1-out.hs @@ -0,0 +1,6 @@ +-- | Another datatype... +data D' + deriving (Show) +-- ^ ...with two docstrings. + +-- more diff --git a/data/examples/other/following-comment-last-1.hs b/data/examples/other/following-comment-last-1.hs new file mode 100644 index 0000000..7a3ce72 --- /dev/null +++ b/data/examples/other/following-comment-last-1.hs @@ -0,0 +1,6 @@ +-- | Another datatype... +data D' + deriving (Show) +-- ^ ...with two docstrings. + +-- more diff --git a/data/examples/other/following-comment-last-2-out.hs b/data/examples/other/following-comment-last-2-out.hs new file mode 100644 index 0000000..29fb159 --- /dev/null +++ b/data/examples/other/following-comment-last-2-out.hs @@ -0,0 +1,7 @@ +-- | Another datatype... +data D' + deriving (Show) +-- ^ ...with two docstrings. + +-- more +data B diff --git a/data/examples/other/following-comment-last-2.hs b/data/examples/other/following-comment-last-2.hs new file mode 100644 index 0000000..c0afbb0 --- /dev/null +++ b/data/examples/other/following-comment-last-2.hs @@ -0,0 +1,8 @@ +-- | Another datatype... +data D' + deriving (Show) +-- ^ ...with two docstrings. + +-- more + +data B diff --git a/data/examples/other/following-comment-last-3-out.hs b/data/examples/other/following-comment-last-3-out.hs new file mode 100644 index 0000000..280bd24 --- /dev/null +++ b/data/examples/other/following-comment-last-3-out.hs @@ -0,0 +1,4 @@ +-- | Another datatype... +data D' +-- ^ ...with two docstrings. +-- even on second line diff --git a/data/examples/other/following-comment-last-3.hs b/data/examples/other/following-comment-last-3.hs new file mode 100644 index 0000000..280bd24 --- /dev/null +++ b/data/examples/other/following-comment-last-3.hs @@ -0,0 +1,4 @@ +-- | Another datatype... +data D' +-- ^ ...with two docstrings. +-- even on second line diff --git a/src/Ormolu/Printer/Meat/Module.hs b/src/Ormolu/Printer/Meat/Module.hs index fc070da..6900320 100644 --- a/src/Ormolu/Printer/Meat/Module.hs +++ b/src/Ormolu/Printer/Meat/Module.hs @@ -15,6 +15,7 @@ import GHC import Ormolu.Imports import Ormolu.Printer.Combinators import Ormolu.Printer.Comments +import Ormolu.Printer.Internal (isNewlineModified) import Ormolu.Printer.Meat.Common import Ormolu.Printer.Meat.Declaration import Ormolu.Printer.Meat.Declaration.Warning @@ -54,7 +55,13 @@ p_hsModule exts (L moduleSpan HsModule {..}) = do forM_ (sortImports hsmodImports) (located' p_hsmodImport) when (hasImports && hasDecls) newline p_hsDecls Free hsmodDecls - when hasDecls newline trailingComments <- hasMoreComments + when hasDecls $ do + newlineModified <- isNewlineModified + newline + -- In this case we need to insert a newline between the comments + -- output as a side effect of the previous newline and trailing + -- comments to prevent them from merging. + when (newlineModified && trailingComments) newline when (trailingComments && hasModuleHeader) newline spitRemainingComments