2016-05-20 23:13:15 +03:00
|
|
|
{-# LANGUAGE FlexibleInstances, RecordWildCards #-}
|
2016-02-28 22:05:19 +03:00
|
|
|
module AlignmentSpec where
|
2015-12-17 21:08:47 +03:00
|
|
|
|
2016-02-28 22:01:56 +03:00
|
|
|
import Alignment
|
2016-04-26 22:44:11 +03:00
|
|
|
import ArbitraryTerm ()
|
2016-05-20 19:39:56 +03:00
|
|
|
import Control.Arrow ((&&&))
|
2015-12-17 21:08:47 +03:00
|
|
|
import Control.Comonad.Cofree
|
2016-04-01 17:13:22 +03:00
|
|
|
import Control.Monad.Free
|
2016-05-14 01:25:42 +03:00
|
|
|
import Data.Align hiding (align)
|
2016-03-22 02:08:02 +03:00
|
|
|
import Data.Bifunctor.Join
|
2016-04-25 19:54:51 +03:00
|
|
|
import Data.Foldable (toList)
|
2016-02-29 18:30:33 +03:00
|
|
|
import Data.Functor.Both as Both
|
2016-04-15 02:34:59 +03:00
|
|
|
import Data.Functor.Identity
|
2016-04-25 21:51:25 +03:00
|
|
|
import Data.Maybe (catMaybes, fromMaybe)
|
2016-04-26 21:54:24 +03:00
|
|
|
import Data.Monoid
|
2016-04-26 21:53:50 +03:00
|
|
|
import Data.Text.Arbitrary ()
|
2016-04-15 05:00:00 +03:00
|
|
|
import Data.These
|
2015-12-18 23:03:54 +03:00
|
|
|
import Diff
|
2016-03-31 00:33:07 +03:00
|
|
|
import Info
|
2016-04-15 02:40:36 +03:00
|
|
|
import Patch
|
2016-03-01 03:39:04 +03:00
|
|
|
import Prelude hiding (fst, snd)
|
2016-04-26 21:54:24 +03:00
|
|
|
import qualified Prelude
|
2015-12-18 23:03:54 +03:00
|
|
|
import Range
|
2016-03-03 23:40:41 +03:00
|
|
|
import qualified Source
|
2016-03-05 03:53:47 +03:00
|
|
|
import SplitDiff
|
2015-12-17 21:08:47 +03:00
|
|
|
import Syntax
|
2016-05-19 19:12:11 +03:00
|
|
|
import Term
|
2016-04-26 21:53:50 +03:00
|
|
|
import Test.Hspec
|
2016-04-26 22:44:11 +03:00
|
|
|
import Test.Hspec.QuickCheck
|
2016-05-20 22:34:20 +03:00
|
|
|
import Test.QuickCheck
|
2015-12-17 21:08:47 +03:00
|
|
|
|
|
|
|
spec :: Spec
|
2016-01-05 18:38:51 +03:00
|
|
|
spec = parallel $ do
|
2016-05-19 20:40:10 +03:00
|
|
|
describe "alignBranch" $ do
|
2016-03-24 20:54:11 +03:00
|
|
|
it "produces symmetrical context" $
|
2016-05-19 20:40:10 +03:00
|
|
|
alignBranch getRange ([] :: [Identity [Join These (SplitDiff String Info)]]) (both [Range 0 2, Range 2 4] [Range 0 2, Range 2 4]) `shouldBe`
|
2016-04-22 19:18:40 +03:00
|
|
|
[ Join (These (Range 0 2, [])
|
2016-03-24 20:53:58 +03:00
|
|
|
(Range 0 2, []))
|
|
|
|
, Join (These (Range 2 4, [])
|
|
|
|
(Range 2 4, []))
|
|
|
|
]
|
|
|
|
|
2016-04-05 01:19:58 +03:00
|
|
|
it "produces asymmetrical context" $
|
2016-05-19 20:40:10 +03:00
|
|
|
alignBranch getRange ([] :: [Identity [Join These (SplitDiff String Info)]]) (both [Range 0 2, Range 2 4] [Range 0 1]) `shouldBe`
|
2016-04-22 19:18:40 +03:00
|
|
|
[ Join (These (Range 0 2, [])
|
2016-03-24 20:56:07 +03:00
|
|
|
(Range 0 1, []))
|
|
|
|
, Join (This (Range 2 4, []))
|
|
|
|
]
|
|
|
|
|
2016-05-20 20:05:38 +03:00
|
|
|
prop "covers every input line" $
|
|
|
|
pendingWith "TBD"
|
|
|
|
|
|
|
|
prop "covers every input child" $
|
|
|
|
pendingWith "TBD"
|
|
|
|
|
|
|
|
prop "covers every line of every input child" $
|
|
|
|
pendingWith "TBD"
|
|
|
|
|
2016-03-22 02:08:02 +03:00
|
|
|
describe "alignDiff" $ do
|
2016-03-24 20:44:57 +03:00
|
|
|
it "aligns identical branches on a single line" $
|
2016-05-14 01:27:38 +03:00
|
|
|
let sources = both (Source.fromList "[ foo ]") (Source.fromList "[ foo ]") in
|
2016-05-19 19:12:11 +03:00
|
|
|
align sources (pure (info 0 7) `branch` [ pure (info 2 5) `leaf` "foo" ]) `shouldBe` prettyDiff sources
|
2016-03-24 20:05:44 +03:00
|
|
|
[ Join (These (info 0 7 `branch` [ info 2 5 `leaf` "foo" ])
|
|
|
|
(info 0 7 `branch` [ info 2 5 `leaf` "foo" ])) ]
|
|
|
|
|
2016-03-24 20:44:57 +03:00
|
|
|
it "aligns identical branches spanning multiple lines" $
|
2016-05-14 01:27:38 +03:00
|
|
|
let sources = both (Source.fromList "[\nfoo\n]") (Source.fromList "[\nfoo\n]") in
|
2016-05-19 19:12:11 +03:00
|
|
|
align sources (pure (info 0 7) `branch` [ pure (info 2 5) `leaf` "foo" ]) `shouldBe` prettyDiff sources
|
2016-03-24 20:05:52 +03:00
|
|
|
[ Join (These (info 0 2 `branch` [])
|
|
|
|
(info 0 2 `branch` []))
|
|
|
|
, Join (These (info 2 6 `branch` [ info 2 5 `leaf` "foo" ])
|
|
|
|
(info 2 6 `branch` [ info 2 5 `leaf` "foo" ]))
|
|
|
|
, Join (These (info 6 7 `branch` [])
|
|
|
|
(info 6 7 `branch` []))
|
|
|
|
]
|
|
|
|
|
2016-03-24 20:44:57 +03:00
|
|
|
it "aligns reformatted branches" $
|
2016-05-14 01:27:38 +03:00
|
|
|
let sources = both (Source.fromList "[ foo ]") (Source.fromList "[\nfoo\n]") in
|
2016-05-19 19:12:11 +03:00
|
|
|
align sources (pure (info 0 7) `branch` [ pure (info 2 5) `leaf` "foo" ]) `shouldBe` prettyDiff sources
|
2016-05-13 17:51:52 +03:00
|
|
|
[ Join (That (info 0 2 `branch` []))
|
|
|
|
, Join (These (info 0 7 `branch` [ info 2 5 `leaf` "foo" ])
|
|
|
|
(info 2 6 `branch` [ info 2 5 `leaf` "foo" ]))
|
2016-03-22 07:17:20 +03:00
|
|
|
, Join (That (info 6 7 `branch` []))
|
2016-03-22 02:08:02 +03:00
|
|
|
]
|
|
|
|
|
2016-03-24 20:44:57 +03:00
|
|
|
it "aligns nodes following reformatted branches" $
|
2016-05-14 01:27:38 +03:00
|
|
|
let sources = both (Source.fromList "[ foo ]\nbar\n") (Source.fromList "[\nfoo\n]\nbar\n") in
|
2016-05-19 19:12:11 +03:00
|
|
|
align sources (pure (info 0 12) `branch` [ pure (info 0 7) `branch` [ pure (info 2 5) `leaf` "foo" ], pure (info 8 11) `leaf` "bar" ]) `shouldBe` prettyDiff sources
|
2016-05-13 17:51:52 +03:00
|
|
|
[ Join (That (info 0 2 `branch` [ info 0 2 `branch` [] ]))
|
|
|
|
, Join (These (info 0 8 `branch` [ info 0 7 `branch` [ info 2 5 `leaf` "foo" ] ])
|
|
|
|
(info 2 6 `branch` [ info 2 6 `branch` [ info 2 5 `leaf` "foo" ] ]))
|
2016-03-22 07:17:20 +03:00
|
|
|
, Join (That (info 6 8 `branch` [ info 6 7 `branch` [] ]))
|
|
|
|
, Join (These (info 8 12 `branch` [ info 8 11 `leaf` "bar" ])
|
|
|
|
(info 8 12 `branch` [ info 8 11 `leaf` "bar" ]))
|
|
|
|
, Join (These (info 12 12 `branch` [])
|
|
|
|
(info 12 12 `branch` []))
|
2016-03-22 02:08:02 +03:00
|
|
|
]
|
|
|
|
|
2016-04-05 01:26:10 +03:00
|
|
|
it "aligns identical branches with multiple children on the same line" $
|
2016-05-14 01:27:38 +03:00
|
|
|
let sources = pure (Source.fromList "[ foo, bar ]") in
|
2016-05-19 19:12:11 +03:00
|
|
|
align sources (pure (info 0 12) `branch` [ pure (info 2 5) `leaf` "foo", pure (info 7 10) `leaf` "bar" ]) `shouldBe` prettyDiff sources
|
2016-04-05 18:37:40 +03:00
|
|
|
[ Join (runBothWith These (pure (info 0 12 `branch` [ info 2 5 `leaf` "foo", info 7 10 `leaf` "bar" ])) ) ]
|
2016-04-05 01:26:10 +03:00
|
|
|
|
2016-04-15 02:40:36 +03:00
|
|
|
it "aligns insertions" $
|
2016-05-14 01:27:38 +03:00
|
|
|
let sources = both (Source.fromList "a") (Source.fromList "a\nb") in
|
2016-05-19 19:12:11 +03:00
|
|
|
align sources (both (info 0 1) (info 0 3) `branch` [ pure (info 0 1) `leaf` "a", insert (info 2 3 `leaf` "b") ]) `shouldBe` prettyDiff sources
|
2016-04-15 02:40:36 +03:00
|
|
|
[ Join (These (info 0 1 `branch` [ info 0 1 `leaf` "a" ])
|
|
|
|
(info 0 2 `branch` [ info 0 1 `leaf` "a" ]))
|
2016-05-19 19:12:11 +03:00
|
|
|
, Join (That (info 2 3 `branch` [ insert (info 2 3 `leaf` "b") ]))
|
2016-04-15 02:40:36 +03:00
|
|
|
]
|
|
|
|
|
2016-05-16 17:54:56 +03:00
|
|
|
it "aligns total insertions" $
|
|
|
|
let sources = both (Source.fromList "") (Source.fromList "a") in
|
2016-05-19 19:12:11 +03:00
|
|
|
align sources (insert (info 0 1 `leaf` "a")) `shouldBe` prettyDiff sources
|
|
|
|
[ Join (That (insert (info 0 1 `leaf` "a"))) ]
|
2016-05-16 17:54:56 +03:00
|
|
|
|
2016-05-16 19:37:13 +03:00
|
|
|
it "aligns insertions into empty branches" $
|
2016-05-19 00:22:16 +03:00
|
|
|
let sources = both (Source.fromList "[ ]") (Source.fromList "[a]") in
|
2016-05-19 19:12:11 +03:00
|
|
|
align sources (pure (info 0 3) `branch` [ insert (info 1 2 `leaf` "a") ]) `shouldBe` prettyDiff sources
|
2016-05-19 00:22:16 +03:00
|
|
|
[ Join (These (info 0 3 `branch` [])
|
2016-05-19 19:12:11 +03:00
|
|
|
(info 0 3 `branch` [ insert (info 1 2 `leaf` "a") ])) ]
|
2016-05-16 19:37:13 +03:00
|
|
|
|
2016-05-19 07:13:26 +03:00
|
|
|
it "aligns symmetrically following insertions" $
|
2016-05-14 01:25:42 +03:00
|
|
|
let sources = both (Source.fromList "a\nc") (Source.fromList "a\nb\nc") in
|
2016-05-19 19:12:11 +03:00
|
|
|
align sources (both (info 0 3) (info 0 5) `branch` [ pure (info 0 1) `leaf` "a", insert (info 2 3 `leaf` "b"), both (info 2 3) (info 4 5) `leaf` "c" ])
|
|
|
|
`shouldBe` prettyDiff sources
|
2016-04-15 23:10:09 +03:00
|
|
|
[ Join (These (info 0 2 `branch` [ info 0 1 `leaf` "a" ])
|
|
|
|
(info 0 2 `branch` [ info 0 1 `leaf` "a" ]))
|
2016-05-19 19:12:11 +03:00
|
|
|
, Join (That (info 2 4 `branch` [ insert (info 2 3 `leaf` "b") ]))
|
2016-04-15 23:10:09 +03:00
|
|
|
, Join (These (info 2 3 `branch` [ info 2 3 `leaf` "c" ])
|
|
|
|
(info 4 5 `branch` [ info 4 5 `leaf` "c" ]))
|
|
|
|
]
|
|
|
|
|
2016-05-19 07:19:08 +03:00
|
|
|
it "symmetrical nodes force the alignment of asymmetrical nodes on both sides" $
|
|
|
|
let sources = both (Source.fromList "[ a, b ]") (Source.fromList "[ b, c ]") in
|
2016-05-19 19:12:11 +03:00
|
|
|
align sources (pure (info 0 8) `branch` [ delete (info 2 3 `leaf` "a"), both (info 5 6) (info 2 3) `leaf` "b", insert (info 5 6 `leaf` "c") ]) `shouldBe` prettyDiff sources
|
|
|
|
[ Join (These (info 0 8 `branch` [ delete (info 2 3 `leaf` "a"), info 5 6 `leaf` "b" ])
|
|
|
|
(info 0 8 `branch` [ info 2 3 `leaf` "b", insert (info 5 6 `leaf` "c") ])) ]
|
2016-05-19 07:19:08 +03:00
|
|
|
|
2016-05-19 16:01:15 +03:00
|
|
|
it "when one of two symmetrical nodes must be split, splits the latter" $
|
|
|
|
let sources = both (Source.fromList "[ a, b ]") (Source.fromList "[ a\n, b\n]") in
|
2016-05-19 19:12:11 +03:00
|
|
|
align sources (both (info 0 8) (info 0 9) `branch` [ pure (info 2 3) `leaf` "a", both (info 5 6) (info 6 7) `leaf` "b" ]) `shouldBe` prettyDiff sources
|
2016-05-19 16:01:15 +03:00
|
|
|
[ Join (These (info 0 8 `branch` [ info 2 3 `leaf` "a", info 5 6 `leaf` "b" ])
|
|
|
|
(info 0 4 `branch` [ info 2 3 `leaf` "a" ]))
|
|
|
|
, Join (That (info 4 8 `branch` [ info 6 7 `leaf` "b" ]))
|
|
|
|
, Join (That (info 8 9 `branch` []))
|
|
|
|
]
|
|
|
|
|
2016-05-19 19:22:48 +03:00
|
|
|
it "aligns deletions before insertions" $
|
|
|
|
let sources = both (Source.fromList "[ a ]") (Source.fromList "[ b ]") in
|
|
|
|
align sources (pure (info 0 5) `branch` [ delete (info 2 3 `leaf` "a"), insert (info 2 3 `leaf` "b") ]) `shouldBe` prettyDiff sources
|
|
|
|
[ Join (This (info 0 5 `branch` [ delete (info 2 3 `leaf` "a") ]))
|
|
|
|
, Join (That (info 0 5 `branch` [ insert (info 2 3 `leaf` "b") ]))
|
|
|
|
]
|
|
|
|
|
2016-05-19 19:52:51 +03:00
|
|
|
it "aligns context-only lines symmetrically" $
|
|
|
|
let sources = both (Source.fromList "[\n a\n,\n b\n]") (Source.fromList "[\n a, b\n\n\n]") in
|
|
|
|
align sources (both (info 0 13) (info 0 12) `branch` [ pure (info 4 5) `leaf` "a", both (info 10 11) (info 7 8) `leaf` "b" ]) `shouldBe` prettyDiff sources
|
|
|
|
[ Join (These (info 0 2 `branch` [])
|
|
|
|
(info 0 2 `branch` []))
|
|
|
|
, Join (These (info 2 6 `branch` [ info 4 5 `leaf` "a" ])
|
|
|
|
(info 2 9 `branch` [ info 4 5 `leaf` "a", info 7 8 `leaf` "b" ]))
|
|
|
|
, Join (These (info 6 8 `branch` [])
|
|
|
|
(info 9 10 `branch` []))
|
|
|
|
, Join (These (info 8 12 `branch` [ info 10 11 `leaf` "b" ])
|
|
|
|
(info 10 11 `branch` []))
|
|
|
|
, Join (These (info 12 13 `branch` [])
|
|
|
|
(info 11 12 `branch` []))
|
|
|
|
]
|
|
|
|
|
2016-05-19 23:16:35 +03:00
|
|
|
it "aligns asymmetrical nodes preceding their symmetrical siblings conservatively" $
|
|
|
|
let sources = both (Source.fromList "[ b, c ]") (Source.fromList "[ a\n, c\n]") in
|
|
|
|
align sources (both (info 0 8) (info 0 9) `branch` [ insert (info 2 3 `leaf` "a"), delete (info 2 3 `leaf` "b"), both (info 5 6) (info 6 7) `leaf` "c" ]) `shouldBe` prettyDiff sources
|
|
|
|
[ Join (That (info 0 4 `branch` [ insert (info 2 3 `leaf` "a") ]))
|
|
|
|
, Join (These (info 0 8 `branch` [ delete (info 2 3 `leaf` "b"), info 5 6 `leaf` "c" ])
|
|
|
|
(info 4 8 `branch` [ info 6 7 `leaf` "c" ]))
|
|
|
|
, Join (That (info 8 9 `branch` []))
|
|
|
|
]
|
|
|
|
|
2016-04-26 22:44:11 +03:00
|
|
|
describe "numberedRows" $
|
|
|
|
prop "counts only non-empty values" $
|
|
|
|
\ xs -> counts (numberedRows (xs :: [Join These Char])) `shouldBe` length . catMaybes <$> Join (unalign (runJoin <$> xs))
|
|
|
|
|
2016-05-20 22:34:11 +03:00
|
|
|
data Child = Child
|
2016-05-20 22:40:40 +03:00
|
|
|
{ childKey :: String
|
2016-05-20 22:50:15 +03:00
|
|
|
, childContents :: String
|
2016-05-20 22:44:24 +03:00
|
|
|
, childMargin :: String
|
2016-05-20 22:34:11 +03:00
|
|
|
}
|
|
|
|
|
2016-05-21 04:50:52 +03:00
|
|
|
data BranchElement
|
|
|
|
= Child' String (Join These String {- newlines or asterisks -})
|
|
|
|
| Margin (Join These String {- newlines or hyphens -})
|
|
|
|
|
2016-05-21 05:11:22 +03:00
|
|
|
toSource :: [BranchElement] -> Source.Source Char
|
2016-05-21 04:49:29 +03:00
|
|
|
toSource = Source.fromList . concatMap show
|
|
|
|
|
2016-05-21 05:11:22 +03:00
|
|
|
toSources :: [Join These BranchElement] -> Both (Source.Source Char)
|
2016-05-21 04:50:41 +03:00
|
|
|
toSources = fmap toSource . toChildLists
|
|
|
|
|
2016-05-21 05:11:22 +03:00
|
|
|
toChildLists :: [Join These BranchElement] -> Both [BranchElement]
|
2016-05-21 04:50:08 +03:00
|
|
|
toChildLists = foldMap (modifyJoin (fromThese [] []) . fmap (:[]))
|
|
|
|
|
2016-05-20 22:34:20 +03:00
|
|
|
instance Arbitrary Child where
|
2016-05-20 22:50:31 +03:00
|
|
|
arbitrary = Child <$> key <*> contents <*> margin
|
2016-05-20 22:41:20 +03:00
|
|
|
where key = listOf1 (elements (['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9']))
|
2016-05-20 23:06:49 +03:00
|
|
|
contents = listOf (padding '*')
|
|
|
|
margin = listOf (padding '-')
|
|
|
|
padding char = frequency [ (10, pure char)
|
|
|
|
, (1, pure '\n') ]
|
2016-05-20 22:34:20 +03:00
|
|
|
|
2016-05-20 23:40:25 +03:00
|
|
|
shrink Child {..} | null childContents, null childMargin = []
|
|
|
|
| otherwise = Child childKey <$> "" : shrinkList (const []) childContents <*> "" : shrinkList (const []) childMargin
|
2016-05-20 23:29:09 +03:00
|
|
|
|
2016-05-21 04:59:46 +03:00
|
|
|
instance Arbitrary BranchElement where
|
|
|
|
arbitrary = oneof [ Child' <$> key <*> joinTheseOf contents
|
|
|
|
, Margin <$> joinTheseOf margin ]
|
|
|
|
where key = listOf1 (elements (['a'..'z'] ++ ['A'..'Z'] ++ ['0'..'9']))
|
|
|
|
contents = listOf (padding '*')
|
|
|
|
margin = listOf (padding '-')
|
|
|
|
padding char = frequency [ (10, pure char)
|
|
|
|
, (1, pure '\n') ]
|
|
|
|
joinTheseOf g = oneof [ Join . This <$> g
|
|
|
|
, Join . That <$> g
|
|
|
|
, (Join .) . These <$> g <*> g ]
|
|
|
|
|
2016-05-21 05:08:22 +03:00
|
|
|
shrink (Child' key contents) = Child' key <$> traverse (shrinkList (const [])) contents
|
|
|
|
shrink (Margin contents) = Margin <$> traverse (shrinkList (const [])) contents
|
|
|
|
|
2016-05-20 23:07:13 +03:00
|
|
|
instance Show Child where
|
2016-05-20 23:13:15 +03:00
|
|
|
show Child {..} = childMargin ++ "(" ++ childKey ++ childContents ++ ")"
|
2016-05-20 23:07:13 +03:00
|
|
|
|
2016-05-21 04:54:33 +03:00
|
|
|
instance Show BranchElement where
|
2016-05-21 05:02:29 +03:00
|
|
|
show (Child' key contents) = showThese (showContents <$> contents)
|
2016-05-21 04:54:33 +03:00
|
|
|
where showContents contents = "(" ++ key ++ contents ++ ")"
|
2016-05-21 05:03:39 +03:00
|
|
|
show (Margin contents) = showThese contents
|
2016-05-21 04:54:33 +03:00
|
|
|
|
2016-05-21 05:03:13 +03:00
|
|
|
showThese :: Join These String -> String
|
2016-05-21 05:04:09 +03:00
|
|
|
showThese = these ('<':) ('>':) (\ a b -> a ++ " | " ++ b) . runJoin
|
2016-05-21 05:03:13 +03:00
|
|
|
|
2016-04-26 21:54:24 +03:00
|
|
|
counts :: [Join These (Int, a)] -> Both Int
|
|
|
|
counts numbered = fromMaybe 0 . getLast . mconcat . fmap Last <$> Join (unalign (runJoin . fmap Prelude.fst <$> numbered))
|
|
|
|
|
2016-05-20 19:39:56 +03:00
|
|
|
align :: Both (Source.Source Char) -> ConstructibleFree (Patch (Term String Info)) (Both Info) -> PrettyDiff (SplitDiff String Info)
|
|
|
|
align sources = PrettyDiff sources . fmap (fmap (getRange &&& id)) . alignDiff sources . deconstruct
|
2016-03-22 01:54:27 +03:00
|
|
|
|
2016-04-01 17:15:48 +03:00
|
|
|
info :: Int -> Int -> Info
|
2016-04-14 19:57:45 +03:00
|
|
|
info = ((\ r -> Info r mempty 0) .) . Range
|
2016-04-25 19:37:56 +03:00
|
|
|
|
2016-05-20 19:39:56 +03:00
|
|
|
prettyDiff :: Both (Source.Source Char) -> [Join These (ConstructibleFree (SplitPatch (Term String Info)) Info)] -> PrettyDiff (SplitDiff String Info)
|
|
|
|
prettyDiff sources = PrettyDiff sources . fmap (fmap ((getRange &&& id) . deconstruct))
|
2016-05-19 19:12:11 +03:00
|
|
|
|
2016-05-20 19:39:56 +03:00
|
|
|
data PrettyDiff a = PrettyDiff { unPrettySources :: Both (Source.Source Char), unPrettyLines :: [Join These (Range, a)] }
|
2016-04-25 21:59:20 +03:00
|
|
|
deriving Eq
|
2016-04-25 20:00:26 +03:00
|
|
|
|
2016-05-20 19:39:56 +03:00
|
|
|
instance Show a => Show (PrettyDiff a) where
|
2016-05-19 19:22:33 +03:00
|
|
|
show (PrettyDiff sources lines) = prettyPrinted -- ++ "\n" ++ show lines
|
|
|
|
where prettyPrinted = showLine (maximum (0 : (maximum . fmap length <$> shownLines))) <$> shownLines >>= ('\n':)
|
|
|
|
shownLines = catMaybes $ toBoth <$> lines
|
2016-04-25 22:08:20 +03:00
|
|
|
showLine n line = uncurry ((++) . (++ " | ")) (fromThese (replicate n ' ') (replicate n ' ') (runJoin (pad n <$> line)))
|
2016-05-20 19:39:56 +03:00
|
|
|
showDiff (range, _) = filter (/= '\n') . toList . Source.slice range
|
2016-04-25 22:03:49 +03:00
|
|
|
pad n string = showString (take n string) (replicate (max 0 (n - length string)) ' ')
|
2016-04-25 21:43:07 +03:00
|
|
|
toBoth them = showDiff <$> them `applyThese` modifyJoin (uncurry These) sources
|
2016-05-19 18:03:45 +03:00
|
|
|
|
2016-05-19 19:12:11 +03:00
|
|
|
newtype ConstructibleFree patch annotation = ConstructibleFree { deconstruct :: Free (Annotated String annotation) patch }
|
|
|
|
|
|
|
|
|
2016-05-19 18:03:45 +03:00
|
|
|
class PatchConstructible p where
|
2016-05-19 19:12:11 +03:00
|
|
|
insert :: Term String Info -> p
|
|
|
|
delete :: Term String Info -> p
|
2016-05-19 18:04:19 +03:00
|
|
|
|
2016-05-19 19:12:11 +03:00
|
|
|
instance PatchConstructible (Patch (Term String Info)) where
|
2016-05-19 18:04:19 +03:00
|
|
|
insert = Insert
|
|
|
|
delete = Delete
|
|
|
|
|
2016-05-19 19:12:11 +03:00
|
|
|
instance PatchConstructible (SplitPatch (Term String Info)) where
|
2016-05-19 18:04:19 +03:00
|
|
|
insert = SplitInsert
|
|
|
|
delete = SplitDelete
|
2016-05-19 19:12:11 +03:00
|
|
|
|
|
|
|
instance PatchConstructible patch => PatchConstructible (ConstructibleFree patch annotation) where
|
|
|
|
insert = ConstructibleFree . Pure . insert
|
|
|
|
delete = ConstructibleFree . Pure . delete
|
|
|
|
|
|
|
|
|
|
|
|
class SyntaxConstructible s where
|
|
|
|
leaf :: annotation -> String -> s annotation
|
|
|
|
branch :: annotation -> [s annotation] -> s annotation
|
|
|
|
|
|
|
|
instance SyntaxConstructible (ConstructibleFree patch) where
|
|
|
|
leaf info = ConstructibleFree . Free . Annotated info . Leaf
|
|
|
|
branch info = ConstructibleFree . Free . Annotated info . Indexed . fmap deconstruct
|
|
|
|
|
|
|
|
instance SyntaxConstructible (Cofree (Syntax String)) where
|
|
|
|
info `leaf` value = info :< Leaf value
|
|
|
|
info `branch` children = info :< Indexed children
|