pretty-diff 0.4.0.1 → 0.4.0.2
raw patch · 4 files changed
+114/−11 lines, 4 files
Files
- CHANGELOG.md +2/−0
- pretty-diff.cabal +2/−2
- src/Pretty/Diff.hs +42/−9
- test/Main.hs +68/−0
CHANGELOG.md view
@@ -1,3 +1,5 @@+# 0.4.0.2+- small fix for different line number count # 0.4.0.1 - small fix for smaller diff context # 0.4.0.0
pretty-diff.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 2de8bb4300f0e1ec9cb8d75f7c3bf5d103cc03bdff7e05ca1afee805857aaafa+-- hash: 4f793bc4b441a763102c87017e021f3fab936f2ee528c21b41c3a8444c5922b9 name: pretty-diff-version: 0.4.0.1+version: 0.4.0.2 synopsis: Pretty printing a diff of two values. description: Please see the README at <https://github.com/stoeffel/pretty-diff>. category: Diffing
src/Pretty/Diff.hs view
@@ -38,7 +38,7 @@ import qualified Data.Algorithm.Diff as Diff import Data.Default (Default, def) import Data.Function ((&))-import Data.List (take, transpose, zipWith)+import Data.List (take, transpose) import Data.Maybe (fromMaybe, mapMaybe) import Data.String (IsString) import Data.Text (Text)@@ -108,12 +108,17 @@ pretty Config {separatorText, wrapping, multilineContext} x y = let xs = Text.lines x ys = Text.lines y- in [ zipWith (above' wrapping) xs ys & extractContext multilineContext (False, [], []) & mconcat,+ in [ zipWithSameLength (above' wrapping) xs ys & extractContext multilineContext (False, [], []) & mconcat, separator separatorText,- zipWith (below' wrapping) xs ys & extractContext multilineContext (False, [], []) & mconcat+ zipWithSameLength (below' wrapping) xs ys & extractContext multilineContext (False, [], []) & mconcat ] & mconcat +zipWithSameLength :: (Maybe a -> Maybe a -> b) -> [a] -> [a] -> [b]+zipWithSameLength f [] ys = map (\y -> f Nothing (Just y)) ys+zipWithSameLength f xs [] = map (\x -> f (Just x) Nothing) xs+zipWithSameLength f (x : xs) (y : ys) = f (Just x) (Just y) : zipWithSameLength f xs ys+ -- | Printing The first value and the diff indicator above. -- -- @@@ -126,11 +131,25 @@ -- @ above :: Wrapping -> Text -> Text -> Text above wrapping x y =- let (_, res) = above' wrapping x y+ let (_, res) = above' wrapping (Just x) (Just y) in res -above' :: Wrapping -> Text -> Text -> (Bool, Text)-above' wrapping x y =+above' :: Wrapping -> Maybe Text -> Maybe Text -> (Bool, Text)+above' wrapping Nothing (Just y) =+ ( True,+ withDiffLine First down (map Diff.Second $ Text.unpack y)+ & wrap wrapping+ & filterEmptyLines+ & Text.unlines+ )+above' wrapping (Just x) Nothing =+ ( True,+ withDiffLine First down (map Diff.First $ Text.unpack x)+ & wrap wrapping+ & filterEmptyLines+ & Text.unlines+ )+above' wrapping (Just x) (Just y) = let diffs = Diff.getDiff (Text.unpack x)@@ -154,11 +173,25 @@ -- @ below :: Wrapping -> Text -> Text -> Text below wrapping x y =- let (_, res) = below' wrapping x y+ let (_, res) = below' wrapping (Just x) (Just y) in res -below' :: Wrapping -> Text -> Text -> (Bool, Text)-below' wrapping x y =+below' :: Wrapping -> Maybe Text -> Maybe Text -> (Bool, Text)+below' wrapping Nothing (Just y) =+ ( True,+ withDiffLine Second up (map Diff.Second $ Text.unpack y)+ & wrap wrapping+ & filterEmptyLines+ & Text.unlines+ )+below' wrapping (Just x) Nothing =+ ( True,+ withDiffLine Second up (map Diff.First $ Text.unpack x)+ & wrap wrapping+ & filterEmptyLines+ & Text.unlines+ )+below' wrapping (Just x) (Just y) = let diffs = Diff.getDiff (Text.unpack x)
test/Main.hs view
@@ -282,6 +282,74 @@ "c", "d", "e"+ ],+ testCase "wrapping w/ surrounding multilineContext" $+ expectDiffToEqual+ ( Diff.pretty+ def {Diff.wrapping = Diff.Wrap 3, Diff.multilineContext = Diff.Surrounding 1 "..."}+ "a\nb\nc\nxdddddddd\ne\n"+ "a\nb\nc\nddddddddd\ne\n"+ )+ [ "...",+ "c",+ "▼",+ "xdd",+ "ddd",+ "ddd",+ "e",+ "╷",+ "│",+ "╵",+ "...",+ "c",+ "ddd",+ "ddd",+ "ddd",+ " ▲",+ "e"+ ],+ testCase "few lines (no diff)" $+ expectDiffToEqual+ ( Diff.pretty+ def {Diff.wrapping = Diff.NoWrap, Diff.multilineContext = Diff.Surrounding 1 "..."}+ "a\n"+ "a\n"+ )+ [ "a",+ "╷",+ "│",+ "╵",+ "a"+ ],+ testCase "first is longer" $+ expectDiffToEqual+ ( Diff.pretty+ def+ "a\nB"+ "a\n"+ )+ [ "a",+ "▼",+ "B",+ "╷",+ "│",+ "╵",+ "a"+ ],+ testCase "second is longer" $+ expectDiffToEqual+ ( Diff.pretty+ def+ "a\n"+ "a\nB"+ )+ [ "a",+ "╷",+ "│",+ "╵",+ "a",+ "B",+ "▲" ] ]