2016-07-15 16:39:03 +03:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
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
|
2017-07-28 21:37:02 +03:00
|
|
|
import Control.Arrow ((&&&))
|
2017-09-09 16:58:07 +03:00
|
|
|
import Control.Monad.Free (wrap)
|
2016-05-24 19:00:03 +03:00
|
|
|
import Control.Monad.State
|
2016-05-14 01:25:42 +03:00
|
|
|
import Data.Align hiding (align)
|
2016-05-25 16:54:21 +03:00
|
|
|
import Data.Bifunctor
|
2016-03-22 02:08:02 +03:00
|
|
|
import Data.Bifunctor.Join
|
2017-09-27 19:41:41 +03:00
|
|
|
import Data.Diff
|
2017-07-28 21:37:02 +03:00
|
|
|
import Data.Functor.Both as Both hiding (fst, snd)
|
2017-01-09 22:13:41 +03:00
|
|
|
import Data.Functor.Listable
|
2017-07-28 21:37:02 +03:00
|
|
|
import Data.List (nub, sort)
|
|
|
|
import Data.Maybe (catMaybes, fromMaybe)
|
2016-09-16 00:21:37 +03:00
|
|
|
import Data.Monoid hiding ((<>))
|
2017-06-24 16:46:28 +03:00
|
|
|
import Data.Range
|
2016-06-17 20:33:50 +03:00
|
|
|
import Data.Record
|
2017-07-28 21:37:02 +03:00
|
|
|
import Data.Semigroup ((<>))
|
2017-06-24 16:59:41 +03:00
|
|
|
import qualified Data.Source as Source
|
2017-09-27 19:43:11 +03:00
|
|
|
import Data.SplitDiff
|
2017-09-27 19:37:37 +03:00
|
|
|
import Data.Term
|
2017-02-10 00:45:04 +03:00
|
|
|
import qualified Data.Text as Text
|
2016-04-15 05:00:00 +03:00
|
|
|
import Data.These
|
2016-09-15 00:45:23 +03:00
|
|
|
import Test.Hspec (Spec, describe, it, parallel)
|
|
|
|
import Test.Hspec.Expectations.Pretty
|
2017-01-09 22:13:41 +03:00
|
|
|
import Test.Hspec.LeanCheck
|
|
|
|
import Test.LeanCheck
|
2016-07-13 21:32:53 +03:00
|
|
|
import GHC.Show (Show(..))
|
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" $
|
2017-10-10 20:18:11 +03:00
|
|
|
alignBranch getRange ([] :: [Join These (SplitDiff [] (Record '[Range]))]) (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" $
|
2017-10-10 20:18:11 +03:00
|
|
|
alignBranch getRange ([] :: [Join These (SplitDiff [] (Record '[Range]))]) (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" $
|
2016-05-25 21:13:27 +03:00
|
|
|
\ elements -> let (_, children, ranges) = toAlignBranchInputs elements in
|
2017-07-28 21:37:02 +03:00
|
|
|
join <$> traverse (modifyJoin (fromThese [] []) . fmap (pure . fst)) (alignBranch snd children ranges) `shouldBe` ranges
|
2016-05-20 20:05:38 +03:00
|
|
|
|
|
|
|
prop "covers every input child" $
|
2016-05-25 21:14:38 +03:00
|
|
|
\ elements -> let (_, children, ranges) = toAlignBranchInputs elements in
|
2017-07-28 21:37:02 +03:00
|
|
|
sort (nub (keysOfAlignedChildren (alignBranch snd children ranges))) `shouldBe` sort (nub (catMaybes (branchElementKey <$> elements)))
|
2016-05-20 20:05:38 +03:00
|
|
|
|
|
|
|
prop "covers every line of every input child" $
|
2016-05-26 18:39:24 +03:00
|
|
|
\ elements -> let (_, children, ranges) = toAlignBranchInputs elements in
|
2017-07-28 21:37:02 +03:00
|
|
|
sort (keysOfAlignedChildren (alignBranch snd children ranges)) `shouldBe` sort (do
|
2016-05-27 20:58:50 +03:00
|
|
|
line <- children
|
2017-07-28 21:37:02 +03:00
|
|
|
these (pure . fst) (pure . fst) (\ (k1, _) (k2, _) -> [ k1, k2 ]) . runJoin $ line)
|
2016-05-20 20:05:38 +03:00
|
|
|
|
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" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "[ foo ]") (Source.fromText "[ foo ]") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 7, info 0 7) `merge` [ (info 2 5, info 2 5) `merge` Leaf "foo" ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (These (wrap $ info 0 7 `In` [ wrap $ info 2 5 `In` [] ])
|
|
|
|
(wrap $ info 0 7 `In` [ wrap $ info 2 5 `In` [] ])) ]
|
2016-03-24 20:05:44 +03:00
|
|
|
|
2016-03-24 20:44:57 +03:00
|
|
|
it "aligns identical branches spanning multiple lines" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "[\nfoo\n]") (Source.fromText "[\nfoo\n]") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 7, info 0 7) `merge` [ (info 2 5, info 2 5) `merge` Leaf "foo" ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (These (wrap $ info 0 2 `In` [])
|
|
|
|
(wrap $ info 0 2 `In` []))
|
|
|
|
, Join (These (wrap $ info 2 6 `In` [ wrap $ info 2 5 `In` [] ])
|
|
|
|
(wrap $ info 2 6 `In` [ wrap $ info 2 5 `In` [] ]))
|
|
|
|
, Join (These (wrap $ info 6 7 `In` [])
|
|
|
|
(wrap $ info 6 7 `In` []))
|
2016-03-24 20:05:52 +03:00
|
|
|
]
|
|
|
|
|
2016-03-24 20:44:57 +03:00
|
|
|
it "aligns reformatted branches" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "[ foo ]") (Source.fromText "[\nfoo\n]") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 7, info 0 7) `merge` [ (info 2 5, info 2 5) `merge` Leaf "foo" ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (That (wrap $ info 0 2 `In` []))
|
|
|
|
, Join (These (wrap $ info 0 7 `In` [ wrap $ info 2 5 `In` [] ])
|
|
|
|
(wrap $ info 2 6 `In` [ wrap $ info 2 5 `In` [] ]))
|
|
|
|
, Join (That (wrap $ info 6 7 `In` []))
|
2016-03-22 02:08:02 +03:00
|
|
|
]
|
|
|
|
|
2016-03-24 20:44:57 +03:00
|
|
|
it "aligns nodes following reformatted branches" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "[ foo ]\nbar\n") (Source.fromText "[\nfoo\n]\nbar\n") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 12, info 0 12) `merge` [ (info 0 7, info 0 7) `merge` [ (info 2 5, info 2 5) `merge` Leaf "foo" ], (info 8 11, info 8 11) `merge` Leaf "bar" ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (That (wrap $ info 0 2 `In` [ wrap $ info 0 2 `In` [] ]))
|
|
|
|
, Join (These (wrap $ info 0 8 `In` [ wrap $ info 0 7 `In` [ wrap $ info 2 5 `In` [] ] ])
|
|
|
|
(wrap $ info 2 6 `In` [ wrap $ info 2 6 `In` [ wrap $ info 2 5 `In` [] ] ]))
|
|
|
|
, Join (That (wrap $ info 6 8 `In` [ wrap $ info 6 7 `In` [] ]))
|
|
|
|
, Join (These (wrap $ info 8 12 `In` [ wrap $ info 8 11 `In` [] ])
|
|
|
|
(wrap $ info 8 12 `In` [ wrap $ info 8 11 `In` [] ]))
|
|
|
|
, Join (These (wrap $ info 12 12 `In` [])
|
|
|
|
(wrap $ info 12 12 `In` []))
|
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" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = pure (Source.fromText "[ foo, bar ]") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 12, info 0 12) `merge` [ (info 2 5, info 2 5) `merge` Leaf "foo", (info 7 10, info 7 10) `merge` Leaf "bar" ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (runBothWith These (pure (wrap $ info 0 12 `In` [ wrap $ info 2 5 `In` [], wrap $ info 7 10 `In` [] ])) ) ]
|
2016-04-05 01:26:10 +03:00
|
|
|
|
2016-04-15 02:40:36 +03:00
|
|
|
it "aligns insertions" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "a") (Source.fromText "a\nb") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 1, info 0 3) `merge` [ (info 0 1, info 0 1) `merge` Leaf "a", inserting (Term (info 2 3 `In` Leaf "b")) ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (These (wrap $ info 0 1 `In` [ wrap $ info 0 1 `In` [] ])
|
|
|
|
(wrap $ info 0 2 `In` [ wrap $ info 0 1 `In` [] ]))
|
|
|
|
, Join (That (wrap $ info 2 3 `In` [ pure (SplitInsert (Term (info 2 3 `In` []))) ]))
|
2016-04-15 02:40:36 +03:00
|
|
|
]
|
|
|
|
|
2016-05-16 17:54:56 +03:00
|
|
|
it "aligns total insertions" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "") (Source.fromText "a") in
|
2017-09-11 22:48:58 +03:00
|
|
|
align sources (inserting (Term (info 0 1 `In` Leaf "a"))) `shouldBe` prettyDiff sources
|
|
|
|
[ Join (That (pure (SplitInsert (Term (info 0 1 `In` []))))) ]
|
2016-05-16 17:54:56 +03:00
|
|
|
|
2016-05-16 19:37:13 +03:00
|
|
|
it "aligns insertions into empty branches" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "[ ]") (Source.fromText "[a]") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 3, info 0 3) `merge` [ inserting (Term (info 1 2 `In` Leaf "a")) ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (That (wrap $ info 0 3 `In` [ pure (SplitInsert (Term (info 1 2 `In` []))) ]))
|
|
|
|
, Join (This (wrap $ info 0 3 `In` []))
|
2016-05-27 22:30:39 +03:00
|
|
|
]
|
2016-05-16 19:37:13 +03:00
|
|
|
|
2016-05-19 07:13:26 +03:00
|
|
|
it "aligns symmetrically following insertions" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "a\nc") (Source.fromText "a\nb\nc") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 3, info 0 5) `merge` [ (info 0 1, info 0 1) `merge` Leaf "a", inserting (Term (info 2 3 `In` Leaf "b")), (info 2 3, info 4 5) `merge` Leaf "c" ])
|
2016-05-19 19:12:11 +03:00
|
|
|
`shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (These (wrap $ info 0 2 `In` [ wrap $ info 0 1 `In` [] ])
|
|
|
|
(wrap $ info 0 2 `In` [ wrap $ info 0 1 `In` [] ]))
|
|
|
|
, Join (That (wrap $ info 2 4 `In` [ pure (SplitInsert (Term (info 2 3 `In` []))) ]))
|
|
|
|
, Join (These (wrap $ info 2 3 `In` [ wrap $ info 2 3 `In` [] ])
|
|
|
|
(wrap $ info 4 5 `In` [ wrap $ info 4 5 `In` [] ]))
|
2016-04-15 23:10:09 +03:00
|
|
|
]
|
|
|
|
|
2016-05-19 07:19:08 +03:00
|
|
|
it "symmetrical nodes force the alignment of asymmetrical nodes on both sides" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "[ a, b ]") (Source.fromText "[ b, c ]") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 8, info 0 8) `merge` [ deleting (Term (info 2 3 `In` Leaf "a")), (info 5 6, info 2 3) `merge` Leaf "b", inserting (Term (info 5 6 `In` Leaf "c")) ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (These (wrap $ info 0 8 `In` [ pure (SplitDelete (Term (info 2 3 `In` []))), wrap $ info 5 6 `In` [] ])
|
|
|
|
(wrap $ info 0 8 `In` [ wrap $ info 2 3 `In` [], pure (SplitInsert (Term (info 5 6 `In` []))) ])) ]
|
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" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "[ a, b ]") (Source.fromText "[ a\n, b\n]") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 8, info 0 9) `merge` [ (info 2 3, info 2 3) `merge` Leaf "a", (info 5 6, info 6 7) `merge` Leaf "b" ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (These (wrap $ info 0 8 `In` [ wrap $ info 2 3 `In` [], wrap $ info 5 6 `In` [] ])
|
|
|
|
(wrap $ info 0 4 `In` [ wrap $ info 2 3 `In` [] ]))
|
|
|
|
, Join (That (wrap $ info 4 8 `In` [ wrap $ info 6 7 `In` [] ]))
|
|
|
|
, Join (That (wrap $ info 8 9 `In` []))
|
2016-05-19 16:01:15 +03:00
|
|
|
]
|
|
|
|
|
2016-05-19 19:22:48 +03:00
|
|
|
it "aligns deletions before insertions" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "[ a ]") (Source.fromText "[ b ]") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 5, info 0 5) `merge` [ deleting (Term (info 2 3 `In` Leaf "a")), inserting (Term (info 2 3 `In` Leaf "b")) ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (This (wrap $ info 0 5 `In` [ pure (SplitDelete (Term (info 2 3 `In` []))) ]))
|
|
|
|
, Join (That (wrap $ info 0 5 `In` [ pure (SplitInsert (Term (info 2 3 `In` []))) ]))
|
2016-05-19 19:22:48 +03:00
|
|
|
]
|
|
|
|
|
2016-05-19 19:52:51 +03:00
|
|
|
it "aligns context-only lines symmetrically" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "[\n a\n,\n b\n]") (Source.fromText "[\n a, b\n\n\n]") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 13, info 0 12) `merge` [ (info 4 5, info 4 5) `merge` Leaf "a", (info 10 11, info 7 8) `merge` Leaf "b" ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (These (wrap $ info 0 2 `In` [])
|
|
|
|
(wrap $ info 0 2 `In` []))
|
|
|
|
, Join (These (wrap $ info 2 6 `In` [ wrap $ info 4 5 `In` [] ])
|
|
|
|
(wrap $ info 2 9 `In` [ wrap $ info 4 5 `In` [], wrap $ info 7 8 `In` [] ]))
|
|
|
|
, Join (These (wrap $ info 6 8 `In` [])
|
|
|
|
(wrap $ info 9 10 `In` []))
|
|
|
|
, Join (This (wrap $ info 8 12 `In` [ wrap $ info 10 11 `In` [] ]))
|
|
|
|
, Join (These (wrap $ info 12 13 `In` [])
|
|
|
|
(wrap $ info 10 11 `In` []))
|
|
|
|
, Join (That (wrap $ info 11 12 `In` []))
|
2016-05-19 19:52:51 +03:00
|
|
|
]
|
|
|
|
|
2016-05-19 23:16:35 +03:00
|
|
|
it "aligns asymmetrical nodes preceding their symmetrical siblings conservatively" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "[ b, c ]") (Source.fromText "[ a\n, c\n]") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 8, info 0 9) `merge` [ inserting (Term (info 2 3 `In` Leaf "a")), deleting (Term (info 2 3 `In` Leaf "b")), (info 5 6, info 6 7) `merge` Leaf "c" ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (That (wrap $ info 0 4 `In` [ pure (SplitInsert (Term (info 2 3 `In` []))) ]))
|
|
|
|
, Join (These (wrap $ info 0 8 `In` [ pure (SplitDelete (Term (info 2 3 `In` []))), wrap $ info 5 6 `In` [] ])
|
|
|
|
(wrap $ info 4 8 `In` [ wrap $ info 6 7 `In` [] ]))
|
|
|
|
, Join (That (wrap $ info 8 9 `In` []))
|
2016-05-19 23:16:35 +03:00
|
|
|
]
|
|
|
|
|
2016-05-26 16:31:25 +03:00
|
|
|
it "aligns symmetrical reformatted nodes" $
|
2017-02-10 00:45:04 +03:00
|
|
|
let sources = both (Source.fromText "a [ b ]\nc") (Source.fromText "a [\nb\n]\nc") in
|
2017-10-10 20:18:11 +03:00
|
|
|
align sources ((info 0 9, info 0 9) `merge` [ (info 0 1, info 0 1) `merge` Leaf "a", (info 2 7, info 2 7) `merge` [ (info 4 5, info 4 5) `merge` Leaf "b" ], (info 8 9, info 8 9) `merge` Leaf "c" ]) `shouldBe` prettyDiff sources
|
2017-09-11 22:48:58 +03:00
|
|
|
[ Join (These (wrap $ info 0 8 `In` [ wrap $ info 0 1 `In` [], wrap $ info 2 7 `In` [ wrap $ info 4 5 `In` [] ] ])
|
|
|
|
(wrap $ info 0 4 `In` [ wrap $ info 0 1 `In` [], wrap $ info 2 4 `In` [] ]))
|
|
|
|
, Join (That (wrap $ info 4 6 `In` [ wrap $ info 4 6 `In` [ wrap $ info 4 5 `In` [] ] ]))
|
|
|
|
, Join (That (wrap $ info 6 8 `In` [ wrap $ info 6 7 `In` [] ]))
|
|
|
|
, Join (These (wrap $ info 8 9 `In` [ wrap $ info 8 9 `In` [] ])
|
|
|
|
(wrap $ info 8 9 `In` [ wrap $ info 8 9 `In` [] ]))
|
2016-05-26 16:31:25 +03:00
|
|
|
]
|
|
|
|
|
2016-06-27 22:02:28 +03:00
|
|
|
describe "numberedRows" $ do
|
2016-04-26 22:44:11 +03:00
|
|
|
prop "counts only non-empty values" $
|
2017-01-09 22:13:41 +03:00
|
|
|
\ xs -> counts (numberedRows (unListableF <$> xs :: [Join These Char])) `shouldBe` length . catMaybes <$> Join (unalign (runJoin . unListableF <$> xs))
|
2016-04-26 22:44:11 +03:00
|
|
|
|
2016-05-21 04:50:52 +03:00
|
|
|
data BranchElement
|
2017-07-28 21:37:02 +03:00
|
|
|
= Child Text.Text (Join These Text.Text)
|
|
|
|
| Margin (Join These Text.Text)
|
2016-05-25 17:26:44 +03:00
|
|
|
deriving Show
|
2016-05-21 04:50:52 +03:00
|
|
|
|
2017-07-28 21:37:02 +03:00
|
|
|
branchElementKey :: BranchElement -> Maybe Text.Text
|
2016-05-24 20:36:04 +03:00
|
|
|
branchElementKey (Child key _) = Just key
|
|
|
|
branchElementKey _ = Nothing
|
|
|
|
|
2017-07-28 21:37:02 +03:00
|
|
|
toAlignBranchInputs :: [BranchElement] -> (Both Source.Source, [Join These (Text.Text, Range)], Both [Range])
|
2016-05-26 22:25:45 +03:00
|
|
|
toAlignBranchInputs elements = (sources, join . (`evalState` both 0 0) . traverse go $ elements, ranges)
|
2017-07-28 21:37:02 +03:00
|
|
|
where go :: BranchElement -> State (Both Int) [Join These (Text.Text, Range)]
|
2016-05-25 16:54:21 +03:00
|
|
|
go child@(Child key _) = do
|
2016-05-26 22:25:45 +03:00
|
|
|
lines <- traverse (\ (Child _ contents) -> do
|
2016-05-26 00:10:30 +03:00
|
|
|
prev <- get
|
2017-02-13 20:17:46 +03:00
|
|
|
let next = (+) <$> prev <*> modifyJoin (fromThese 0 0) (Text.length <$> contents)
|
2016-05-25 16:54:21 +03:00
|
|
|
put next
|
2016-05-26 22:25:45 +03:00
|
|
|
pure $! modifyJoin (runBothWith bimap (const <$> (Range <$> prev <*> next))) contents) (alignBranchElement child)
|
2016-05-27 20:58:50 +03:00
|
|
|
pure $! fmap ((,) key) <$> lines
|
2016-05-21 07:08:01 +03:00
|
|
|
go (Margin contents) = do
|
|
|
|
prev <- get
|
2017-02-13 20:17:46 +03:00
|
|
|
put $ (+) <$> prev <*> modifyJoin (fromThese 0 0) (Text.length <$> contents)
|
2016-05-26 22:25:45 +03:00
|
|
|
pure []
|
2016-05-26 16:36:44 +03:00
|
|
|
alignBranchElement element = case element of
|
2016-07-14 17:39:31 +03:00
|
|
|
Child key contents -> Child key <$> joinCrosswalk lines contents
|
|
|
|
Margin contents -> Margin <$> joinCrosswalk lines contents
|
2017-06-24 18:13:22 +03:00
|
|
|
where lines = fmap Source.toText . Source.sourceLines . Source.fromText
|
2017-02-10 00:45:04 +03:00
|
|
|
sources = foldMap Source.fromText <$> bothContents elements
|
2017-06-24 18:13:22 +03:00
|
|
|
ranges = fmap (filter (\ (Range start end) -> start /= end)) $ Source.sourceLineRangesWithin <$> (Source.totalRange <$> sources) <*> sources
|
2016-05-25 21:13:27 +03:00
|
|
|
bothContents = foldMap (modifyJoin (fromThese [] []) . fmap (:[]) . branchElementContents)
|
2016-05-26 16:37:06 +03:00
|
|
|
branchElementContents (Child _ contents) = contents
|
|
|
|
branchElementContents (Margin contents) = contents
|
2016-05-21 06:51:24 +03:00
|
|
|
|
2017-07-28 21:37:02 +03:00
|
|
|
keysOfAlignedChildren :: [Join These (Range, [(Text.Text, Range)])] -> [Text.Text]
|
|
|
|
keysOfAlignedChildren lines = lines >>= these id id (<>) . runJoin . fmap (fmap fst . snd)
|
2016-05-24 20:36:12 +03:00
|
|
|
|
2016-07-14 17:39:21 +03:00
|
|
|
joinCrosswalk :: Bicrosswalk p => Align f => (a -> f b) -> Join p a -> f (Join p b)
|
|
|
|
joinCrosswalk f = fmap Join . bicrosswalk f f . runJoin
|
|
|
|
|
2017-01-09 22:13:41 +03:00
|
|
|
instance Listable BranchElement where
|
|
|
|
tiers = oneof [ (\ key -> Child key `mapT` joinTheseOf (contents key)) `concatMapT` key
|
2017-02-13 20:17:46 +03:00
|
|
|
, Margin `mapT` joinTheseOf (Text.singleton `mapT` padding '-') ]
|
|
|
|
where key = Text.singleton `mapT` [['a'..'z'] <> ['A'..'Z'] <> ['0'..'9']]
|
|
|
|
contents key = (wrap key . Text.singleton) `mapT` padding '*'
|
2017-07-28 21:37:02 +03:00
|
|
|
wrap key contents = "(" <> key <> contents <> ")" :: Text.Text
|
2017-01-09 22:13:41 +03:00
|
|
|
padding :: Char -> [Tier Char]
|
|
|
|
padding char = frequency [ (10, [[char]])
|
|
|
|
, (1, [['\n']]) ]
|
|
|
|
joinTheseOf g = oneof [ (Join . This) `mapT` g
|
|
|
|
, (Join . That) `mapT` g
|
|
|
|
, productWith ((Join .) . These) g g ]
|
|
|
|
frequency :: [(Int, [Tier a])] -> [Tier a]
|
|
|
|
frequency = concatT . foldr ((\/) . pure . uncurry replicate) []
|
|
|
|
oneof :: [[[a]]] -> [[a]]
|
|
|
|
oneof = frequency . fmap ((,) 1)
|
|
|
|
|
2016-05-21 05:08:22 +03:00
|
|
|
|
2016-04-26 21:54:24 +03:00
|
|
|
counts :: [Join These (Int, a)] -> Both Int
|
2017-07-28 21:37:02 +03:00
|
|
|
counts numbered = fromMaybe 0 . getLast . mconcat . fmap Last <$> Join (unalign (runJoin . fmap fst <$> numbered))
|
2016-04-26 21:54:24 +03:00
|
|
|
|
2017-09-14 16:41:52 +03:00
|
|
|
align :: Both Source.Source -> Diff Syntax (Record '[Range]) (Record '[Range]) -> PrettyDiff (SplitDiff [] (Record '[Range]))
|
2017-09-09 16:18:08 +03:00
|
|
|
align sources = PrettyDiff sources . fmap (fmap (getRange &&& id)) . alignDiff sources
|
2016-03-22 01:54:27 +03:00
|
|
|
|
2016-07-15 16:39:03 +03:00
|
|
|
info :: Int -> Int -> Record '[Range]
|
2017-01-19 23:46:28 +03:00
|
|
|
info start end = Range start end :. Nil
|
2016-04-25 19:37:56 +03:00
|
|
|
|
2017-09-09 16:18:08 +03:00
|
|
|
prettyDiff :: Both Source.Source -> [Join These (SplitDiff [] (Record '[Range]))] -> PrettyDiff (SplitDiff [] (Record '[Range]))
|
|
|
|
prettyDiff sources = PrettyDiff sources . fmap (fmap ((getRange &&& id)))
|
2016-05-19 19:12:11 +03:00
|
|
|
|
2017-02-13 20:17:46 +03:00
|
|
|
data PrettyDiff a = PrettyDiff { unPrettySources :: Both Source.Source, unPrettyLines :: [Join These (Range, a)] }
|
2016-04-25 21:59:20 +03:00
|
|
|
deriving Eq
|
2016-04-25 20:00:26 +03:00
|
|
|
|
2016-09-16 01:38:18 +03:00
|
|
|
instance Show (PrettyDiff a) where
|
2016-05-27 22:30:27 +03:00
|
|
|
showsPrec _ (PrettyDiff sources lines) = (prettyPrinted ++) -- . (("\n" ++ show lines) ++)
|
2016-05-19 19:22:33 +03:00
|
|
|
where prettyPrinted = showLine (maximum (0 : (maximum . fmap length <$> shownLines))) <$> shownLines >>= ('\n':)
|
|
|
|
shownLines = catMaybes $ toBoth <$> lines
|
2016-09-09 21:47:24 +03:00
|
|
|
showLine n line = uncurry ((<>) . (++ " | ")) (fromThese (replicate n ' ') (replicate n ' ') (runJoin (pad n <$> line)))
|
2017-02-14 23:42:14 +03:00
|
|
|
showDiff (range, _) = filter (/= '\n') . Text.unpack . Source.toText . Source.slice range
|
2016-09-09 21:47:24 +03:00
|
|
|
pad n string = (<>) (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
|