1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 10:15:55 +03:00

Define a patch helper to eliminate Patches.

This commit is contained in:
Rob Rix 2017-09-13 21:55:41 -04:00
parent cf9d752d43
commit 547da73009
4 changed files with 13 additions and 21 deletions

View File

@ -4,9 +4,7 @@ module Patch
( Patch(..)
, after
, before
, unPatch
, maybeFst
, maybeSnd
, patch
) where
import Data.Aeson
@ -29,25 +27,17 @@ data Patch a b
-- | Return the item from the after side of the patch.
after :: Patch before after -> Maybe after
after = maybeSnd . unPatch
after = patch (const Nothing) Just (\ _ b -> Just b)
-- | Return the item from the before side of the patch.
before :: Patch before after -> Maybe before
before = maybeFst . unPatch
before = patch Just (const Nothing) (\ a _ -> Just a)
-- | Return both sides of a patch.
unPatch :: Patch before after -> These before after
unPatch (Replace a b) = These a b
unPatch (Insert b) = That b
unPatch (Delete a) = This a
-- | Return Just the value in This, or the first value in These, if any.
maybeFst :: These a b -> Maybe a
maybeFst = these Just (const Nothing) ((Just .) . const)
-- | Return Just the value in That, or the second value in These, if any.
maybeSnd :: These a b -> Maybe b
maybeSnd = these (const Nothing) Just ((Just .) . flip const)
patch :: (before -> result) -> (after -> result) -> (before -> after -> result) -> Patch before after -> result
patch ifDelete _ _ (Delete a) = ifDelete a
patch _ ifInsert _ (Insert b) = ifInsert b
patch _ _ ifReplace (Replace a b) = ifReplace a b
-- Instances

View File

@ -23,7 +23,6 @@ import Data.Semigroup ((<>))
import Data.Source
import Data.These
import Diff
import Patch
import Prelude hiding (fst, snd)
import SplitDiff
@ -181,3 +180,6 @@ changeIncludingContext leadingContext rows = case changes of
-- | Whether a row has changes on either side.
rowHasChanges :: (Foldable f, Functor f) => Join These (SplitDiff f annotation) -> Bool
rowHasChanges row = or (hasChanges <$> row)
maybeSnd :: These a b -> Maybe b
maybeSnd = these (const Nothing) Just (\ _ a -> Just a)

View File

@ -156,7 +156,7 @@ tableOfContentsBy selector = fromMaybe [] . cata (\ r -> case r of
(Just a, Just []) -> Just [Changed a]
(_ , entries) -> entries)
where patchEntry = these Deleted Inserted (const Replaced) . unPatch
where patchEntry = patch Deleted Inserted (const Replaced)
termTableOfContentsBy :: (Foldable f, Functor f)
=> (forall b. TermF f annotation b -> Maybe a)

View File

@ -51,9 +51,9 @@ spec = parallel $ do
\ term -> tableOfContentsBy (Just . termAnnotation) (diffTerms term term) `shouldBe` [Unchanged (lastValue (term :: Term Syntax (Record '[Category])))]
prop "produces inserted/deleted/replaced entries for relevant nodes within patches" $
\ patch -> tableOfContentsBy (Just . termAnnotation) (these deleting inserting replacing (unPatch patch))
\ p -> tableOfContentsBy (Just . termAnnotation) (patch deleting inserting replacing p)
`shouldBe`
these (fmap Deleted) (fmap Inserted) (const (fmap Replaced)) (unPatch (bimap (foldMap pure) (foldMap pure) (patch :: Patch (Term Syntax Int) (Term Syntax Int))))
patch (fmap Deleted) (fmap Inserted) (const (fmap Replaced)) (bimap (foldMap pure) (foldMap pure) (p :: Patch (Term Syntax Int) (Term Syntax Int)))
prop "produces changed entries for relevant nodes containing irrelevant patches" $
\ diff -> let diff' = merge (0, 0) (Indexed [1 <$ (diff :: Diff Syntax Int)]) in