on 2dfd181 help sign in
ormolu/src/Ormolu/Diff.hs
55 lines 1.7 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 BasicTypes (SourceText)
13import Data.Generics
14import GHC
15import Ormolu.CommentStream
16import Ormolu.Imports (sortImports)
18-- | Return 'False' if two annotated ASTs are the same modulo span
19-- positions.
21diff
22 :: (CommentStream, ParsedSource) -- ^ First comment stream and AST
23 -> (CommentStream, ParsedSource) -- ^ Second comment stream and AST
24 -> Bool
25diff (cstream0, ps0) (cstream1, ps1) =
26 not (matchIgnoringSrcSpans cstream0 cstream1)
27 || not (matchIgnoringSrcSpans ps0 ps1)
29-- | Compare two values for equality disregarding differences in 'SrcSpan's
30-- and the ordering of import lists.
32matchIgnoringSrcSpans :: Data a => a -> a -> Bool
33matchIgnoringSrcSpans = genericQuery
34 where
35 genericQuery :: GenericQ (GenericQ Bool)
36 genericQuery x y = (toConstr x == toConstr y)
37 && and (gzipWithQ (genericQuery
38 `extQ` srcSpanEq
39 `extQ` hsModuleEq
40 `extQ` sourceTextEq) x y)
42 srcSpanEq :: SrcSpan -> GenericQ Bool
43 srcSpanEq _ _ = True
45 hsModuleEq :: HsModule GhcPs -> GenericQ Bool
46 hsModuleEq hs0 hs1' =
47 case cast hs1' :: Maybe (HsModule GhcPs) of
48 Nothing -> False
49 Just hs1 ->
50 matchIgnoringSrcSpans
51 hs0 { hsmodImports = sortImports (hsmodImports hs0) }
52 hs1 { hsmodImports = sortImports (hsmodImports hs1) }
54 sourceTextEq :: SourceText -> GenericQ Bool
55 sourceTextEq _ _ = True