1
1
mirror of https://github.com/github/semantic.git synced 2024-12-29 18:06:14 +03:00
semantic/src/Renderer/Patch.hs

162 lines
7.7 KiB
Haskell
Raw Normal View History

module Renderer.Patch (
2015-12-18 17:48:04 +03:00
patch,
2016-03-02 01:13:53 +03:00
hunks,
Hunk(..)
2015-12-18 17:48:04 +03:00
) where
2015-12-18 01:22:09 +03:00
import Alignment
2015-12-18 01:22:53 +03:00
import Diff
import Line
2016-03-01 03:39:04 +03:00
import Prelude hiding (fst, snd)
import qualified Prelude
import Range
import Renderer
2015-12-25 01:41:45 +03:00
import Row
2015-12-31 01:42:18 +03:00
import Source hiding ((++), break)
import SplitDiff
import Control.Comonad.Cofree
import Control.Monad.Free
import Data.Functor.Both as Both
2016-03-09 07:24:50 +03:00
import Data.List
2015-12-31 02:29:52 +03:00
import Data.Maybe
2015-12-30 23:56:12 +03:00
import Data.Monoid
2015-12-18 01:22:53 +03:00
2016-02-03 20:58:54 +03:00
-- | Render a diff in the traditional patch format.
patch :: Renderer a String
patch diff sources = mconcat $ showHunk sources <$> hunks diff sources
2016-02-03 20:58:54 +03:00
-- | A hunk in a patch, including the offset, changes, and context.
data Hunk a = Hunk { offset :: Both (Sum Int), changes :: [Change a], trailingContext :: [Row a] }
2015-12-25 02:11:38 +03:00
deriving (Eq, Show)
2015-12-18 16:25:37 +03:00
2016-02-03 20:58:54 +03:00
-- | A change in a patch hunk, along with its preceding context.
data Change a = Change { context :: [Row a], contents :: [Row a] }
deriving (Eq, Show)
2016-02-03 20:58:54 +03:00
-- | The number of lines in the hunk before and after.
hunkLength :: Hunk a -> Both (Sum Int)
2015-12-31 00:25:18 +03:00
hunkLength hunk = mconcat $ (changeLength <$> changes hunk) <> (rowLength <$> trailingContext hunk)
2016-02-03 20:58:54 +03:00
-- | The number of lines in change before and after.
changeLength :: Change a -> Both (Sum Int)
changeLength change = mconcat $ (rowLength <$> context change) <> (rowLength <$> contents change)
2015-12-30 23:56:12 +03:00
2016-02-03 20:58:54 +03:00
-- | The number of lines in the row, each being either 0 or 1.
rowLength :: Row a -> Both (Sum Int)
2016-02-29 17:22:52 +03:00
rowLength = fmap lineLength . unRow
2015-12-30 23:56:03 +03:00
2016-02-03 20:58:54 +03:00
-- | The length of the line, being either 0 or 1.
2015-12-30 23:55:55 +03:00
lineLength :: Line a -> Sum Int
lineLength (Line []) = 0
2015-12-30 23:55:55 +03:00
lineLength _ = 1
2016-02-23 02:13:27 +03:00
-- | Given the before and after sources, render a hunk to a string.
showHunk :: Both SourceBlob -> Hunk (SplitDiff a Info) -> String
2016-03-09 09:00:04 +03:00
showHunk blobs hunk = header blobs hunk ++ concat (showChange sources <$> changes hunk) ++ showLines (snd sources) ' ' (snd . unRow <$> trailingContext hunk)
2016-02-29 05:28:24 +03:00
where sources = source <$> blobs
2016-02-03 20:58:54 +03:00
-- | Given the before and after sources, render a change to a string.
showChange :: Both (Source Char) -> Change (SplitDiff a Info) -> String
2016-03-09 09:00:04 +03:00
showChange sources change = showLines (snd sources) ' ' (snd . unRow <$> context change) ++ deleted ++ inserted
where (deleted, inserted) = runBoth $ pure showLines <*> sources <*> Both ('-', '+') <*> Both.unzip (unRow <$> contents change)
2015-12-30 23:27:04 +03:00
2016-02-03 20:58:54 +03:00
-- | Given a source, render a set of lines to a string with a prefix.
showLines :: Source Char -> Char -> [Line (SplitDiff leaf Info)] -> String
2016-02-22 23:31:52 +03:00
showLines source prefix lines = fromMaybe "" . mconcat $ fmap prepend . showLine source <$> lines
where prepend "" = ""
prepend source = prefix : source
2016-02-03 20:58:54 +03:00
-- | Given a source, render a line to a string.
2015-12-31 00:03:38 +03:00
showLine :: Source Char -> Line (SplitDiff leaf Info) -> Maybe String
showLine _ (Line []) = Nothing
2015-12-31 00:03:38 +03:00
showLine source line = Just . toString . (`slice` source) . unionRanges $ getRange <$> unLine line
2015-12-30 23:32:51 +03:00
2016-02-03 20:58:54 +03:00
-- | Return the range from a split diff.
2015-12-30 23:26:49 +03:00
getRange :: SplitDiff leaf Info -> Range
getRange (Free (Annotated (Info range _) _)) = range
getRange (Pure patch) = let Info range _ :< _ = getSplitTerm patch in range
2016-02-23 02:13:27 +03:00
-- | Returns the header given two source blobs and a hunk.
header :: Both SourceBlob -> Hunk a -> String
header blobs hunk = intercalate "\n" [filepathHeader, fileModeHeader, beforeFilepath, afterFilepath, maybeOffsetHeader]
2016-03-04 19:57:37 +03:00
where filepathHeader = "diff --git a/" ++ pathA ++ " b/" ++ pathB
fileModeHeader = case (modeA, modeB) of
(Nothing, Just mode) -> intercalate "\n" [ "new file mode " ++ modeToDigits mode, blobOidHeader ]
(Just mode, Nothing) -> intercalate "\n" [ "old file mode " ++ modeToDigits mode, blobOidHeader ]
(Just mode, Just other) | mode == other -> "index " ++ oidA ++ ".." ++ oidB ++ " " ++ modeToDigits mode
(Just mode1, Just mode2) -> intercalate "\n" [
"old mode" ++ modeToDigits mode1,
"new mode " ++ modeToDigits mode2 ++ " " ++ blobOidHeader
]
(Nothing, Nothing) -> ""
2016-03-04 19:57:37 +03:00
blobOidHeader = "index " ++ oidA ++ ".." ++ oidB
modeHeader :: String -> Maybe SourceKind -> String -> String
modeHeader ty maybeMode path = case maybeMode of
Just _ -> ty ++ "/" ++ path
2016-03-04 19:57:37 +03:00
Nothing -> "/dev/null"
beforeFilepath = "--- " ++ modeHeader "a" modeA pathA
afterFilepath = "+++ " ++ modeHeader "b" modeB pathB
maybeOffsetHeader = if lengthA > 0 && lengthB > 0
then offsetHeader
else mempty
2016-03-04 20:28:19 +03:00
offsetHeader = "@@ -" ++ offsetA ++ "," ++ show lengthA ++ " +" ++ offsetB ++ "," ++ show lengthB ++ " @@" ++ "\n"
2016-03-04 19:57:37 +03:00
(lengthA, lengthB) = runBoth . fmap getSum $ hunkLength hunk
2016-03-04 20:28:19 +03:00
(offsetA, offsetB) = runBoth . fmap (show . getSum) $ offset hunk
2016-03-04 19:57:37 +03:00
(pathA, pathB) = runBoth $ path <$> blobs
(oidA, oidB) = runBoth $ oid <$> blobs
(modeA, modeB) = runBoth $ blobKind <$> blobs
2015-12-18 16:07:43 +03:00
2016-02-03 20:58:54 +03:00
-- | Render a diff as a series of hunks.
2016-02-03 00:53:48 +03:00
hunks :: Renderer a [Hunk (SplitDiff a Info)]
2016-03-02 22:12:37 +03:00
hunks _ blobs | Both (True, True) <- Source.null . source <$> blobs = [Hunk { offset = mempty, changes = [], trailingContext = [] }]
hunks diff blobs = hunksInRows (Both (1, 1)) $ fmap Prelude.fst <$> splitDiffByLines (source <$> blobs) diff
2016-02-29 05:10:02 +03:00
-- | Given beginning line numbers, turn rows in a split diff into hunks in a
-- | patch.
hunksInRows :: Both (Sum Int) -> [Row (SplitDiff a Info)] -> [Hunk (SplitDiff a Info)]
hunksInRows start rows = case nextHunk start rows of
2016-03-02 01:13:53 +03:00
Nothing -> []
Just (hunk, rest) -> hunk : hunksInRows (offset hunk <> hunkLength hunk) rest
-- | Given beginning line numbers, return the next hunk and the remaining rows
-- | of the split diff.
nextHunk :: Both (Sum Int) -> [Row (SplitDiff a Info)] -> Maybe (Hunk (SplitDiff a Info), [Row (SplitDiff a Info)])
nextHunk start rows = case nextChange start rows of
Nothing -> Nothing
Just (offset, change, rest) -> let (changes, rest') = contiguousChanges rest in Just (Hunk offset (change : changes) $ take 3 rest', drop 3 rest')
2015-12-31 02:06:47 +03:00
where contiguousChanges rows = case break rowHasChanges (take 7 rows) of
(_, []) -> ([], rows)
(context, _) -> case changeIncludingContext context (drop (length context) rows) of
2015-12-31 01:54:27 +03:00
Nothing -> ([], rows)
Just (change, rest) -> let (changes, rest') = contiguousChanges rest in (change : changes, rest')
-- | Given beginning line numbers, return the number of lines to the next
-- | the next change, and the remaining rows of the split diff.
nextChange :: Both (Sum Int) -> [Row (SplitDiff a Info)] -> Maybe (Both (Sum Int), Change (SplitDiff a Info), [Row (SplitDiff a Info)])
2015-12-31 02:10:58 +03:00
nextChange start rows = case changeIncludingContext leadingContext afterLeadingContext of
Nothing -> Nothing
Just (change, afterChanges) -> Just (start <> mconcat (rowLength <$> skippedContext), change, afterChanges)
2015-12-31 02:10:58 +03:00
where (leadingRows, afterLeadingContext) = break rowHasChanges rows
2015-12-31 00:33:45 +03:00
(skippedContext, leadingContext) = splitAt (max (length leadingRows - 3) 0) leadingRows
2016-02-03 20:58:54 +03:00
-- | Return a Change with the given context and the rows from the begginning of
-- | the given rows that have changes, or Nothing if the first row has no
-- | changes.
changeIncludingContext :: [Row (SplitDiff a Info)] -> [Row (SplitDiff a Info)] -> Maybe (Change (SplitDiff a Info), [Row (SplitDiff a Info)])
changeIncludingContext leadingContext rows = case changes of
[] -> Nothing
_ -> Just (Change leadingContext changes, afterChanges)
where (changes, afterChanges) = span rowHasChanges rows
2016-02-03 20:58:54 +03:00
-- | Whether a row has changes on either side.
rowHasChanges :: Row (SplitDiff a Info) -> Bool
2016-02-29 17:22:52 +03:00
rowHasChanges (Row lines) = or (lineHasChanges <$> lines)
2016-02-03 20:58:54 +03:00
-- | Whether a line has changes.
lineHasChanges :: Line (SplitDiff a Info) -> Bool
lineHasChanges = or . fmap diffHasChanges
2016-02-03 20:58:54 +03:00
-- | Whether a split diff has changes.
diffHasChanges :: SplitDiff a Info -> Bool
diffHasChanges = or . fmap (const True)