From e70571f701041e976e7a8c22dd416a7f9f5ef4f0 Mon Sep 17 00:00:00 2001 From: Lev Dvorkin Date: Sun, 6 Nov 2022 18:18:36 +0300 Subject: [PATCH 1/5] Failing test added --- .../Haskell/Stylish/Step/Data/Tests.hs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/Language/Haskell/Stylish/Step/Data/Tests.hs b/tests/Language/Haskell/Stylish/Step/Data/Tests.hs index 62be7a79..9d67f808 100644 --- a/tests/Language/Haskell/Stylish/Step/Data/Tests.hs +++ b/tests/Language/Haskell/Stylish/Step/Data/Tests.hs @@ -79,6 +79,7 @@ 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 ] case00 :: Assertion @@ -1381,6 +1382,24 @@ 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" + ] + sameSameStyle :: Config sameSameStyle = Config SameLine SameLine 2 2 False True SameLine False True NoMaxColumns From 315ddbffacbfe3ebe2e54e8d1e6e5c218284324c Mon Sep 17 00:00:00 2001 From: Lev Dvorkin Date: Sun, 6 Nov 2022 23:21:09 +0300 Subject: [PATCH 2/5] Issue solved in a not fine way --- lib/Language/Haskell/Stylish/Step/Data.hs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 From 7181d3ab4b5abaca96f36776e7c5dde87194ac93 Mon Sep 17 00:00:00 2001 From: Lev Dvorkin Date: Sat, 19 Nov 2022 10:23:17 +0300 Subject: [PATCH 3/5] Failing test added --- .../Haskell/Stylish/Step/Data/Tests.hs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Language/Haskell/Stylish/Step/Data/Tests.hs b/tests/Language/Haskell/Stylish/Step/Data/Tests.hs index 9d67f808..2bdea6c9 100644 --- a/tests/Language/Haskell/Stylish/Step/Data/Tests.hs +++ b/tests/Language/Haskell/Stylish/Step/Data/Tests.hs @@ -80,6 +80,7 @@ tests = testGroup "Language.Haskell.Stylish.Step.Data.Tests" , testCase "case 65" case65 , testCase "case 66 (issue #411)" case66 , testCase "case 67 (issue #425)" case67 + , testCase "case 68 (issue #426)" case68 ] case00 :: Assertion @@ -1400,6 +1401,23 @@ case67 = assertSnippet (step indentIndentStyle) input input , " | 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 From ba79d2109193b2ded2186b26ae0752edbe67707e Mon Sep 17 00:00:00 2001 From: Lev Dvorkin Date: Sat, 19 Nov 2022 10:26:07 +0300 Subject: [PATCH 4/5] takeNext modified to fix the issue --- lib/Language/Haskell/Stylish/Comments.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Language/Haskell/Stylish/Comments.hs b/lib/Language/Haskell/Stylish/Comments.hs index f1b09853..6eabab0f 100644 --- a/lib/Language/Haskell/Stylish/Comments.hs +++ b/lib/Language/Haskell/Stylish/Comments.hs @@ -102,9 +102,9 @@ takeNext [] ((cb, c) : comments) = takeNext ((ib, i) : items) [] = Just (ib, NextItem i, items, []) takeNext ((ib, i) : items) ((cb, c) : comments) - | blockStart ib == blockStart cb = + | blockEnd ib == blockStart cb = Just (ib <> cb, NextItemWithComment i c, items, comments) - | blockStart ib < blockStart cb = + | blockEnd ib < blockStart cb = Just (ib, NextItem i, items, (cb, c) : comments) | otherwise = Just (cb, NextComment c, (ib, i) : items, comments) From 732c50d0038ec07b763973f548b6bd1ba5281ce7 Mon Sep 17 00:00:00 2001 From: Lev Dvorkin Date: Sat, 19 Nov 2022 10:28:32 +0300 Subject: [PATCH 5/5] takeNext rewritten using compare --- lib/Language/Haskell/Stylish/Comments.hs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/Language/Haskell/Stylish/Comments.hs b/lib/Language/Haskell/Stylish/Comments.hs index 6eabab0f..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) - | blockEnd ib == blockStart cb = - Just (ib <> cb, NextItemWithComment i c, items, comments) - | blockEnd 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) --------------------------------------------------------------------------------