diff --git a/src/Renderer/SExpression.hs b/src/Renderer/SExpression.hs index 0aa479753..6c03bb4f7 100644 --- a/src/Renderer/SExpression.hs +++ b/src/Renderer/SExpression.hs @@ -17,31 +17,28 @@ import Term -- | Returns a ByteString SExpression formatted diff. renderSExpressionDiff :: (ConstrainAll Show fields, Foldable f, Functor f) => Diff f (Record fields) -> ByteString -renderSExpressionDiff diff = printDiff diff 0 <> "\n" +renderSExpressionDiff diff = cata printDiffF diff 0 0 <> "\n" -- | Returns a ByteString SExpression formatted term. renderSExpressionTerm :: (ConstrainAll Show fields, Foldable f, Functor f) => Term f (Record fields) -> ByteString -renderSExpressionTerm term = printTerm term 0 <> "\n" +renderSExpressionTerm term = cata printTermF term 0 0 <> "\n" -printDiff :: (ConstrainAll Show fields, Foldable f, Functor f) => Diff f (Record fields) -> Int -> ByteString -printDiff = cata $ \ diff level -> case diff of +printDiffF :: (ConstrainAll Show fields, Foldable f, Functor f) => DiffF f (Record fields) (Int -> Int -> ByteString) -> Int -> Int -> ByteString +printDiffF diff parentLevel level = case diff of Patch patch -> case patch of - Insert term -> pad (level - 1) <> "{+" <> printTermF term level <> "+}" - Delete term -> pad (level - 1) <> "{-" <> printTermF term level <> "-}" - Replace a b -> pad (level - 1) <> "{ " <> printTermF a level <> pad (level - 1) <> "->" <> printTermF b level <> " }" - Copy vs (Join (_, annotation)) syntax -> pad level <> "(" <> showBindings (fmap ($ 0) <$> vs) <> showAnnotation annotation <> foldr (\d acc -> d (level + 1) <> acc) "" syntax <> ")" - Var v -> pad level <> showMetaVar v + Insert term -> pad parentLevel (level - 1) <> "{+" <> printTermF term (parentLevel + level) 0 <> "+}" + Delete term -> pad parentLevel (level - 1) <> "{-" <> printTermF term (parentLevel + level) 0 <> "-}" + Replace a b -> pad parentLevel (level - 1) <> "{ " <> printTermF a (parentLevel + level) 0 <> pad parentLevel (level - 1) <> "->" <> printTermF b (parentLevel + level) 0 <> " }" + Copy vs (Join (_, annotation)) syntax -> pad parentLevel level <> "(" <> showBindings (fmap (($ level) . ($ parentLevel)) <$> vs) <> showAnnotation annotation <> foldr (\d -> (d parentLevel (level + 1) <>)) "" syntax <> ")" + Var v -> pad parentLevel level <> showMetaVar v -printTerm :: (ConstrainAll Show fields, Foldable f, Functor f) => Term f (Record fields) -> Int -> ByteString -printTerm term level = cata printTermF term level +printTermF :: (ConstrainAll Show fields, Foldable f, Functor f) => TermF f (Record fields) (Int -> Int -> ByteString) -> Int -> Int -> ByteString +printTermF (annotation :< syntax) parentLevel level = + pad parentLevel level <> "(" <> showAnnotation annotation <> foldr (\t -> (t parentLevel (level + 1) <>)) "" syntax <> ")" -printTermF :: (ConstrainAll Show fields, Foldable f, Functor f) => TermF f (Record fields) (Int -> ByteString) -> Int -> ByteString -printTermF (annotation :< syntax) level = - pad level <> "(" <> showAnnotation annotation <> foldr (\t -> (t (level + 1) <>)) "" syntax <> ")" - -pad :: Int -> ByteString -pad n | n <= 0 = "" - | otherwise = "\n" <> replicate (2 * n) ' ' +pad :: Int -> Int -> ByteString +pad p n | n <= 0 = "" + | otherwise = "\n" <> replicate (2 * (p + n)) ' ' showAnnotation :: ConstrainAll Show fields => Record fields -> ByteString