diff --git a/app/Main.hs b/app/Main.hs index 6ee12f63c..5bb051545 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -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 diff --git a/semantic-diff.cabal b/semantic-diff.cabal index 06bfd4784..6b5803861 100644 --- a/semantic-diff.cabal +++ b/semantic-diff.cabal @@ -30,6 +30,7 @@ library , Split , Unified , Parser + , Renderer , TreeSitter , Source build-depends: base >= 4.8 && < 5 diff --git a/src/PatchOutput.hs b/src/PatchOutput.hs index b7e56eb33..4d8a2235a 100644 --- a/src/PatchOutput.hs +++ b/src/PatchOutput.hs @@ -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] } diff --git a/src/Renderer.hs b/src/Renderer.hs new file mode 100644 index 000000000..5414c1920 --- /dev/null +++ b/src/Renderer.hs @@ -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 diff --git a/src/Split.hs b/src/Split.hs index a815ab318..b1ea4aa41 100644 --- a/src/Split.hs +++ b/src/Split.hs @@ -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") <>) diff --git a/src/Unified.hs b/src/Unified.hs index 9505af74b..56b25fea4 100644 --- a/src/Unified.hs +++ b/src/Unified.hs @@ -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