1
1
mirror of https://github.com/github/semantic.git synced 2025-01-03 13:02:37 +03:00

Add mapPatch

This commit is contained in:
joshvera 2016-08-08 14:19:47 -04:00
parent 51cb8918a9
commit d8ff761f40
2 changed files with 8 additions and 6 deletions

View File

@ -44,12 +44,8 @@ diffSummaries sources = para $ \diff ->
annotateWithCategory children = maybeToList (prependSummary (Both.snd sources) <$> (afterTerm diff')) <*> (children >>= snd) in annotateWithCategory children = maybeToList (prependSummary (Both.snd sources) <$> (afterTerm diff')) <*> (children >>= snd) in
case diff of case diff of
-- Skip comments and leaves since they don't have any changes -- Skip comments and leaves since they don't have any changes
Free (_ :< Leaf _) -> []
Free (_ :< (S.Comment _)) -> []
(Free (_ :< syntax)) -> annotateWithCategory (toList syntax) (Free (_ :< syntax)) -> annotateWithCategory (toList syntax)
(Pure (Insert term)) -> [ DiffSummary (Insert $ termToDiffInfo afterSource term) [] ] (Pure patch) -> [ DiffSummary (mapPatch (termToDiffInfo beforeSource) (termToDiffInfo afterSource) patch) [] ]
(Pure (Delete term)) -> [ DiffSummary (Delete $ termToDiffInfo beforeSource term) [] ]
(Pure (Replace t1 t2)) -> [ DiffSummary (Replace (termToDiffInfo beforeSource t1) (termToDiffInfo afterSource t2)) [] ]
where where
(beforeSource, afterSource) = runJoin sources (beforeSource, afterSource) = runJoin sources
@ -122,7 +118,7 @@ toTermName source term = case unwrap term of
maybeParentContext :: [(Category, Text)] -> Doc maybeParentContext :: [(Category, Text)] -> Doc
maybeParentContext annotations = case annotations of maybeParentContext annotations = case annotations of
[] -> "" [] -> ""
(annotation:xs) -> space <> "in the" <+> (toDoc $ snd annotation) <+> toDoc (toCategoryName $ fst annotation) (annotation:_) -> space <> "in the" <+> (toDoc $ snd annotation) <+> toDoc (toCategoryName $ fst annotation)
toDoc :: Text -> Doc toDoc :: Text -> Doc
toDoc = string . toS toDoc = string . toS

View File

@ -9,6 +9,7 @@ module Patch
, patchSum , patchSum
, maybeFst , maybeFst
, maybeSnd , maybeSnd
, mapPatch
) where ) where
import Data.These import Data.These
@ -51,6 +52,11 @@ unPatch (Replace a b) = These a b
unPatch (Insert b) = That b unPatch (Insert b) = That b
unPatch (Delete a) = This a unPatch (Delete a) = This a
mapPatch :: (a -> b) -> (a -> b) -> Patch a -> Patch b
mapPatch f _ (Delete a ) = Delete (f a)
mapPatch _ g (Insert b) = Insert (g b)
mapPatch f g (Replace a b) = Replace (f a) (g b)
-- | Calculate the cost of the patch given a function to compute the cost of a item. -- | Calculate the cost of the patch given a function to compute the cost of a item.
patchSum :: (a -> Integer) -> Patch a -> Integer patchSum :: (a -> Integer) -> Patch a -> Integer
patchSum termCost patch = maybe 0 termCost (before patch) + maybe 0 termCost (after patch) patchSum termCost patch = maybe 0 termCost (before patch) + maybe 0 termCost (after patch)