2015-11-20 00:35:06 +03:00
|
|
|
module Unified where
|
2015-11-20 01:36:32 +03:00
|
|
|
|
|
|
|
import Diff
|
2015-11-20 01:37:07 +03:00
|
|
|
import Patch
|
|
|
|
import Syntax
|
|
|
|
import Term
|
2015-11-20 02:53:23 +03:00
|
|
|
import Control.Arrow
|
2015-11-20 01:37:07 +03:00
|
|
|
import Control.Monad.Free
|
2015-11-20 02:53:23 +03:00
|
|
|
import Control.Comonad.Cofree
|
2015-11-20 02:59:24 +03:00
|
|
|
import Data.Map
|
2015-11-20 01:36:32 +03:00
|
|
|
|
2015-11-20 02:06:25 +03:00
|
|
|
unified :: Diff a Info -> String -> String -> String
|
2015-11-20 01:37:07 +03:00
|
|
|
unified diff before after =
|
2015-11-20 02:53:23 +03:00
|
|
|
fst $ iter f mapped where
|
|
|
|
mapped = fmap (unifiedPatch &&& range) diff
|
|
|
|
f (Annotated (_, Info range _) (Leaf _)) = (substring range after, Just range)
|
2015-11-20 02:58:41 +03:00
|
|
|
f (Annotated (_, Info range _) (Indexed i)) = (unifiedRange range i after, Just range)
|
2015-11-20 02:58:56 +03:00
|
|
|
f (Annotated (_, Info range _) (Fixed f)) = (unifiedRange range f after, Just range)
|
2015-11-20 02:53:23 +03:00
|
|
|
f (Annotated (_, Info range _) (Keyed k)) = ("", Just range)
|
|
|
|
|
2015-11-20 01:37:07 +03:00
|
|
|
unifiedPatch :: Patch (Term a annotation) -> String
|
|
|
|
unifiedPatch _ = ""
|
2015-11-20 02:53:37 +03:00
|
|
|
|
2015-11-20 02:08:11 +03:00
|
|
|
unifiedRange :: Range -> [(String, Maybe Range)] -> String -> String
|
2015-11-20 02:53:37 +03:00
|
|
|
unifiedRange range children source = out ++ substring Range { start = previous, end = end range } after where
|
2015-11-20 02:59:24 +03:00
|
|
|
(out, previous) = Prelude.foldl accumulateContext ("", start range) children
|
2015-11-20 02:56:58 +03:00
|
|
|
accumulateContext (out, previous) (child, Just range) = (out ++ substring Range { start = previous, end = start range } source ++ child, end range)
|
|
|
|
accumulateContext (out, previous) (child, _) = (out ++ child, previous)
|
2015-11-20 02:05:33 +03:00
|
|
|
|
|
|
|
substring :: Range -> String -> String
|
|
|
|
substring range = take (end range) . drop (start range)
|
2015-11-20 02:30:37 +03:00
|
|
|
|
|
|
|
range :: Patch (Term a Info) -> Maybe Range
|
|
|
|
range patch = range . extract <$> after patch where
|
|
|
|
extract (annotation :< _) = annotation
|
|
|
|
range (Info range _) = range
|