From 9765775242642200c88e966b6e175c2693358997 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Sat, 9 Sep 2017 16:55:32 +0100 Subject: [PATCH] Define printDiff as a catamorphism. --- src/Renderer/SExpression.hs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Renderer/SExpression.hs b/src/Renderer/SExpression.hs index c34f5b816..5d518bbc0 100644 --- a/src/Renderer/SExpression.hs +++ b/src/Renderer/SExpression.hs @@ -6,6 +6,7 @@ module Renderer.SExpression import Data.Bifunctor.Join import Data.ByteString.Char8 hiding (foldr, spanEnd) +import Data.Functor.Foldable (cata) import Data.Record import Data.Semigroup import Diff @@ -14,20 +15,20 @@ import Prelude hiding (replicate) import Term -- | Returns a ByteString SExpression formatted diff. -renderSExpressionDiff :: (ConstrainAll Show fields, Foldable f) => Diff f (Record fields) -> ByteString +renderSExpressionDiff :: (ConstrainAll Show fields, Foldable f, Functor f) => Diff f (Record fields) -> ByteString renderSExpressionDiff diff = printDiff diff 0 <> "\n" -- | Returns a ByteString SExpression formatted term. -renderSExpressionTerm :: (ConstrainAll Show fields, Foldable f) => Term f (Record fields) -> ByteString +renderSExpressionTerm :: (ConstrainAll Show fields, Foldable f, Functor f) => Term f (Record fields) -> ByteString renderSExpressionTerm term = printTerm term 0 <> "\n" -printDiff :: (ConstrainAll Show fields, Foldable f) => Diff f (Record fields) -> Int -> ByteString -printDiff diff level = case unDiff diff of +printDiff :: (ConstrainAll Show fields, Foldable f, Functor f) => Diff f (Record fields) -> Int -> ByteString +printDiff = cata $ \ diff level -> case diff of Patch patch -> case patch of Insert term -> pad (level - 1) <> "{+" <> printTerm term level <> "+}" Delete term -> pad (level - 1) <> "{-" <> printTerm term level <> "-}" Replace a b -> pad (level - 1) <> "{ " <> printTerm a level <> pad (level - 1) <> "->" <> printTerm b level <> " }" - Copy (Join (_, annotation)) syntax -> pad' level <> "(" <> showAnnotation annotation <> foldr (\d acc -> printDiff d (level + 1) <> acc) "" syntax <> ")" + Copy (Join (_, annotation)) syntax -> pad' level <> "(" <> showAnnotation annotation <> foldr (\d acc -> d (level + 1) <> acc) "" syntax <> ")" where pad' :: Int -> ByteString pad' n = if n < 1 then "" else pad n