1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 00:42:33 +03:00

Add a Renderer type for rendering diffs

This commit is contained in:
Matt Diephouse 2016-01-14 13:18:40 -05:00
parent f0760188b9
commit 9c4ea153bc
5 changed files with 14 additions and 3 deletions

View File

@ -30,6 +30,7 @@ library
, Split
, Unified
, Parser
, Renderer
, TreeSitter
, Source
build-depends: base >= 4.8 && < 5

View File

@ -6,6 +6,7 @@ module PatchOutput (
import Diff
import Line
import Range
import Renderer
import Row
import Source hiding ((++), break)
import Split
@ -15,7 +16,7 @@ import Control.Monad.Free
import Data.Maybe
import Data.Monoid
patch :: Diff a Info -> Source Char -> Source Char -> String
patch :: Renderer a String
patch diff sourceA sourceB = mconcat $ showHunk (sourceA, sourceB) <$> hunks diff (sourceA, sourceB)
data Hunk a = Hunk { offset :: (Sum Int, Sum Int), changes :: [Change a], trailingContext :: [Row a] }

7
src/Renderer.hs Normal file
View File

@ -0,0 +1,7 @@
module Renderer where
import Diff
import Source
-- | A function that will render a diff, given the two source files.
type Renderer a b = Diff a Info -> Source Char -> Source Char -> b

View File

@ -6,6 +6,7 @@ import Diff
import Line
import Row
import Patch
import Renderer
import Term
import Syntax
import Control.Comonad.Cofree
@ -30,7 +31,7 @@ type ClassName = T.Text
classifyMarkup :: Foldable f => f String -> Markup -> Markup
classifyMarkup categories element = maybe element ((element !) . A.class_ . stringValue . ("category-" ++)) $ maybeFirst categories
split :: Diff leaf Info -> Source Char -> Source Char -> IO TL.Text
split :: Renderer leaf (IO TL.Text)
split diff before after = return . renderHtml
. docTypeHtml
. ((head $ link ! A.rel "stylesheet" ! A.href "style.css") <>)

View File

@ -5,6 +5,7 @@ import Patch
import Syntax
import Term
import Range
import Renderer
import Source hiding ((++))
import Control.Arrow
import Control.Monad.Free
@ -13,7 +14,7 @@ import Data.List hiding (foldl)
import qualified Data.OrderedMap as Map
import Rainbow
unified :: Diff a Info -> Source Char -> Source Char -> IO ByteString
unified :: Renderer a (IO ByteString)
unified diff before after = do
renderer <- byteStringMakerFromEnvironment
return . mconcat . chunksToByteStrings renderer . fst $ iter g mapped where