on master help sign in
Implement diffing of ASTs
commit41a54caca1f178b8dfba2868507165780c3e1bec authormrkkrp <mark.karpov@tweag.io> dateThu 21 Feb 2019, 18:42 (8 years ago) parent120b4ca
2 files changed+19−1
ormolu.cabal+1 src/Ormolu/Diff.hs+18−1
ormolu.cabal+1
@@ -34,6 +34,7 @@ library
3434 , ghc-boot-th >= 8.4.3
3535 , ghc-exactprint >= 0.5.6
3636 , mtl >= 2.0 && < 3.0
37+ , syb >= 0.7 && < 0.8
3738 , text >= 0.2 && < 1.3
3839 exposed-modules: Ormolu
3940 , Ormolu.Config
src/Ormolu/Diff.hs+18−1
@@ -1,3 +1,6 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE RankNTypes #-}
3+
14-- | Diffing GHC ASTs modulo span positions.
25
36module Ormolu.Diff
@@ -5,6 +8,7 @@ module Ormolu.Diff
58 )
69where
710
11+import Data.Generics
812import GHC
913import Language.Haskell.GHC.ExactPrint.Types
1014
@@ -15,4 +19,17 @@ diff
1519 :: (Anns, ParsedSource) -- ^ First annotated AST
1620 -> (Anns, ParsedSource) -- ^ Second annotated AST
1721 -> Bool
18−diff _ _ = False -- TODO
22+diff (_, ps0) (_, ps1) =
23+ not (matchParsedSources ps0 ps1)
24+
25+-- | Compare two 'ParsedSource' values disregarding differences in
26+-- 'SrcSpan's.
27+
28+matchParsedSources :: ParsedSource -> ParsedSource -> Bool
29+matchParsedSources x0 y0 = geq' x0 y0
30+ where
31+ geq' :: GenericQ (GenericQ Bool)
32+ geq' x y = (toConstr x == toConstr y)
33+ && and (gzipWithQ (geq' `extQ` srcSpanEq) x y)
34+ srcSpanEq :: SrcSpan -> GenericQ Bool
35+ srcSpanEq _ _ = True