1
1
mirror of https://github.com/github/semantic.git synced 2024-12-21 13:51:44 +03:00

Render diffs to JSON w/o aligning.

This commit is contained in:
Rob Rix 2017-05-16 12:42:50 -04:00
parent 1cd523b9eb
commit cb9ec9e65e

View File

@ -6,7 +6,6 @@ module Renderer.JSON
, ToJSONFields(..)
) where
import Alignment
import Data.Aeson (ToJSON, toJSON, encode, object, (.=))
import Data.Aeson as A hiding (json)
import Data.Bifunctor.Join
@ -16,10 +15,10 @@ import Data.These
import Data.Vector as Vector hiding (toList)
import Diff
import Info
import Patch
import Prologue hiding ((++))
import qualified Data.Map as Map
import Source
import SplitDiff
import Syntax as S
--
@ -28,12 +27,11 @@ import Syntax as S
-- | Render a diff to a string representing its JSON.
json :: (ToJSONFields (Record fields), HasField fields Range) => Both SourceBlob -> Diff (Syntax Text) (Record fields) -> Map Text Value
json blobs diff = Map.fromList [
("rows", toJSON (annotateRows (alignDiff (source <$> blobs) diff))),
("oids", toJSON (oid <$> blobs)),
("paths", toJSON (path <$> blobs)) ]
where annotateRows :: [Join These a] -> [Join These (NumberedLine a)]
annotateRows = fmap (fmap NumberedLine) . numberedRows
json blobs diff = Map.fromList
[ ("diff", toJSON diff)
, ("oids", toJSON (oid <$> blobs))
, ("paths", toJSON (path <$> blobs))
]
-- | A numbered 'a'.
newtype NumberedLine a = NumberedLine (Int, a)
@ -49,6 +47,9 @@ instance ToJSON a => ToJSON (Join These a) where
toJSON (Join vs) = A.Array . Vector.fromList $ toJSON <$> these pure pure (\ a b -> [ a, b ]) vs
toEncoding = foldable
instance ToJSON a => ToJSONFields (Join (,) a) where
toJSONFields (Join (a, b)) = [ "before" .= a, "after" .= b ]
instance ToJSON a => ToJSON (Join (,) a) where
toJSON (Join (a, b)) = A.Array . Vector.fromList $ toJSON <$> [ a, b ]
@ -73,6 +74,10 @@ instance (ToJSONFields h, ToJSONFields (Record t)) => ToJSONFields (Record (h ':
instance ToJSONFields (Record '[]) where
toJSONFields _ = []
instance ToJSONFields (Record fs) => ToJSON (Record fs) where
toJSON = object . toJSONFields
toEncoding = pairs . mconcat . toJSONFields
instance ToJSONFields Range where
toJSONFields Range{..} = ["sourceRange" .= [ start, end ]]
@ -101,10 +106,10 @@ instance (ToJSONFields a, ToJSONFields (f b)) => ToJSONFields (FreeF f a b) wher
toJSONFields (Free f) = toJSONFields f
toJSONFields (Pure a) = toJSONFields a
instance ToJSON a => ToJSONFields (SplitPatch a) where
toJSONFields (SplitInsert a) = [ "insert" .= a ]
toJSONFields (SplitDelete a) = [ "delete" .= a ]
toJSONFields (SplitReplace a) = [ "replace" .= a ]
instance ToJSON a => ToJSONFields (Patch a) where
toJSONFields (Insert a) = [ "insert" .= a ]
toJSONFields (Delete a) = [ "delete" .= a ]
toJSONFields (Replace a b) = [ "replace" .= [a, b] ]
instance ToJSON a => ToJSONFields [a] where
toJSONFields list = [ "children" .= list ]