1{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE RankNTypes #-}
3{-# LANGUAGE RecordWildCards #-}
5-- | Diffing GHC ASTs modulo span positions.
13import BasicTypes (SourceText)
14import Data.ByteString (ByteString)
17import Ormolu.Imports (sortImports)
18import Ormolu.Parser.Result
20-- | Result of comparing two 'ParseResult's.
23 = Same -- ^ Two parse results are the same
24 | Different [SrcSpan] -- ^ Two parse results differ
26instance Semigroup Diff where
29 Different xs <> Different ys = Different (xs ++ ys)
31instance Monoid Diff where
34-- | Return 'False' if two annotated ASTs are the same modulo span
37diff :: ParseResult -> ParseResult -> Diff
38diff ParseResult { prCommentStream = cstream0
39 , prParsedSource = ps0
41 ParseResult { prCommentStream = cstream1
42 , prParsedSource = ps1
44 matchIgnoringSrcSpans cstream0 cstream1 <>
45 matchIgnoringSrcSpans ps0 ps1
47-- | Compare two values for equality disregarding differences in 'SrcSpan's
48-- and the ordering of import lists.
50matchIgnoringSrcSpans :: Data a => a -> a -> Diff
51matchIgnoringSrcSpans = genericQuery
53 genericQuery :: GenericQ (GenericQ Diff)
55 -- NOTE 'ByteString' implement 'Data' instance manually and does not
56 -- implement 'toConstr', so we have to deal with it in a special way.
57 | Just x' <- cast x, Just y' <- cast y =
58 if x' == (y' :: ByteString)
61 | typeOf x == typeOf y, toConstr x == toConstr y =
69 | otherwise = Different []
71 srcSpanEq :: SrcSpan -> GenericQ Diff
74 hsModuleEq :: HsModule GhcPs -> GenericQ Diff
76 case cast hs1' :: Maybe (HsModule GhcPs) of
77 Nothing -> Different []
80 hs0 { hsmodImports = sortImports (hsmodImports hs0) }
81 hs1 { hsmodImports = sortImports (hsmodImports hs1) }
83 sourceTextEq :: SourceText -> GenericQ Diff
84 sourceTextEq _ _ = Same
90 forLocated x@(L mspn _) y =
91 maybe id appendSpan (cast mspn) (genericQuery x y)
93 appendSpan :: SrcSpan -> Diff -> Diff
94 appendSpan s (Different ss) | fresh && helpful = Different (s:ss)
96 fresh = not $ any (flip isSubspanOf s) ss
97 helpful = isGoodSrcSpan s
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list