on 4e087ff help sign in
ormolu/src/Ormolu/Diff.hs
48 lines 1.4 kB history raw
1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE RankNTypes #-}
3{-# LANGUAGE RecordWildCards #-}
5-- | Diffing GHC ASTs modulo span positions.
7module Ormolu.Diff
8 ( diff
9 )
10where
12import Data.Generics
13import GHC
14import Ormolu.CommentStream
15import Ormolu.Imports (sortImports)
17-- | Return 'False' if two annotated ASTs are the same modulo span
18-- positions.
20diff
21 :: (CommentStream, ParsedSource) -- ^ First comment stream and AST
22 -> (CommentStream, ParsedSource) -- ^ Second comment stream and AST
23 -> Bool
24diff (cstream0, ps0) (cstream1, ps1) =
25 not (matchIgnoringSrcSpans cstream0 cstream1)
26 || not (matchIgnoringSrcSpans ps0 ps1)
28-- | Compare two values for equality disregarding differences in 'SrcSpan's
29-- and the ordering of import lists.
31matchIgnoringSrcSpans :: Data a => a -> a -> Bool
32matchIgnoringSrcSpans = genericQuery
33 where
34 genericQuery :: GenericQ (GenericQ Bool)
35 genericQuery x y = (toConstr x == toConstr y)
36 && and (gzipWithQ (genericQuery `extQ` srcSpanEq `extQ` hsModuleEq) x y)
38 srcSpanEq :: SrcSpan -> GenericQ Bool
39 srcSpanEq _ _ = True
41 hsModuleEq :: HsModule GhcPs -> GenericQ Bool
42 hsModuleEq hs0 hs1' =
43 case cast hs1' :: Maybe (HsModule GhcPs) of
44 Nothing -> False
45 Just hs1 ->
46 matchIgnoringSrcSpans
47 hs0 { hsmodImports = sortImports (hsmodImports hs0) }
48 hs1 { hsmodImports = sortImports (hsmodImports hs1) }