Alan Zimmerman pushed to branch wip/az/epa-tidy-locatedxxx-39 at Glasgow Haskell Compiler / GHC

Commits:

2 changed files:

Changes:

  • utils/check-exact/ExactPrint.hs
    ... ... @@ -78,7 +78,7 @@ import Data.Functor.Const
    78 78
     import Data.Typeable
    
    79 79
     import Data.List ( partition, sort )
    
    80 80
     import qualified Data.List.NonEmpty as NE
    
    81
    -import Data.Maybe ( isJust )
    
    81
    +import Data.Maybe ( isJust, isNothing, fromMaybe )
    
    82 82
     import Data.Void
    
    83 83
     
    
    84 84
     import Utils
    
    ... ... @@ -122,13 +122,11 @@ defaultEPState = EPState
    122 122
                  , uExtraDPReturn = Nothing
    
    123 123
                  , pAcceptSpan = False
    
    124 124
     
    
    125
    -             , epPos       = (1,1)
    
    126
    -             , pMarkLayout = False
    
    127
    -             , pLHS = LayoutStartCol 1
    
    125
    +             , pPos        = (1,1)
    
    126
    +             , pLayout = [LayoutFrame (LayoutStartCol 1) (Just (LayoutStartCol 1))]
    
    128 127
     
    
    129 128
                  , dPriorEndPosition = (1,1)
    
    130
    -             , dMarkLayout = False
    
    131
    -             , dLHS        = LayoutStartCol 1
    
    129
    +             , dLayout = [LayoutFrame (LayoutStartCol 1) (Just (LayoutStartCol 1))]
    
    132 130
     
    
    133 131
                  , epComments = []
    
    134 132
                  , epCommentsApplied = []
    
    ... ... @@ -172,6 +170,13 @@ instance Monoid w => Semigroup (EPWriter w) where
    172 170
     instance Monoid w => Monoid (EPWriter w) where
    
    173 171
       mempty = EPWriter mempty
    
    174 172
     
    
    173
    +-- ---------------------------------------------------------------------
    
    174
    +
    
    175
    +data LayoutFrame = LayoutFrame
    
    176
    +  { lfSaved    :: !LayoutStartCol         -- ^ effective offset at scope entry
    
    177
    +  , lfResolved :: !(Maybe LayoutStartCol) -- ^ column of the first token printed in this scope
    
    178
    +  }
    
    179
    +
    
    175 180
     data EPState = EPState
    
    176 181
                  { uAnchorSpan :: !RealSrcSpan -- ^ in pre-changed AST
    
    177 182
                                               -- reference frame, from
    
    ... ... @@ -189,16 +194,14 @@ data EPState = EPState
    189 194
                                        -- end of the span.
    
    190 195
     
    
    191 196
                  -- Print phase
    
    192
    -             , epPos        :: !Pos -- ^ Current output position
    
    193
    -             , pMarkLayout  :: !Bool
    
    194
    -             , pLHS   :: !LayoutStartCol
    
    197
    +             , pPos         :: !Pos -- ^ Current output position
    
    198
    +             , pLayout      :: [LayoutFrame]
    
    195 199
     
    
    196 200
                  -- Delta phase
    
    197 201
                  , dPriorEndPosition :: !Pos -- ^ End of Position reached
    
    198 202
                                              -- when processing the
    
    199 203
                                              -- preceding element
    
    200
    -             , dMarkLayout :: !Bool
    
    201
    -             , dLHS        :: !LayoutStartCol
    
    204
    +             , dLayout     :: [LayoutFrame]
    
    202 205
     
    
    203 206
                  -- Shared
    
    204 207
                  , epComments :: ![Comment]
    
    ... ... @@ -2644,7 +2647,7 @@ instance ExactPrint (HsIPBinds GhcPs) where
    2644 2647
       getAnnotationEntry = const NoEntryVal
    
    2645 2648
       setAnnotationAnchor a _ _ _ = a
    
    2646 2649
     
    
    2647
    -  exact (IPBinds x binds) = setLayoutBoth $ do
    
    2650
    +  exact (IPBinds x binds) = do
    
    2648 2651
           binds' <- mapM markAnnotated binds
    
    2649 2652
           return (IPBinds x binds')
    
    2650 2653
     
    
    ... ... @@ -3018,7 +3021,7 @@ instance ExactPrint (HsExpr GhcPs) where
    3018 3021
         an0 <- markLensFun an lhsCaseAnnCase markEpToken
    
    3019 3022
         e' <- markAnnotated e
    
    3020 3023
         an1 <- markLensFun an0 lhsCaseAnnOf markEpToken
    
    3021
    -    alts' <- setLayoutBoth $ markAnnotated alts
    
    3024
    +    alts' <- markAnnotated alts
    
    3022 3025
         return (HsCase an1 e' alts')
    
    3023 3026
     
    
    3024 3027
       exact (HsIf an e1 e2 e3) = do
    
    ... ... @@ -3040,7 +3043,7 @@ instance ExactPrint (HsExpr GhcPs) where
    3040 3043
       exact (HsLet (tkLet, tkIn) binds e) = do
    
    3041 3044
         setLayoutBoth $ do -- Make sure the 'in' gets indented too
    
    3042 3045
           tkLet' <- markEpToken tkLet
    
    3043
    -      binds' <- setLayoutBoth $ markAnnotated binds
    
    3046
    +      binds' <- markAnnotated binds
    
    3044 3047
           tkIn' <- markEpToken tkIn
    
    3045 3048
           e' <- markAnnotated e
    
    3046 3049
           return (HsLet (tkLet',tkIn') binds' e')
    
    ... ... @@ -3302,7 +3305,7 @@ instance ExactPrint (HsUntypedSplice GhcPs) where
    3302 3305
         -- the colOffset for now.
    
    3303 3306
         -- TODO: use local?
    
    3304 3307
         oldOffset <- getLayoutOffsetP
    
    3305
    -    EPState{pMarkLayout} <- get
    
    3308
    +    pMarkLayout <- queryLayoutStack <$> gets pLayout
    
    3306 3309
         unless pMarkLayout $ setLayoutOffsetP 0
    
    3307 3310
         printStringAdvance
    
    3308 3311
                 -- Note: Lexer.x does not provide unicode alternative. 2017-02-26
    
    ... ... @@ -3525,7 +3528,7 @@ instance ExactPrint (HsCmd GhcPs) where
    3525 3528
       exact (HsCmdLet (tkLet, tkIn) binds e) = do
    
    3526 3529
         setLayoutBoth $ do -- Make sure the 'in' gets indented too
    
    3527 3530
           tkLet' <- markEpToken tkLet
    
    3528
    -      binds' <- setLayoutBoth $ markAnnotated binds
    
    3531
    +      binds' <- markAnnotated binds
    
    3529 3532
           tkIn' <- markEpToken tkIn
    
    3530 3533
           e' <- markAnnotated e
    
    3531 3534
           return (HsCmdLet (tkLet', tkIn') binds' e')
    
    ... ... @@ -5026,30 +5029,51 @@ printQueuedComment Comment{commentContents} dp = do
    5026 5029
     
    
    5027 5030
     ------------------------------------------------------------------------
    
    5028 5031
     
    
    5032
    +effectiveLayout :: [LayoutFrame] -> LayoutStartCol
    
    5033
    +effectiveLayout (f:_) = fromMaybe (lfSaved f) (lfResolved f)
    
    5034
    +effectiveLayout [] = LayoutStartCol 1
    
    5035
    +
    
    5036
    +popLayoutStack :: [LayoutFrame] -> [LayoutFrame]
    
    5037
    +popLayoutStack (_:rest) = rest
    
    5038
    +popLayoutStack [] = []
    
    5039
    +
    
    5040
    +queryLayoutStack :: [LayoutFrame] -> Bool
    
    5041
    +queryLayoutStack (f:_) = isNothing (lfResolved f)
    
    5042
    +queryLayoutStack [] = False
    
    5043
    +
    
    5044
    +
    
    5045
    +-- | Work down the stack filling in the resolved layout column for all
    
    5046
    +-- so-far-unresolved ones. These can only be in the prefix of the
    
    5047
    +-- list.
    
    5048
    +resolveLayoutStack :: [LayoutFrame] -> LayoutStartCol -> [LayoutFrame]
    
    5049
    +resolveLayoutStack (f:rest) c =
    
    5050
    +  case lfResolved f of
    
    5051
    +    Nothing -> f { lfResolved = Just c } : resolveLayoutStack rest c
    
    5052
    +    Just _  -> f:rest
    
    5053
    +resolveLayoutStack [] _ = []
    
    5054
    +
    
    5029 5055
     setLayoutBoth :: (Monad m, Monoid w) => EP w m a -> EP w m a
    
    5030
    -setLayoutBoth k = do
    
    5056
    +setLayoutBoth action = do
    
    5031 5057
       oldLHS <- getLayoutOffsetD
    
    5032 5058
       oldAnchorOffset <- getLayoutOffsetP
    
    5033 5059
       debugM $ "setLayoutBoth: (oldLHS,oldAnchorOffset)=" ++ show (oldLHS,oldAnchorOffset)
    
    5034
    -  modify (\a -> a { dMarkLayout = True
    
    5035
    -                  , pMarkLayout = True } )
    
    5060
    +  modify (\a -> a { dLayout = (LayoutFrame (effectiveLayout (dLayout a)) Nothing:dLayout a)
    
    5061
    +                  , pLayout = (LayoutFrame (effectiveLayout (pLayout a)) Nothing:pLayout a)} )
    
    5036 5062
       let reset = do
    
    5037
    -        debugM $ "setLayoutBoth:reset: (oldLHS,oldAnchorOffset)=" ++ show (oldLHS,oldAnchorOffset)
    
    5038
    -        modify (\a -> a { dMarkLayout = False
    
    5039
    -                        , dLHS = oldLHS
    
    5040
    -                        , pMarkLayout = False
    
    5041
    -                        , pLHS = oldAnchorOffset} )
    
    5042
    -  k <* reset
    
    5063
    +        debugM $ "pushLayout:reset: (oldLHS,oldAnchorOffset)=" ++ show (oldLHS,oldAnchorOffset)
    
    5064
    +        modify (\a -> a { dLayout = popLayoutStack (dLayout a)
    
    5065
    +                        , pLayout = popLayoutStack (pLayout a) })
    
    5066
    +  action <* reset
    
    5043 5067
     
    
    5044 5068
     ------------------------------------------------------------------------
    
    5045 5069
     
    
    5046 5070
     getPosP :: (Monad m, Monoid w) => EP w m Pos
    
    5047
    -getPosP = gets epPos
    
    5071
    +getPosP = gets pPos
    
    5048 5072
     
    
    5049 5073
     setPosP :: (Monad m, Monoid w) => Pos -> EP w m ()
    
    5050 5074
     setPosP l = do
    
    5051 5075
       debugM $ "setPosP:" ++ show l
    
    5052
    -  modify (\s -> s {epPos = l})
    
    5076
    +  modify (\s -> s {pPos = l})
    
    5053 5077
     
    
    5054 5078
     getExtraDP :: (Monad m, Monoid w) => EP w m (Maybe EpaLocation)
    
    5055 5079
     getExtraDP = gets uExtraDP
    
    ... ... @@ -5100,14 +5124,13 @@ setPriorEndASTPD pe@(fm,to) = do
    5100 5124
     
    
    5101 5125
     setLayoutStartD :: (Monad m, Monoid w) => Int -> EP w m ()
    
    5102 5126
     setLayoutStartD p = do
    
    5103
    -  EPState{dMarkLayout} <- get
    
    5127
    +  dMarkLayout <- queryLayoutStack <$> gets dLayout
    
    5104 5128
       when dMarkLayout $ do
    
    5105 5129
         debugM $ "setLayoutStartD: setting dLHS=" ++ show p
    
    5106
    -    modify (\s -> s { dMarkLayout = False
    
    5107
    -                    , dLHS = LayoutStartCol p})
    
    5130
    +    modify (\s -> s { dLayout = resolveLayoutStack (dLayout s) (LayoutStartCol p)})
    
    5108 5131
     
    
    5109 5132
     getLayoutOffsetD :: (Monad m, Monoid w) => EP w m LayoutStartCol
    
    5110
    -getLayoutOffsetD = gets dLHS
    
    5133
    +getLayoutOffsetD = effectiveLayout <$> gets dLayout
    
    5111 5134
     
    
    5112 5135
     setAnchorU :: (Monad m, Monoid w) => RealSrcSpan -> EP w m ()
    
    5113 5136
     setAnchorU rss = do
    
    ... ... @@ -5168,12 +5191,12 @@ applyComment c = do
    5168 5191
         (h:t) -> modify (\s -> s { epCommentsApplied = (c:h):t } )
    
    5169 5192
     
    
    5170 5193
     getLayoutOffsetP :: (Monad m, Monoid w) => EP w m LayoutStartCol
    
    5171
    -getLayoutOffsetP = gets pLHS
    
    5194
    +getLayoutOffsetP = effectiveLayout <$> gets pLayout
    
    5172 5195
     
    
    5173 5196
     setLayoutOffsetP :: (Monad m, Monoid w) => LayoutStartCol -> EP w m ()
    
    5174 5197
     setLayoutOffsetP c = do
    
    5175 5198
       debugM $ "setLayoutOffsetP:" ++ show c
    
    5176
    -  modify (\s -> s { pLHS = c })
    
    5199
    +  modify (\s -> s { pLayout = resolveLayoutStack (pLayout s) c})
    
    5177 5200
     
    
    5178 5201
     
    
    5179 5202
     -- ---------------------------------------------------------------------
    
    ... ... @@ -5208,11 +5231,12 @@ adjustDeltaForOffsetM dp = do
    5208 5231
     
    
    5209 5232
     printString :: (Monad m, Monoid w) => Bool -> String -> EP w m ()
    
    5210 5233
     printString layout str = do
    
    5211
    -  EPState{epPos = (_,c), pMarkLayout} <- get
    
    5234
    +  pMarkLayout <- queryLayoutStack <$> gets pLayout
    
    5235
    +  EPState{pPos = (_,c) } <- get
    
    5212 5236
       EPOptions{epTokenPrint, epWhitespacePrint} <- ask
    
    5213 5237
       when (pMarkLayout && layout) $ do
    
    5214 5238
         debugM $ "printString: setting pLHS to " ++ show c
    
    5215
    -    modify (\s -> s { pLHS = LayoutStartCol c, pMarkLayout = False } )
    
    5239
    +    setLayoutOffsetP (LayoutStartCol c)
    
    5216 5240
     
    
    5217 5241
       -- Advance position, taking care of any newlines in the string
    
    5218 5242
       let strDP = dpFromString str
    

  • utils/check-exact/Main.hs
    ... ... @@ -36,10 +36,7 @@ import GHC.Data.FastString
    36 36
     -- ---------------------------------------------------------------------
    
    37 37
     
    
    38 38
     _tt :: IO ()
    
    39
    --- _tt = testOneFile changers "/home/alanz/mysrc/git.haskell.org/worktree/master/_build/stage1/lib/"
    
    40
    -_tt = testOneFile changers "/home/alanz/mysrc/git.haskell.org/ghc/_build/stage1/lib/"
    
    41
    --- _tt = testOneFile changers "/home/alanz/mysrc/git.haskell.org/worktree/exactprint/_build/stage1/lib"
    
    42
    --- _tt = testOneFile changers "/home/alanz/mysrc/git.haskell.org/worktree/epw/_build/stage1/lib"
    
    39
    +_tt = testOneFile changers "../../_build/stage1/lib/"
    
    43 40
     
    
    44 41
      -- "../../testsuite/tests/ghc-api/exactprint/RenameCase1.hs" (Just changeRenameCase1)
    
    45 42
      -- "../../testsuite/tests/ghc-api/exactprint/LayoutLet2.hs" (Just changeLayoutLet2)