diff options
author | Alan Zimmerman <alan.zimm@gmail.com> | 2022-12-13 23:30:52 +0000 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2022-12-22 23:38:35 -0500 |
commit | 3699a5542caa88a8718588e68549b6291bcb5bfc (patch) | |
tree | 7b6697260afde589dc05aef808a1bf8ce07b1ccc /utils/check-exact/ExactPrint.hs | |
parent | b2c7523d8987bedf13a7dd682d836ffb76cbe09d (diff) | |
download | haskell-3699a5542caa88a8718588e68549b6291bcb5bfc.tar.gz |
EPA: Make EOF position part of AnnsModule
Closes #20951
Closes #19697
Diffstat (limited to 'utils/check-exact/ExactPrint.hs')
-rw-r--r-- | utils/check-exact/ExactPrint.hs | 51 |
1 files changed, 32 insertions, 19 deletions
diff --git a/utils/check-exact/ExactPrint.hs b/utils/check-exact/ExactPrint.hs index 52fb4136ec..57389565b4 100644 --- a/utils/check-exact/ExactPrint.hs +++ b/utils/check-exact/ExactPrint.hs @@ -114,6 +114,7 @@ defaultEPState = EPState , uExtraDP = Nothing , epComments = [] , epCommentsApplied = [] + , epEof = Nothing } @@ -188,6 +189,7 @@ data EPState = EPState -- Shared , epComments :: ![Comment] , epCommentsApplied :: ![[Comment]] + , epEof :: !(Maybe (RealSrcSpan, RealSrcSpan)) } -- --------------------------------------------------------------------- @@ -238,11 +240,7 @@ instance HasEntry (EpAnn a) where fromAnn' :: (HasEntry a) => a -> Entry fromAnn' an = case fromAnn an of NoEntryVal -> NoEntryVal - Entry a c _ u -> Entry a c' FlushComments u - where - c' = case c of - EpaComments cs -> EpaCommentsBalanced (filterEofComment False cs) (filterEofComment True cs) - EpaCommentsBalanced cp ct -> EpaCommentsBalanced cp ct + Entry a c _ u -> Entry a c FlushComments u -- --------------------------------------------------------------------- @@ -355,7 +353,7 @@ enterAnn (Entry anchor' cs flush canUpdateAnchor) a = do let mflush = when (flush == FlushComments) $ do debugM $ "flushing comments in enterAnn:" ++ showAst cs - flushComments (getFollowingComments cs ++ filterEofComment True (priorComments cs)) + flushComments (getFollowingComments cs) advance edp a' <- exact a @@ -369,6 +367,17 @@ enterAnn (Entry anchor' cs flush canUpdateAnchor) a = do mapM_ printOneComment (map tokComment $ getFollowingComments cs) debugM $ "ending trailing comments" + eof <- getEofPos + case eof of + Nothing -> return () + Just (pos, prior) -> do + let dp = if pos == prior + then (DifferentLine 1 0) + else origDelta pos prior + debugM $ "EOF:(pos,prior,dp) =" ++ showGhc (ss2pos pos, ss2pos prior, dp) + printStringAtLsDelta dp "" + setEofPos Nothing -- Only do this once + let newAchor = anchor' { anchor_op = MovedAnchor edp } let r = case canUpdateAnchor of CanUpdateAnchor -> setAnnotationAnchor a' newAchor (mkEpaComments (priorCs++ postCs) []) @@ -413,23 +422,12 @@ addComments csNew = do -- ones in the state. flushComments :: (Monad m, Monoid w) => [LEpaComment] -> EP w m () flushComments trailing = do - addCommentsA (filterEofComment False trailing) + addCommentsA trailing cs <- getUnallocatedComments debugM $ "flushing comments starting" mapM_ printOneComment (sortComments cs) - debugM $ "flushing comments:EOF:trailing:" ++ showAst (trailing) - debugM $ "flushing comments:EOF:" ++ showAst (filterEofComment True trailing) - mapM_ printOneComment (map tokComment (filterEofComment True trailing)) debugM $ "flushing comments done" -filterEofComment :: Bool -> [LEpaComment] -> [LEpaComment] -filterEofComment keep cs = fixCs cs - where - notEof com = case com of - L _ (GHC.EpaComment (EpaEofComment) _) -> keep - _ -> not keep - fixCs c = filter notEof c - -- --------------------------------------------------------------------- -- |In order to interleave annotations into the stream, we turn them into @@ -1397,6 +1395,13 @@ instance ExactPrint (HsModule GhcPs) where EpAnnNotUsed -> (am_decls $ anns an0) EpAnn _ r _ -> r + -- Print EOF + case am_eof $ anns an of + Nothing -> return () + Just (pos, prior) -> do + debugM $ "am_eof:" ++ showGhc (pos, prior) + setEofPos (Just (pos, prior)) + let anf = an0 { anns = (anns an0) { am_decls = am_decls' }} debugM $ "HsModule, anf=" ++ showAst anf @@ -4761,7 +4766,7 @@ printStringAtLsDelta cl s = do -- `debug` ("printStringAtLsDelta:(pos,s):" ++ show (undelta p cl colOffset,s)) p' <- getPosP d <- getPriorEndD - debugM $ "printStringAtLsDelta:(pos,p',d,s):" ++ show (undelta p cl colOffset,p',d,s) + debugM $ "printStringAtLsDelta:(pos,p,p',d,s):" ++ show (undelta p cl colOffset,p,p',d,s) else return () `debug` ("printStringAtLsDelta:bad delta for (mc,s):" ++ show (cl,s)) -- --------------------------------------------------------------------- @@ -4873,6 +4878,14 @@ setAnchorU rss = do debugM $ "setAnchorU:" ++ show (rs2range rss) modify (\s -> s { uAnchorSpan = rss }) +getEofPos :: (Monad m, Monoid w) => EP w m (Maybe (RealSrcSpan, RealSrcSpan)) +getEofPos = gets epEof + +setEofPos :: (Monad m, Monoid w) => Maybe (RealSrcSpan, RealSrcSpan) -> EP w m () +setEofPos l = modify (\s -> s {epEof = l}) + +-- --------------------------------------------------------------------- + getUnallocatedComments :: (Monad m, Monoid w) => EP w m [Comment] getUnallocatedComments = gets epComments |