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

Merge pull request #406 from github/formats-and-renderers

Formats and Renderers
This commit is contained in:
Josh Vera 2016-01-14 13:58:14 -05:00
commit 6966a797a6
6 changed files with 17 additions and 6 deletions

View File

@ -24,10 +24,10 @@ import qualified Data.Text.ICU.Convert as Convert
import Data.Bifunctor.Join
-- | The available types of diff rendering.
data Renderer = Unified | Split | Patch
data Format = Unified | Split | Patch
-- | The command line arguments to the application.
data Arguments = Arguments { renderer :: Renderer, output :: Maybe FilePath, sourceA :: FilePath, sourceB :: FilePath }
data Arguments = Arguments { format :: Format, output :: Maybe FilePath, sourceA :: FilePath, sourceB :: FilePath }
-- | A parser for the application's command-line arguments.
arguments :: Parser Arguments
@ -53,7 +53,7 @@ main = do
-- | Print a diff, given the command-line arguments, source files, and terms.
printDiff :: Arguments -> (Source Char, Source Char) -> (Term T.Text Info, Term T.Text Info) -> IO ()
printDiff arguments (aSource, bSource) (aTerm, bTerm) = case renderer arguments of
printDiff arguments (aSource, bSource) (aTerm, bTerm) = case format arguments of
Unified -> do
rendered <- unified diff aSource bSource
B1.putStr rendered

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