1
1
mirror of https://github.com/github/semantic.git synced 2024-12-26 08:25:19 +03:00

Join the sums.

This commit is contained in:
Rob Rix 2016-02-28 21:20:58 -05:00
parent e33014ef27
commit b663e59c4c

View File

@ -24,7 +24,7 @@ patch :: Renderer a String
patch diff sources = mconcat $ showHunk sources <$> hunks diff sources patch diff sources = mconcat $ showHunk sources <$> hunks diff sources
-- | A hunk in a patch, including the offset, changes, and context. -- | A hunk in a patch, including the offset, changes, and context.
data Hunk a = Hunk { offset :: (Sum Int, Sum Int), changes :: [Change a], trailingContext :: [Row a] } data Hunk a = Hunk { offset :: Join (Sum Int), changes :: [Change a], trailingContext :: [Row a] }
deriving (Eq, Show) deriving (Eq, Show)
-- | A change in a patch hunk, along with its preceding context. -- | A change in a patch hunk, along with its preceding context.
@ -32,16 +32,16 @@ data Change a = Change { context :: [Row a], contents :: [Row a] }
deriving (Eq, Show) deriving (Eq, Show)
-- | The number of lines in the hunk before and after. -- | The number of lines in the hunk before and after.
hunkLength :: Hunk a -> (Sum Int, Sum Int) hunkLength :: Hunk a -> Join (Sum Int)
hunkLength hunk = mconcat $ (changeLength <$> changes hunk) <> (rowLength <$> trailingContext hunk) hunkLength hunk = mconcat $ (changeLength <$> changes hunk) <> (rowLength <$> trailingContext hunk)
-- | The number of lines in change before and after. -- | The number of lines in change before and after.
changeLength :: Change a -> (Sum Int, Sum Int) changeLength :: Change a -> Join (Sum Int)
changeLength change = mconcat $ (rowLength <$> context change) <> (rowLength <$> contents change) changeLength change = mconcat $ (rowLength <$> context change) <> (rowLength <$> contents change)
-- | The number of lines in the row, each being either 0 or 1. -- | The number of lines in the row, each being either 0 or 1.
rowLength :: Row a -> (Sum Int, Sum Int) rowLength :: Row a -> Join (Sum Int)
rowLength (Row a b) = (lineLength a, lineLength b) rowLength (Row a b) = pure lineLength <*> Join (a, b)
-- | The length of the line, being either 0 or 1. -- | The length of the line, being either 0 or 1.
lineLength :: Line a -> Sum Int lineLength :: Line a -> Sum Int
@ -78,27 +78,27 @@ header :: Join SourceBlob -> Hunk a -> String
header blobs hunk = "diff --git a/" ++ pathA ++ " b/" ++ pathB ++ "\n" ++ header blobs hunk = "diff --git a/" ++ pathA ++ " b/" ++ pathB ++ "\n" ++
"index " ++ oidA ++ ".." ++ oidB ++ "\n" ++ "index " ++ oidA ++ ".." ++ oidB ++ "\n" ++
"@@ -" ++ show offsetA ++ "," ++ show lengthA ++ " +" ++ show offsetB ++ "," ++ show lengthB ++ " @@\n" "@@ -" ++ show offsetA ++ "," ++ show lengthA ++ " +" ++ show offsetB ++ "," ++ show lengthB ++ " @@\n"
where (lengthA, lengthB) = join bimap getSum $ hunkLength hunk where (lengthA, lengthB) = runJoin . fmap getSum $ hunkLength hunk
(offsetA, offsetB) = join bimap getSum $ offset hunk (offsetA, offsetB) = runJoin . fmap getSum $ offset hunk
(pathA, pathB) = runJoin $ path <$> blobs (pathA, pathB) = runJoin $ path <$> blobs
(oidA, oidB) = runJoin $ oid <$> blobs (oidA, oidB) = runJoin $ oid <$> blobs
-- | Render a diff as a series of hunks. -- | Render a diff as a series of hunks.
hunks :: Renderer a [Hunk (SplitDiff a Info)] hunks :: Renderer a [Hunk (SplitDiff a Info)]
hunks diff blobs = hunksInRows (1, 1) . fst $ splitDiffByLines diff (0, 0) (before, after) hunks diff blobs = hunksInRows (Join (1, 1)) . fst $ splitDiffByLines diff (0, 0) (before, after)
where where
(before, after) = runJoin $ source <$> blobs (before, after) = runJoin $ source <$> blobs
-- | Given beginning line numbers, turn rows in a split diff into hunks in a -- | Given beginning line numbers, turn rows in a split diff into hunks in a
-- | patch. -- | patch.
hunksInRows :: (Sum Int, Sum Int) -> [Row (SplitDiff a Info)] -> [Hunk (SplitDiff a Info)] hunksInRows :: Join (Sum Int) -> [Row (SplitDiff a Info)] -> [Hunk (SplitDiff a Info)]
hunksInRows start rows = case nextHunk start rows of hunksInRows start rows = case nextHunk start rows of
Nothing -> [] Nothing -> []
Just (hunk, rest) -> hunk : hunksInRows (offset hunk <> hunkLength hunk) rest Just (hunk, rest) -> hunk : hunksInRows (offset hunk <> hunkLength hunk) rest
-- | Given beginning line numbers, return the next hunk and the remaining rows -- | Given beginning line numbers, return the next hunk and the remaining rows
-- | of the split diff. -- | of the split diff.
nextHunk :: (Sum Int, Sum Int) -> [Row (SplitDiff a Info)] -> Maybe (Hunk (SplitDiff a Info), [Row (SplitDiff a Info)]) nextHunk :: Join (Sum Int) -> [Row (SplitDiff a Info)] -> Maybe (Hunk (SplitDiff a Info), [Row (SplitDiff a Info)])
nextHunk start rows = case nextChange start rows of nextHunk start rows = case nextChange start rows of
Nothing -> Nothing Nothing -> Nothing
Just (offset, change, rest) -> let (changes, rest') = contiguousChanges rest in Just (Hunk offset (change : changes) $ take 3 rest', drop 3 rest') Just (offset, change, rest) -> let (changes, rest') = contiguousChanges rest in Just (Hunk offset (change : changes) $ take 3 rest', drop 3 rest')
@ -110,7 +110,7 @@ nextHunk start rows = case nextChange start rows of
-- | Given beginning line numbers, return the number of lines to the next -- | Given beginning line numbers, return the number of lines to the next
-- | the next change, and the remaining rows of the split diff. -- | the next change, and the remaining rows of the split diff.
nextChange :: (Sum Int, Sum Int) -> [Row (SplitDiff a Info)] -> Maybe ((Sum Int, Sum Int), Change (SplitDiff a Info), [Row (SplitDiff a Info)]) nextChange :: Join (Sum Int) -> [Row (SplitDiff a Info)] -> Maybe (Join (Sum Int), Change (SplitDiff a Info), [Row (SplitDiff a Info)])
nextChange start rows = case changeIncludingContext leadingContext afterLeadingContext of nextChange start rows = case changeIncludingContext leadingContext afterLeadingContext of
Nothing -> Nothing Nothing -> Nothing
Just (change, afterChanges) -> Just (start <> mconcat (rowLength <$> skippedContext), change, afterChanges) Just (change, afterChanges) -> Just (start <> mconcat (rowLength <$> skippedContext), change, afterChanges)