Diff

Copyright | (c) David Fox (2015)
License | BSD 3 Clause
Maintainer | s.clover@gmail.com
Stability | experimental
Portability | portable
Safe Haskell | Safe-Inferred
Language | Haskell2010

Data.Algorithm.DiffContext

Description

Generates a grouped diff with merged runs, and outputs them in the manner of diff -u

Synopsis

- type ContextDiff c = [Hunk c]
- type Hunk c = [Diff [c]]
- getContextDiff :: Eq a => Maybe Int -> [a] -> [a] -> ContextDiff (Numbered a)
- prettyContextDiff :: Doc -> Doc -> (Numbered c -> Doc) -> ContextDiff (Numbered c) -> Doc
- prettyContextDiffOld :: Doc -> Doc -> (c -> Doc) -> ContextDiff c -> Doc
- getContextDiffNumbered :: Eq a => Maybe Int -> [Numbered a] -> [Numbered a] -> ContextDiff (Numbered a)
- data Numbered a = Numbered Int a
- numbered :: [a] -> [Numbered a]
- unnumber :: Numbered a -> a
- unNumberContextDiff :: ContextDiff (Numbered a) -> ContextDiff a
- groupBy' :: (a -> a -> Bool) -> [a] -> [[a]]

# Documentation

type ContextDiff c = [Hunk c] #

type Hunk c = [Diff [c]] #

getContextDiff #

Arguments

:: Eq a |
=> Maybe Int | Number of context elements, Nothing means infinite
-> [a] |
-> [a] |
-> ContextDiff (Numbered a) |

    > let textA = ["a","b","c","d","e","f","g","h","i","j","k"]
    > let textB = ["a","b","d","e","f","g","h","i","j"]
    > let diff = getContextDiff (Just 2) textA textB
    > prettyContextDiff (text "file1") (text "file2") (text . unnumber) diff
    --- file1
    +++ file2
    @@ -1,5 +1,4 @@
     a
     b
    -c
     d
     e
    @@ -9,3 +8,2 @@
     i
     j
    -k

prettyContextDiff #

Arguments

:: Doc | Document 1 name
-> Doc | Document 2 name
-> (Numbered c -> Doc) | Element pretty printer
-> ContextDiff (Numbered c) |
-> Doc |

Pretty print a ContextDiff in the manner of diff -u.

prettyContextDiffOld #

Arguments

:: Doc | Document 1 name
-> Doc | Document 2 name
-> (c -> Doc) | Element pretty printer
-> ContextDiff c |
-> Doc |

Pretty print without line numbers.

getContextDiffNumbered #

Arguments

:: Eq a |
=> Maybe Int | Number of context elements, Nothing means infinite
-> [Numbered a] |
-> [Numbered a] |
-> ContextDiff (Numbered a) |

data Numbered a #

Constructors

Numbered Int a |

#### Instances

Instances details

Eq a => Eq (Numbered a) # |
Instance details Defined in Data.Algorithm.DiffContext Methods (==) :: Numbered a -> Numbered a -> Bool # (/=) :: Numbered a -> Numbered a -> Bool #
Ord a => Ord (Numbered a) # |
Instance details Defined in Data.Algorithm.DiffContext Methods compare :: Numbered a -> Numbered a -> Ordering # (<) :: Numbered a -> Numbered a -> Bool # (<=) :: Numbered a -> Numbered a -> Bool # (>) :: Numbered a -> Numbered a -> Bool # (>=) :: Numbered a -> Numbered a -> Bool # max :: Numbered a -> Numbered a -> Numbered a # min :: Numbered a -> Numbered a -> Numbered a #
Show a => Show (Numbered a) # |
Instance details Defined in Data.Algorithm.DiffContext Methods showsPrec :: Int -> Numbered a -> ShowS # show :: Numbered a -> String # showList :: [Numbered a] -> ShowS #

numbered :: [a] -> [Numbered a] #

unnumber :: Numbered a -> a #

unNumberContextDiff :: ContextDiff (Numbered a) -> ContextDiff a #

If for some reason you need the line numbers stripped from the result of getContextDiff for backwards compatibility.

groupBy' :: (a -> a -> Bool) -> [a] -> [[a]] #

A version of `groupBy` that does not assume the argument function is transitive. This is used to partition the `Diff` list into segments that begin and end with matching (`Both`) text, with and have non-matching (`First` and `Second`) text in the middle.

    let notBoth1 a b = not (a == 1 || b == 1) in

    groupBy' notBoth1 [1,1,2,3,1,1,4,5,6,1]
    [[1],[1,2,3,1],[1,4,5,6,1]]

    groupBy notBoth1 [1,1,2,3,1,1,4,5,6,1]
    [[1],[1,2,3],[1],[1,4,5,6],[1]]

In the first result the list is split anywhere there are two adjacent ones, as desired.