@@ -1,5 +1,6 @@
1{-# LANGUAGE DeriveDataTypeable #-}
1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE RankNTypes #-}
2{-# LANGUAGE RankNTypes #-}
3+{-# LANGUAGE RecordWildCards #-}
4-- | Diffing GHC ASTs modulo span positions.
5-- | Diffing GHC ASTs modulo span positions.
@@ -9,8 +10,11 @@ module Ormolu.Diff
11import Data.Generics
12import Data.Generics
13+import Data.Maybe (mapMaybe)
13import Language.Haskell.GHC.ExactPrint.Types
15import Language.Haskell.GHC.ExactPrint.Types
16+import Ormolu.Comments
17+import qualified Data.Map.Strict as M
15-- | Return 'False' of two annotated ASTs are the same modulo span
19-- | Return 'False' of two annotated ASTs are the same modulo span
16-- positions.
20-- positions.
@@ -19,17 +23,39 @@ diff
19 :: (Anns, ParsedSource) -- ^ First annotated AST
23 :: (Anns, ParsedSource) -- ^ First annotated AST
20 -> (Anns, ParsedSource) -- ^ Second annotated AST
24 -> (Anns, ParsedSource) -- ^ Second annotated AST
22−diff (_, ps0) (_, ps1) =
26+diff (anns0, ps0) (anns1, ps1) =
23− not (matchParsedSources ps0 ps1)
27+ not (matchIgnoringSrcSpans (simplifyAnns anns0) (simplifyAnns anns1))
28+ || not (matchIgnoringSrcSpans ps0 ps1)
25-- | Compare two 'ParsedSource' values disregarding differences in
30-- | Compare two 'ParsedSource' values disregarding differences in
26-- 'SrcSpan's.
31-- 'SrcSpan's.
28−matchParsedSources :: ParsedSource -> ParsedSource -> Bool
33+matchIgnoringSrcSpans :: Data a => a -> a -> Bool
29−matchParsedSources x0 y0 = geq' x0 y0
34+matchIgnoringSrcSpans x0 y0 = geq' x0 y0
31 geq' :: GenericQ (GenericQ Bool)
36 geq' :: GenericQ (GenericQ Bool)
32 geq' x y = (toConstr x == toConstr y)
37 geq' x y = (toConstr x == toConstr y)
33 && and (gzipWithQ (geq' `extQ` srcSpanEq) x y)
38 && and (gzipWithQ (geq' `extQ` srcSpanEq) x y)
34 srcSpanEq :: SrcSpan -> GenericQ Bool
39 srcSpanEq :: SrcSpan -> GenericQ Bool
35 srcSpanEq _ _ = True
40 srcSpanEq _ _ = True
42+-- | Simplified collection of 'Comment's.
44+data Comments = Comments [Comment] [Comment] [Comment]
45+ deriving (Show, Typeable, Data)
47+-- | Simplify a collection of annotations.
49+simplifyAnns :: Anns -> [Comments]
50+simplifyAnns = fmap simplifyAnn . M.elems
52+-- | Simplify single annotation.
54+simplifyAnn :: Annotation -> Comments
55+simplifyAnn Ann {..} = Comments
56+ (f . fst <$> annPriorComments)
57+ (f . fst <$> annFollowingComments)
58+ (mapMaybe (fmap f . annComment) (fst <$> annsDP))
60+ f (Comment str i o) =
61+ Comment (unlines $ normalizeComment str) i o