Diff 0.3.0 → 0.3.1
raw patch · 4 files changed
+120/−42 lines, 4 filesdep ~basePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base
API changes (from Hackage documentation)
+ Data.Algorithm.DiffContext: getContextDiff :: Eq a => Int -> [a] -> [a] -> ContextDiff a
+ Data.Algorithm.DiffContext: prettyContextDiff :: Doc -> Doc -> (c -> Doc) -> ContextDiff c -> Doc
Files
- Diff.cabal +36/−39
- src/Data/Algorithm/Diff.hs +1/−1
- src/Data/Algorithm/DiffContext.hs +83/−0
- src/Data/Algorithm/DiffOutput.hs +0/−2
Diff.cabal view
@@ -1,39 +1,36 @@-name: Diff -version: 0.3.0 -synopsis: O(ND) diff algorithm in haskell. -description: Basic implementation of the standard diff algorithm. -category: Algorithms -license: BSD3 -license-file: LICENSE -author: Sterling Clover -maintainer: s.clover@gmail.com -Tested-With: GHC == 6.8.2 -Build-Type: Simple -build-Depends: base -Cabal-Version: >= 1.6 - -flag small-base - -library - if flag(small-base) - build-depends: base >= 3 && <= 6, array - else - build-depends: base < 3 - hs-source-dirs: src - exposed-modules: - Data.Algorithm.Diff, - Data.Algorithm.DiffOutput - ghc-options: -O2 -Wall -funbox-strict-fields - build-depends: pretty - --- test-suite properties --- hs-source-dirs: src, test --- main-is: Test.hs --- type: exitcode-stdio-1.0 - --- build-depends: --- QuickCheck >= 2.4, test-framework >= 0.4, test-framework-quickcheck2 >= 0.2, process, directory - -source-repository head - type: darcs - location: http://patch-tag.com/r/sclv/diff +name: Diff+version: 0.3.1+synopsis: O(ND) diff algorithm in haskell.+description: Implementation of the standard diff algorithm, and utilities for pretty printing.+category: Algorithms+license: BSD3+license-file: LICENSE+author: Sterling Clover+maintainer: s.clover@gmail.com+Tested-With: GHC == 7.8.4+Build-Type: Simple+build-Depends: base+Cabal-Version: >= 1.6++flag small-base++library+ build-depends: base >= 3 && <= 6, array, pretty+ hs-source-dirs: src+ exposed-modules:+ Data.Algorithm.Diff,+ Data.Algorithm.DiffOutput+ Data.Algorithm.DiffContext+ ghc-options: -O2 -Wall -funbox-strict-fields++-- test-suite properties+-- hs-source-dirs: src, test+-- main-is: Test.hs+-- type: exitcode-stdio-1.0++-- build-depends:+-- QuickCheck >= 2.4, test-framework >= 0.4, test-framework-quickcheck2 >= 0.2, process, directory++source-repository head+ type: darcs+ location: http://hub.darcs.net/sterlingclover/Diff
src/Data/Algorithm/Diff.hs view
@@ -38,7 +38,7 @@ data DL = DL {poi :: !Int, poj :: !Int, path::[DI]} deriving (Show, Eq) -instance Ord DL +instance Ord DL where x <= y = if poi x == poi y then poj x > poj y else poi x <= poi y
+ src/Data/Algorithm/DiffContext.hs view
@@ -0,0 +1,83 @@+-----------------------------------------------------------------------------+-- |+-- Module : Data.Algorithm.DiffContext+-- Copyright : (c) David Fox (2015)+-- License : BSD 3 Clause+-- Maintainer : s.clover@gmail.com+-- Stability : experimental+-- Portability : portable+-- Author : David Fox (ddssff at the email service from google)+--+-- Generates a grouped diff with merged runs, and outputs them in the manner of diff -u+-----------------------------------------------------------------------------+module Data.Algorithm.DiffContext+ ( getContextDiff+ , prettyContextDiff+ ) where++import Data.Algorithm.Diff (Diff(..), getGroupedDiff)+import Data.List (groupBy)+import Data.Monoid ((<>))+import Text.PrettyPrint (Doc, text, empty, hcat)++type ContextDiff c = [[Diff [c]]]++-- | Do a grouped diff and then split up the chunks into runs that+-- contain differences surrounded by N lines of unchanged text. If+-- there is less then 2N+1 lines of unchanged text between two+-- changes, the runs are left merged.+getContextDiff :: Eq a => Int -> [a] -> [a] -> ContextDiff a+getContextDiff context a b =+ group $ swap $ trimTail $ trimHead $ concatMap split $ getGroupedDiff a b+ where+ -- Drop the middle elements of a run of Both if there are more+ -- than enough to form the context of the preceding changes and+ -- the following changes.+ split (Both xs ys) =+ case length xs of+ n | n > (2 * context) -> [Both (take context xs) (take context ys), Both (drop (n - context) xs) (drop (n - context) ys)]+ _ -> [Both xs ys]+ split x = [x]+ -- If split created a pair of Both runs at the beginning or end+ -- of the diff, remove the outermost.+ trimHead [] = []+ trimHead [Both _ _] = []+ trimHead [Both _ _, Both _ _] = []+ trimHead (Both _ _ : x@(Both _ _) : more) = x : more+ trimHead xs = trimTail xs+ trimTail [x@(Both _ _), Both _ _] = [x]+ trimTail (x : more) = x : trimTail more+ trimTail [] = []+ -- If we see Second before First swap them so that the deletions+ -- appear before the additions.+ swap (x@(Second _) : y@(First _) : xs) = y : x : swap xs+ swap (x : xs) = x : swap xs+ swap [] = []+ -- Split the list wherever we see adjacent Both constructors+ group xs =+ groupBy (\ x y -> not (isBoth x && isBoth y)) xs+ where+ isBoth (Both _ _) = True+ isBoth _ = False++-- | Pretty print a ContextDiff in the manner of diff -u.+prettyContextDiff ::+ Doc -- ^ Document 1 name+ -> Doc -- ^ Document 2 name+ -> (c -> Doc) -- ^ Element pretty printer+ -> ContextDiff c+ -> Doc+prettyContextDiff _ _ _ [] = empty+prettyContextDiff old new prettyElem hunks =+ hcat . map (<> text "\n") $ (text "--- " <> old :+ text "+++ " <> new :+ concatMap prettyRun hunks)+ where+ -- Pretty print a run of adjacent changes+ prettyRun hunk =+ text "@@" : concatMap prettyChange hunk++ -- Pretty print a single change (e.g. one line of a text file)+ prettyChange (Both ts _) = map (\ l -> text " " <> prettyElem l) ts+ prettyChange (First ts) = map (\ l -> text "-" <> prettyElem l) ts+ prettyChange (Second ts) = map (\ l -> text "+" <> prettyElem l) ts
src/Data/Algorithm/DiffOutput.hs view
@@ -11,9 +11,7 @@ -- Generates a string output that is similar to diff normal mode ----------------------------------------------------------------------------- module Data.Algorithm.DiffOutput where- import Data.Algorithm.Diff- import Text.PrettyPrint -- | pretty print the differences. The output is similar to the output of the diff utility