Skip to content

Commit

Permalink
Fix more balance comments tests
Browse files Browse the repository at this point in the history
Down to 7 failures
  • Loading branch information
alanz committed Apr 1, 2024
1 parent 78fe9e7 commit abb1635
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
34 changes: 24 additions & 10 deletions src/Language/Haskell/GHC/ExactPrint/Transform.hs
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,7 @@ balanceComments' la1 la2 = do
-- debugM $ "balanceComments': (anc1)=" ++ showAst (anc1)
-- debugM $ "balanceComments': (anc2)=" ++ showAst (anc2)
debugM $ "balanceComments': (cs1f)=" ++ showAst (cs1f)
debugM $ "balanceComments': (cs1fp, cs1ff)=" ++ showAst (cs1fp, cs1ff)
debugM $ "balanceComments': (cs1s)=" ++ showAst (cs1s)
debugM $ "balanceComments': (cs2p, cs2f)=" ++ showAst (cs2p, cs2f)
debugM $ "balanceComments': (cs1stay,cs1move)=" ++ showAst (cs1stay,cs1move)
debugM $ "balanceComments': (an1',an2')=" ++ showAst (an1',an2')
return (la1', la2')
Expand All @@ -539,30 +538,30 @@ balanceComments' la1 la2 = do
anc1 = comments an1
anc2 = comments an2

cs1s = splitCommentsEnd (anchorFromLocatedA la1) anc1
cs1p = priorCommentsDeltas (anchorFromLocatedA la1) (priorComments cs1s)
(p1,m1,f1) = splitComments (anchorFromLocatedA la1) anc1
cs1p = priorCommentsDeltas (anchorFromLocatedA la1) p1

-- Split cs1 following comments into those before any
-- TrailingAnn's on an1, and any after
cs1f = splitCommentsEnd (fullSpanFromLocatedA la1) $ EpaComments (getFollowingComments cs1s)
cs1f = splitCommentsEnd (fullSpanFromLocatedA la1) $ EpaComments f1
cs1fp = priorCommentsDeltas (anchorFromLocatedA la1) (priorComments cs1f)
cs1ff = trailingCommentsDeltas (anchorFromLocatedA la1) (getFollowingComments cs1f)

-- Split cs1ff into those that belong on an1 and ones that must move to an2
(cs1move,cs1stay) = break (simpleBreak 1) cs1ff

cs2s = splitCommentsEnd (anchorFromLocatedA la2) anc2
cs2p = priorCommentsDeltas (anchorFromLocatedA la2) (priorComments cs2s)
cs2f = trailingCommentsDeltas (anchorFromLocatedA la2) (getFollowingComments cs2s)
(p2,m2,f2) = splitComments (anchorFromLocatedA la2) anc2
cs2p = priorCommentsDeltas (anchorFromLocatedA la2) p2
cs2f = trailingCommentsDeltas (anchorFromLocatedA la2) f2

(stay'',move') = break (simpleBreak 1) cs2p
-- Need to also check for comments more closely attached to la1,
-- ie trailing on the same line
(move'',stay') = break (simpleBreak 0) (trailingCommentsDeltas (anchorFromLocatedA la1) (map snd stay''))
move = sortEpaComments $ map snd (cs1fp ++ cs1move ++ move'' ++ move')
stay = sortEpaComments $ map snd (cs1stay ++ stay')
stay = sortEpaComments $ m2 ++ map snd (cs1stay ++ stay')

an1' = setCommentsEpAnn (getLoc la1) (epaCommentsBalanced (map snd cs1p) move)
an1' = setCommentsEpAnn (getLoc la1) (epaCommentsBalanced (m1 ++ map snd cs1p) move)
an2' = setCommentsEpAnn (getLoc la2) (epaCommentsBalanced stay (map snd cs2f))
la1' = L an1' f
la2' = L an2' s
Expand Down Expand Up @@ -600,6 +599,21 @@ priorCommentsDeltas r cs = go r (reverse $ sortEpaComments cs)

-- ---------------------------------------------------------------------

-- | Split comments into ones occurring before the end of the reference
-- span, and those after it.
splitComments :: RealSrcSpan -> EpAnnComments -> ([LEpaComment], [LEpaComment], [LEpaComment])
splitComments p cs = (before, middle, after)
where
cmpe (L (EpaSpan (RealSrcSpan l _)) _) = ss2pos l > ss2posEnd p
cmpe (L _ _) = True

cmpb (L (EpaSpan (RealSrcSpan l _)) _) = ss2pos l > ss2pos p
cmpb (L _ _) = True

(beforeEnd, after) = break cmpe ((priorComments cs) ++ (getFollowingComments cs))
(before, middle) = break cmpb beforeEnd


-- | Split comments into ones occurring before the end of the reference
-- span, and those after it.
splitCommentsEnd :: RealSrcSpan -> EpAnnComments -> EpAnnComments
Expand Down
6 changes: 3 additions & 3 deletions tests/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ tt' = do
-- mkParserTest libdir "ghc98" "IndentedModule2.hs"

-- mkParserTest libdir "ghc92" "TopLevelSemis.hs"
mkParserTestBC libdir "ghc92" "TopLevelSemis.hs"
-- mkParserTestBC libdir "ghc92" "TopLevelSemis.hs"

-- mkParserTest libdir "ghc92" "TopLevelSemis4.hs"
-- mkParserTestBC libdir "ghc92" "TopLevelSemis4.hs"
-- mkParserTest libdir "ghc710" "Process2.hs"
mkParserTestBC libdir "ghc710" "Process2.hs"

-- mkParserTest libdir "ghc710" "CExpected.hs"
-- Needs GHC changes
Expand Down

0 comments on commit abb1635

Please sign in to comment.