diff --git a/lib/Language/Haskell/Stylish/Comments.hs b/lib/Language/Haskell/Stylish/Comments.hs index f1b09853..4d0b183b 100644 --- a/lib/Language/Haskell/Stylish/Comments.hs +++ b/lib/Language/Haskell/Stylish/Comments.hs @@ -102,12 +102,10 @@ takeNext [] ((cb, c) : comments) = takeNext ((ib, i) : items) [] = Just (ib, NextItem i, items, []) takeNext ((ib, i) : items) ((cb, c) : comments) - | blockStart ib == blockStart cb = - Just (ib <> cb, NextItemWithComment i c, items, comments) - | blockStart ib < blockStart cb = - Just (ib, NextItem i, items, (cb, c) : comments) - | otherwise = - Just (cb, NextComment c, (ib, i) : items, comments) + = case blockEnd ib `compare` blockStart cb of + EQ -> Just (ib <> cb, NextItemWithComment i c, items, comments) + LT -> Just (ib, NextItem i, items, (cb, c) : comments) + GT -> Just (cb, NextComment c, (ib, i) : items, comments) -------------------------------------------------------------------------------- diff --git a/lib/Language/Haskell/Stylish/Step/Data.hs b/lib/Language/Haskell/Stylish/Step/Data.hs index 4da96fbb..20bdd478 100644 --- a/lib/Language/Haskell/Stylish/Step/Data.hs +++ b/lib/Language/Haskell/Stylish/Step/Data.hs @@ -97,9 +97,10 @@ step cfg = makeStep "Data" \ls m -> Editor.apply (changes m) ls ldecl <- GHC.hsmodDecls $ GHC.unLoc m GHC.TyClD _ tycld <- pure $ GHC.unLoc ldecl loc <- maybeToList $ GHC.srcSpanToRealSrcSpan $ GHC.getLocA ldecl + let lastInlineComment = GHC.ann $ GHC.getLoc ldecl case tycld of GHC.DataDecl {..} -> pure $ MkDataDecl - { dataComments = epAnnComments tcdDExt + { dataComments = epAnnComments lastInlineComment <> epAnnComments tcdDExt , dataLoc = loc , dataDeclName = tcdLName , dataTypeVars = tcdTyVars diff --git a/tests/Language/Haskell/Stylish/Step/Data/Tests.hs b/tests/Language/Haskell/Stylish/Step/Data/Tests.hs index 62be7a79..2bdea6c9 100644 --- a/tests/Language/Haskell/Stylish/Step/Data/Tests.hs +++ b/tests/Language/Haskell/Stylish/Step/Data/Tests.hs @@ -79,6 +79,8 @@ tests = testGroup "Language.Haskell.Stylish.Step.Data.Tests" , testCase "case 64" case64 , testCase "case 65" case65 , testCase "case 66 (issue #411)" case66 + , testCase "case 67 (issue #425)" case67 + , testCase "case 68 (issue #426)" case68 ] case00 :: Assertion @@ -1381,6 +1383,41 @@ case66 = assertSnippet (step indentIndentStyle) input input , " deriving (Eq, Show)" ] +-- | Inline comment after the last record field +-- +-- Regression test for https://github.com/haskell/stylish-haskell/issues/425 +case67 :: Assertion +case67 = assertSnippet (step indentIndentStyle) input input + where + input = + [ "data Foo" + , " = Foo -- ^ foo" + , " | Bar -- ^ bar" + , " | Baz -- ^ baz" + , "" + , "data Foo'" + , " = Foo' Int -- ^ foo" + , " | Bar' Int -- ^ bar" + , " | Baz' Int -- ^ baz" + ] + +-- | Inline comment at the different line, than the start of the block +-- (record constructor) +-- +-- Regression test for https://github.com/haskell/stylish-haskell/issues/426 +case68 :: Assertion +case68 = assertSnippet (step indentIndentStyle) input input + where + input = + [ "data Foo" + , " = Foo" + , " { foo :: Int" + , " } -- ^ foo" + , " | Bar" + , " { bar :: Int" + , " } -- ^ bar" + ] + sameSameStyle :: Config sameSameStyle = Config SameLine SameLine 2 2 False True SameLine False True NoMaxColumns