2016-03-14 21:13:06 +03:00
|
|
|
|
{-# LANGUAGE RankNTypes #-}
|
2016-03-11 20:29:17 +03:00
|
|
|
|
module Alignment
|
2016-03-14 21:13:06 +03:00
|
|
|
|
( hasChanges
|
2016-03-11 20:29:17 +03:00
|
|
|
|
, numberedRows
|
2016-03-22 15:20:56 +03:00
|
|
|
|
, AlignedDiff
|
|
|
|
|
, alignDiff
|
2016-03-24 20:53:49 +03:00
|
|
|
|
, groupChildrenByLine
|
2016-03-11 20:29:17 +03:00
|
|
|
|
) where
|
2016-02-28 22:01:56 +03:00
|
|
|
|
|
2016-04-15 19:44:04 +03:00
|
|
|
|
import Control.Applicative
|
2016-04-15 04:47:18 +03:00
|
|
|
|
import Control.Arrow ((&&&), (***))
|
2016-02-28 22:01:56 +03:00
|
|
|
|
import Control.Comonad.Cofree
|
2016-03-05 04:18:49 +03:00
|
|
|
|
import Control.Monad
|
2016-02-28 22:01:56 +03:00
|
|
|
|
import Control.Monad.Free
|
2016-03-10 23:06:03 +03:00
|
|
|
|
import Data.Align
|
2016-04-14 23:34:49 +03:00
|
|
|
|
import Data.Biapplicative
|
2016-03-18 21:30:22 +03:00
|
|
|
|
import Data.Bifunctor.Join
|
2016-03-03 07:11:24 +03:00
|
|
|
|
import Data.Copointed
|
2016-03-12 01:59:58 +03:00
|
|
|
|
import Data.Foldable
|
2016-04-04 23:25:16 +03:00
|
|
|
|
import Data.Functor.Both as Both
|
2016-02-28 22:01:56 +03:00
|
|
|
|
import Data.Functor.Identity
|
2016-03-03 15:53:23 +03:00
|
|
|
|
import Data.Maybe
|
2016-04-15 18:57:26 +03:00
|
|
|
|
import Data.Monoid
|
2016-04-15 05:00:00 +03:00
|
|
|
|
import Data.These
|
2016-02-28 22:01:56 +03:00
|
|
|
|
import Diff
|
2016-03-31 00:26:52 +03:00
|
|
|
|
import Info
|
2016-02-28 22:01:56 +03:00
|
|
|
|
import Patch
|
2016-03-01 03:39:04 +03:00
|
|
|
|
import Prelude hiding (fst, snd)
|
2016-02-28 22:01:56 +03:00
|
|
|
|
import Range
|
2016-03-29 00:55:01 +03:00
|
|
|
|
import Source hiding (fromList, uncons, (++))
|
2016-02-28 22:01:56 +03:00
|
|
|
|
import SplitDiff
|
|
|
|
|
import Syntax
|
|
|
|
|
import Term
|
|
|
|
|
|
2016-03-02 00:29:42 +03:00
|
|
|
|
-- | Assign line numbers to the lines on each side of a list of rows.
|
2016-04-15 04:16:02 +03:00
|
|
|
|
numberedRows :: [Join These a] -> [Join These (Int, a)]
|
|
|
|
|
numberedRows = countUp (Join $ These 1 1)
|
|
|
|
|
where countUp from (row : rows) = fromJust ((,) <$> from `applyThese` row) : countUp (succ <$> from) rows
|
2016-03-08 01:13:51 +03:00
|
|
|
|
countUp _ [] = []
|
2016-03-02 00:29:42 +03:00
|
|
|
|
|
2016-03-02 16:05:18 +03:00
|
|
|
|
-- | Determine whether a line contains any patches.
|
2016-04-15 04:16:02 +03:00
|
|
|
|
hasChanges :: SplitDiff leaf Info -> Bool
|
|
|
|
|
hasChanges = or . (True <$)
|
2016-03-09 09:36:40 +03:00
|
|
|
|
|
2016-03-22 15:20:56 +03:00
|
|
|
|
type AlignedDiff leaf = [Join These (SplitDiff leaf Info)]
|
2016-03-18 19:02:07 +03:00
|
|
|
|
|
2016-04-15 00:03:53 +03:00
|
|
|
|
alignDiff :: Both (Source Char) -> Diff leaf Info -> AlignedDiff leaf
|
2016-04-15 03:27:24 +03:00
|
|
|
|
alignDiff sources diff = iter (uncurry (alignSyntax (runBothWith ((Join .) . These)) ((Free .) . Annotated) getRange sources) . (annotation &&& syntax)) (alignPatch sources <$> diff)
|
2016-04-15 00:03:53 +03:00
|
|
|
|
|
2016-03-18 21:15:49 +03:00
|
|
|
|
alignPatch :: Both (Source Char) -> Patch (Term leaf Info) -> AlignedDiff leaf
|
2016-04-15 03:27:24 +03:00
|
|
|
|
alignPatch sources patch = case patch of
|
2016-04-15 16:37:01 +03:00
|
|
|
|
Delete term -> fmap (Pure . SplitDelete) <$> hylo (alignSyntax this (:<) getRange (Identity (fst sources))) unCofree (Identity <$> term)
|
|
|
|
|
Insert term -> fmap (Pure . SplitInsert) <$> hylo (alignSyntax that (:<) getRange (Identity (snd sources))) unCofree (Identity <$> term)
|
2016-04-15 03:27:24 +03:00
|
|
|
|
Replace term1 term2 -> fmap (Pure . SplitReplace) <$> alignWith (fmap (these id id const . runJoin) . Join)
|
|
|
|
|
(hylo (alignSyntax this (:<) getRange (Identity (fst sources))) unCofree (Identity <$> term1))
|
|
|
|
|
(hylo (alignSyntax that (:<) getRange (Identity (snd sources))) unCofree (Identity <$> term2))
|
|
|
|
|
where getRange = characterRange . copoint
|
|
|
|
|
this = Join . This . runIdentity
|
|
|
|
|
that = Join . That . runIdentity
|
|
|
|
|
|
|
|
|
|
alignSyntax :: Applicative f => (forall a. f a -> Join These a) -> (Info -> Syntax leaf term -> term) -> (term -> Range) -> f (Source Char) -> f Info -> Syntax leaf [Join These term] -> [Join These term]
|
|
|
|
|
alignSyntax toJoinThese toNode getRange sources infos syntax = case syntax of
|
2016-04-05 00:18:29 +03:00
|
|
|
|
Leaf s -> catMaybes $ wrapInBranch (const (Leaf s)) . fmap (flip (,) []) <$> sequenceL lineRanges
|
2016-04-15 03:27:24 +03:00
|
|
|
|
Indexed children -> catMaybes $ wrapInBranch Indexed <$> groupChildrenByLine getRange lineRanges children
|
|
|
|
|
Fixed children -> catMaybes $ wrapInBranch Fixed <$> groupChildrenByLine getRange lineRanges children
|
2016-04-15 20:00:42 +03:00
|
|
|
|
Keyed children -> catMaybes $ wrapInBranch Fixed <$> groupChildrenByLine getRange lineRanges children
|
2016-04-05 00:18:29 +03:00
|
|
|
|
where lineRanges = toJoinThese $ actualLineRanges <$> (characterRange <$> infos) <*> sources
|
2016-04-15 03:27:24 +03:00
|
|
|
|
wrapInBranch constructor = applyThese $ toJoinThese ((\ info (range, children) -> toNode (info { characterRange = range }) (constructor children)) <$> infos)
|
2016-03-22 15:20:56 +03:00
|
|
|
|
|
2016-04-15 20:00:42 +03:00
|
|
|
|
groupChildrenByLine :: Foldable f => (term -> Range) -> Join These [Range] -> f [Join These term] -> [Join These (Range, [term])]
|
2016-04-15 03:27:24 +03:00
|
|
|
|
groupChildrenByLine getRange ranges children | not (and $ null <$> ranges)
|
|
|
|
|
, (nextRanges, nextChildren, lines) <- alignChildrenInRanges getRange ranges children
|
|
|
|
|
= lines ++ groupChildrenByLine getRange nextRanges nextChildren
|
|
|
|
|
| otherwise = []
|
2016-03-29 01:27:01 +03:00
|
|
|
|
|
2016-04-15 20:00:42 +03:00
|
|
|
|
alignChildrenInRanges :: Foldable f => (term -> Range) -> Join These [Range] -> f [Join These term] -> (Join These [Range], [[Join These term]], [Join These (Range, [term])])
|
2016-04-15 03:27:24 +03:00
|
|
|
|
alignChildrenInRanges getRange ranges children
|
2016-04-14 20:19:18 +03:00
|
|
|
|
| Just headRanges <- sequenceL $ listToMaybe <$> ranges
|
2016-04-15 03:27:24 +03:00
|
|
|
|
, (intersecting, nonintersecting) <- spanAndSplitFirstLines (intersects getRange headRanges) children
|
2016-04-14 20:19:18 +03:00
|
|
|
|
, (thisLine, nextLines) <- foldr (\ (this, next) (these, nexts) -> (this : these, next ++ nexts)) ([], []) intersecting
|
2016-04-15 19:45:33 +03:00
|
|
|
|
, thisRanges <- fromMaybe headRanges $ const <$> headRanges `applyThese` unionThese (thisLine ++ nextLines)
|
|
|
|
|
, merged <- pairRangesWithLine thisRanges (modifyJoin (uncurry These . fromThese [] []) (unionThese thisLine))
|
|
|
|
|
, advance <- fromThese id id . runJoin . (drop 1 <$) $ unionThese nextLines
|
2016-04-15 03:27:24 +03:00
|
|
|
|
, (nextRanges, nextChildren, nextLines) <- alignChildrenInRanges getRange (modifyJoin (uncurry bimap advance) ranges) (nextLines : nonintersecting)
|
2016-04-14 20:19:18 +03:00
|
|
|
|
= (nextRanges, nextChildren, merged : nextLines)
|
2016-04-15 17:00:42 +03:00
|
|
|
|
| otherwise = ([] <$ ranges, toList children, fmap (flip (,) []) <$> sequenceL ranges)
|
2016-03-31 21:45:19 +03:00
|
|
|
|
|
2016-04-15 20:00:42 +03:00
|
|
|
|
spanAndSplitFirstLines :: Foldable f => (Join These a -> Join These Bool) -> f [Join These a] -> ([(Join These a, [Join These a])], [[Join These a]])
|
2016-04-13 18:47:35 +03:00
|
|
|
|
spanAndSplitFirstLines pred = foldr go ([], [])
|
2016-04-13 23:05:48 +03:00
|
|
|
|
where go child (intersecting, nonintersecting)
|
2016-04-15 16:31:43 +03:00
|
|
|
|
| (first : rest) <- child = let ~(l, r) = splitThese first in
|
2016-04-13 23:05:48 +03:00
|
|
|
|
case fromThese False False . runJoin $ pred first of
|
|
|
|
|
(True, True) -> ((first, rest) : intersecting, nonintersecting)
|
2016-04-15 16:33:30 +03:00
|
|
|
|
(True, False) -> ((fromJust l, maybeToList r ++ rest) : intersecting, nonintersecting)
|
|
|
|
|
(False, True) -> ((fromJust r, maybeToList l ++ rest) : intersecting, nonintersecting)
|
2016-04-13 23:05:48 +03:00
|
|
|
|
_ -> (intersecting, (first : rest) : nonintersecting)
|
2016-04-13 23:05:56 +03:00
|
|
|
|
| otherwise = (intersecting, nonintersecting)
|
2016-04-13 18:08:01 +03:00
|
|
|
|
|
2016-04-15 19:45:33 +03:00
|
|
|
|
unionThese :: (Alternative f, Foldable f, Monoid (f a)) => f (Join These a) -> Join These (f a)
|
|
|
|
|
unionThese as = fromMaybe (Join (These empty empty)) . getUnion . fold $ Union . Just . fmap pure <$> as
|
2016-04-14 16:59:46 +03:00
|
|
|
|
|
2016-04-01 17:56:44 +03:00
|
|
|
|
pairRangesWithLine :: Monoid b => Join These a -> Join These b -> Join These (a, b)
|
|
|
|
|
pairRangesWithLine headRanges childLine = fromMaybe (flip (,) mempty <$> headRanges) $ (,) <$> headRanges `applyThese` childLine
|
2016-04-01 17:55:25 +03:00
|
|
|
|
|
2016-04-15 16:35:41 +03:00
|
|
|
|
-- | Test ranges and terms for intersection on either or both sides.
|
2016-04-15 03:27:24 +03:00
|
|
|
|
intersects :: (term -> Range) -> Join These Range -> Join These term -> Join These Bool
|
|
|
|
|
intersects getRange ranges line = fromMaybe (False <$ line) $ intersectsRange <$> ranges `applyThese` (getRange <$> line)
|
2016-03-29 01:27:01 +03:00
|
|
|
|
|
2016-04-15 16:31:28 +03:00
|
|
|
|
-- | Split a These value up into independent These values representing the left and right sides, if any.
|
2016-04-15 16:33:30 +03:00
|
|
|
|
splitThese :: Join These a -> (Maybe (Join These a), Maybe (Join These a))
|
|
|
|
|
splitThese these = fromThese Nothing Nothing $ bimap (Just . Join . This) (Just . Join . That) (runJoin these)
|
2016-03-29 01:46:22 +03:00
|
|
|
|
|
|
|
|
|
infixl 4 `applyThese`
|
|
|
|
|
|
2016-04-15 16:29:23 +03:00
|
|
|
|
-- | Like `<*>`, but it returns its result in `Maybe` since the result is the intersection of the shapes of the inputs.
|
2016-03-29 01:46:22 +03:00
|
|
|
|
applyThese :: Join These (a -> b) -> Join These a -> Maybe (Join These b)
|
2016-04-15 16:29:23 +03:00
|
|
|
|
applyThese (Join fg) (Join ab) = fmap Join . uncurry maybeThese $ uncurry (***) (bimap (<*>) (<*>) (unpack fg)) (unpack ab)
|
|
|
|
|
where unpack = fromThese Nothing Nothing . bimap Just Just
|
2016-03-29 16:58:31 +03:00
|
|
|
|
|
2016-04-15 16:30:20 +03:00
|
|
|
|
-- Map over the bifunctor inside a Join, producing another Join.
|
2016-03-24 21:18:53 +03:00
|
|
|
|
modifyJoin :: (p a a -> q b b) -> Join p a -> Join q b
|
2016-03-24 21:26:25 +03:00
|
|
|
|
modifyJoin f = Join . f . runJoin
|
2016-04-15 04:47:18 +03:00
|
|
|
|
|
|
|
|
|
-- | Given a pair of Maybes, produce a These containing Just their values, or Nothing if they haven’t any.
|
|
|
|
|
maybeThese :: Maybe a -> Maybe b -> Maybe (These a b)
|
|
|
|
|
maybeThese (Just a) (Just b) = Just (These a b)
|
|
|
|
|
maybeThese (Just a) _ = Just (This a)
|
|
|
|
|
maybeThese _ (Just b) = Just (That b)
|
|
|
|
|
maybeThese _ _ = Nothing
|
|
|
|
|
|
2016-04-15 19:15:47 +03:00
|
|
|
|
-- | A Monoid wrapping Join These, for which mappend is the smallest shape covering both arguments.
|
|
|
|
|
newtype Union a = Union { getUnion :: Maybe (Join These a) }
|
2016-04-15 16:23:41 +03:00
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
|
|
|
|
|
|
2016-04-15 16:26:16 +03:00
|
|
|
|
-- | Instances
|
|
|
|
|
|
2016-04-15 19:15:47 +03:00
|
|
|
|
instance Monoid a => Monoid (Union a) where
|
2016-04-15 04:47:18 +03:00
|
|
|
|
mempty = Union Nothing
|
2016-04-15 19:15:47 +03:00
|
|
|
|
Union (Just a) `mappend` Union (Just b) = Union $ Join <$> uncurry maybeThese (uncurry (***) (bimap mappend mappend (unpack a)) (unpack b))
|
|
|
|
|
where unpack = fromThese Nothing Nothing . runJoin . fmap Just
|
2016-04-15 04:47:18 +03:00
|
|
|
|
Union (Just a) `mappend` _ = Union $ Just a
|
|
|
|
|
Union _ `mappend` Union (Just b) = Union $ Just b
|
|
|
|
|
_ `mappend` _ = Union Nothing
|
2016-04-15 16:26:16 +03:00
|
|
|
|
|
|
|
|
|
instance Bicrosswalk t => Crosswalk (Join t) where
|
|
|
|
|
crosswalk f = fmap Join . bicrosswalk f f . runJoin
|