Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline comment to a record constructor lead to non idempotent behavior #431

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions lib/Language/Haskell/Stylish/Comments.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)


--------------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion lib/Language/Haskell/Stylish/Step/Data.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions tests/Language/Haskell/Stylish/Step/Data/Tests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down