on d82d607 help sign in
ormolu/src/Ormolu/Diff.hs
57 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.Imports (sortImports)
16import Ormolu.Parser.Result
18-- | Return 'False' if two annotated ASTs are the same modulo span
19-- positions.
21diff :: ParseResult -> ParseResult -> Bool
22diff ParseResult { prCommentStream = cstream0
23 , prParsedSource = ps0
24 }
25 ParseResult { prCommentStream = cstream1
26 , prParsedSource = ps1
27 } =
28 not (matchIgnoringSrcSpans cstream0 cstream1)
29 || not (matchIgnoringSrcSpans ps0 ps1)
31-- | Compare two values for equality disregarding differences in 'SrcSpan's
32-- and the ordering of import lists.
34matchIgnoringSrcSpans :: Data a => a -> a -> Bool
35matchIgnoringSrcSpans = genericQuery
36 where
37 genericQuery :: GenericQ (GenericQ Bool)
38 genericQuery x y = (toConstr x == toConstr y)
39 && and (gzipWithQ (genericQuery
40 `extQ` srcSpanEq
41 `extQ` hsModuleEq
42 `extQ` sourceTextEq) x y)
44 srcSpanEq :: SrcSpan -> GenericQ Bool
45 srcSpanEq _ _ = True
47 hsModuleEq :: HsModule GhcPs -> GenericQ Bool
48 hsModuleEq hs0 hs1' =
49 case cast hs1' :: Maybe (HsModule GhcPs) of
50 Nothing -> False
51 Just hs1 ->
52 matchIgnoringSrcSpans
53 hs0 { hsmodImports = sortImports (hsmodImports hs0) }
54 hs1 { hsmodImports = sortImports (hsmodImports hs1) }
56 sourceTextEq :: SourceText -> GenericQ Bool
57 sourceTextEq _ _ = True