1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 08:54:07 +03:00

Everything is lists of chunks.

This commit is contained in:
Rob Rix 2015-11-27 14:28:06 -05:00
parent 4a6a17ce26
commit 8b74a99e7e

View File

@ -14,26 +14,26 @@ import Rainbow
unified :: Diff a Info -> String -> String -> IO ByteString
unified diff before after = do
renderer <- byteStringMakerFromEnvironment
return . mconcat . chunksToByteStrings renderer . pure . fst $ iter g mapped where
return . mconcat . chunksToByteStrings renderer . fst $ iter g mapped where
mapped = fmap (unifiedPatch &&& range) diff
g (Annotated (_, info) syntax) = f info syntax
f (Info range _) (Leaf _) = (chunk $ substring range after, Just range)
f (Info range _) (Leaf _) = (pure . chunk $ substring range after, Just range)
f (Info range _) (Indexed i) = (unifiedRange range i after, Just range)
f (Info range _) (Fixed f) = (unifiedRange range f after, Just range)
f (Info range _) (Keyed k) = (unifiedRange range (sort $ snd <$> Map.toList k) after, Just range)
unifiedPatch :: Patch (Term a Info) -> Chunk String
unifiedPatch patch = (beforeChunk & fore red & bold) <> (afterChunk & fore green & bold) where
beforeChunk = maybe (chunk "") (change "-" . unifiedTerm before) $ Patch.before patch
afterChunk = maybe (chunk "") (change "+" . unifiedTerm after) $ Patch.after patch
unifiedPatch :: Patch (Term a Info) -> [Chunk String]
unifiedPatch patch = (fore red . bold <$> beforeChunk) <> (fore green . bold <$> afterChunk) where
beforeChunk = maybe [] (change "-" . unifiedTerm before) $ Patch.before patch
afterChunk = maybe [] (change "+" . unifiedTerm after) $ Patch.after patch
unifiedTerm :: String -> Term a Info -> Chunk String
unifiedTerm :: String -> Term a Info -> [Chunk String]
unifiedTerm source term = fst $ cata f term
unifiedRange :: Range -> [(Chunk String, Maybe Range)] -> String -> Chunk String
unifiedRange range children source = out <> (chunk $ substring Range { start = previous, end = end range } after) where
(out, previous) = foldl accumulateContext (chunk "", start range) children
accumulateContext (out, previous) (child, Just range) = (mconcat [ out, chunk $ substring Range { start = previous, end = start range } source, child ], end range)
unifiedRange :: Range -> [([Chunk String], Maybe Range)] -> String -> [Chunk String]
unifiedRange range children source = out <> (pure . chunk $ substring Range { start = previous, end = end range } after) where
(out, previous) = foldl accumulateContext ([], start range) children
accumulateContext (out, previous) (child, Just range) = (mconcat [ out, pure . chunk $ substring Range { start = previous, end = start range } source, child ], end range)
accumulateContext (out, previous) (child, _) = (out <> child, previous)
substring :: Range -> String -> String
@ -44,8 +44,8 @@ range patch = range . extract <$> after patch where
extract (annotation :< _) = annotation
range (Info range _) = range
change :: String -> Chunk String -> Chunk String
change bound content = mconcat [ chunk "{", chunk bound, content, chunk bound, chunk "}" ]
change :: String -> [Chunk String] -> [Chunk String]
change bound content = [ chunk "{", chunk bound ] ++ content ++ [ chunk bound, chunk "}" ]
instance Ord Range where
a <= b = start a <= start b