diff --git a/src/Renderer/Patch.hs b/src/Renderer/Patch.hs index e834a07bd..1aa6341ce 100644 --- a/src/Renderer/Patch.hs +++ b/src/Renderer/Patch.hs @@ -1,6 +1,7 @@ module Renderer.Patch ( patch, - hunks + hunks, + Hunk(..) ) where import Alignment @@ -76,16 +77,19 @@ getRange (Pure patch) = let Info range _ :< _ = getSplitTerm patch in range -- | Returns the header given two source blobs and a hunk. header :: Both SourceBlob -> Hunk a -> String -header blobs hunk = "diff --git a/" ++ pathA ++ " b/" ++ pathB ++ "\n" ++ - "index " ++ oidA ++ ".." ++ oidB ++ "\n" ++ - "@@ -" ++ show offsetA ++ "," ++ show lengthA ++ " +" ++ show offsetB ++ "," ++ show lengthB ++ " @@\n" - where (lengthA, lengthB) = runBoth . fmap getSum $ hunkLength hunk - (offsetA, offsetB) = runBoth . fmap getSum $ offset hunk - (pathA, pathB) = runBoth $ path <$> blobs - (oidA, oidB) = runBoth $ oid <$> blobs +header blobs hunk = filepathHeader ++ blobOidHeader ++ maybeOffsetHeader + where filepathHeader = "diff --git a/" ++ pathA ++ " b/" ++ pathB ++ "\n" + blobOidHeader = "index " ++ oidA ++ ".." ++ oidB ++ "\n" + maybeOffsetHeader = if lengthA > 0 && lengthB > 0 then offsetHeader else mempty + offsetHeader = "@@ -" ++ show offsetA ++ "," ++ show lengthA ++ " +" ++ show offsetB ++ "," ++ show lengthB ++ " @@\n" + (lengthA, lengthB) = runBoth . fmap getSum $ hunkLength hunk + (offsetA, offsetB) = runBoth . fmap getSum $ offset hunk + (pathA, pathB) = runBoth $ path <$> blobs + (oidA, oidB) = runBoth $ oid <$> blobs -- | Render a diff as a series of hunks. hunks :: Renderer a [Hunk (SplitDiff a Info)] +hunks diff blobs | Both (True, True) <- Source.null . source <$> blobs = [Hunk { offset = mempty, changes = [], trailingContext = [] }] hunks diff blobs = hunksInRows (Both (1, 1)) . Prelude.fst $ splitDiffByLines diff (pure 0) (source <$> blobs) -- | Given beginning line numbers, turn rows in a split diff into hunks in a diff --git a/src/Source.hs b/src/Source.hs index 070c8621a..9b1a97f75 100644 --- a/src/Source.hs +++ b/src/Source.hs @@ -5,6 +5,7 @@ import qualified Data.Vector as Vector import qualified Data.Text as T data SourceBlob = SourceBlob { source :: Source Char, oid :: String, path :: FilePath } + deriving (Show, Eq) -- | The contents of a source file, backed by a vector for efficient slicing. newtype Source a = Source { getVector :: Vector.Vector a } diff --git a/test/PatchOutputSpec.hs b/test/PatchOutputSpec.hs index c368b4431..04d2bf38e 100644 --- a/test/PatchOutputSpec.hs +++ b/test/PatchOutputSpec.hs @@ -12,5 +12,5 @@ import Test.Hspec spec :: Spec spec = parallel $ describe "hunks" $ - it "empty diffs have no hunks" $ - hunks (Free . Annotated (pure (Info (Range 0 0) mempty)) $ Leaf "") (Both (SourceBlob (fromList "") "abcde" "path2.txt", SourceBlob (fromList "") "xyz" "path2.txt")) `shouldBe` [] + it "empty diffs have empty hunks" $ + hunks (Free . Annotated (pure (Info (Range 0 0) mempty)) $ Leaf "") (Both (SourceBlob (fromList "") "abcde" "path2.txt", SourceBlob (fromList "") "xyz" "path2.txt")) `shouldBe` [Hunk {offset = Both (0, 0), changes = [], trailingContext = []}]