1
1
mirror of https://github.com/github/semantic.git synced 2024-12-01 09:15:01 +03:00

Merge branch 'master' into forward-compatible-rendering

This commit is contained in:
Rob Rix 2016-03-02 11:31:09 -05:00
commit 53413003c6
3 changed files with 15 additions and 10 deletions

View File

@ -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

View File

@ -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 }

View File

@ -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 = []}]