2016-06-03 23:02:06 +03:00
|
|
|
{-# LANGUAGE DataKinds, TypeFamilies, ScopedTypeVariables #-}
|
2016-05-17 20:09:14 +03:00
|
|
|
module DiffSummary (DiffSummary(..), diffSummary, DiffInfo(..)) where
|
2016-04-25 18:46:10 +03:00
|
|
|
|
2016-05-26 21:04:27 +03:00
|
|
|
import Prologue hiding (fst, snd)
|
|
|
|
import Data.String
|
|
|
|
import Data.Maybe (fromJust)
|
2016-04-25 18:46:10 +03:00
|
|
|
import Diff
|
2016-06-03 02:10:41 +03:00
|
|
|
import Info (Info, category)
|
2016-04-25 18:46:10 +03:00
|
|
|
import Patch
|
2016-05-17 22:59:07 +03:00
|
|
|
import Term
|
2016-04-25 18:46:10 +03:00
|
|
|
import Syntax
|
2016-05-16 20:19:30 +03:00
|
|
|
import Category
|
2016-05-03 19:17:38 +03:00
|
|
|
import Data.Functor.Foldable as Foldable
|
2016-05-18 00:34:27 +03:00
|
|
|
import Data.Functor.Both
|
2016-05-18 20:27:19 +03:00
|
|
|
import Data.OrderedMap
|
2016-05-26 21:04:27 +03:00
|
|
|
import Data.Text as Text (unpack)
|
2016-04-25 18:46:10 +03:00
|
|
|
|
2016-05-24 20:06:15 +03:00
|
|
|
data DiffInfo = DiffInfo { categoryName :: String, termName :: Maybe String } deriving (Eq, Show)
|
2016-05-18 17:18:26 +03:00
|
|
|
|
2016-05-24 20:06:15 +03:00
|
|
|
maybeTermName :: HasCategory leaf => Term leaf Info -> Maybe String
|
|
|
|
maybeTermName term = case runCofree term of
|
|
|
|
(_ :< Leaf leaf) -> Just (toCategoryName leaf)
|
|
|
|
(_ :< Keyed children) -> Just (unpack . mconcat $ keys children)
|
2016-05-31 23:13:01 +03:00
|
|
|
(_ :< Indexed children) -> toCategoryName . category <$> head (extract <$> children)
|
|
|
|
(_ :< Fixed children) -> toCategoryName . category <$> head (extract <$> children)
|
2016-05-18 20:27:19 +03:00
|
|
|
|
2016-05-24 19:49:16 +03:00
|
|
|
class HasCategory a where
|
2016-05-24 20:06:15 +03:00
|
|
|
toCategoryName :: a -> String
|
2016-05-18 00:34:27 +03:00
|
|
|
|
2016-05-24 19:49:16 +03:00
|
|
|
instance HasCategory String where
|
2016-05-26 21:04:27 +03:00
|
|
|
toCategoryName = identity
|
2016-05-18 00:34:27 +03:00
|
|
|
|
2016-05-24 19:49:16 +03:00
|
|
|
instance HasCategory Text where
|
2016-05-24 20:06:15 +03:00
|
|
|
toCategoryName = unpack
|
2016-05-18 19:01:16 +03:00
|
|
|
|
2016-05-24 19:49:16 +03:00
|
|
|
instance HasCategory Category where
|
2016-05-24 20:06:15 +03:00
|
|
|
toCategoryName category = case category of
|
2016-06-04 01:34:42 +03:00
|
|
|
Program -> "top level"
|
2016-05-18 00:34:27 +03:00
|
|
|
BinaryOperator -> "binary operator"
|
2016-05-18 20:37:02 +03:00
|
|
|
DictionaryLiteral -> "dictionary"
|
2016-05-18 00:34:27 +03:00
|
|
|
Pair -> "pair"
|
|
|
|
FunctionCall -> "function call"
|
2016-05-18 20:37:02 +03:00
|
|
|
StringLiteral -> "string"
|
|
|
|
IntegerLiteral -> "integer"
|
|
|
|
SymbolLiteral -> "symbol"
|
|
|
|
ArrayLiteral -> "array"
|
2016-06-04 01:34:42 +03:00
|
|
|
Other s -> s
|
2016-05-17 20:09:14 +03:00
|
|
|
|
2016-05-24 19:49:16 +03:00
|
|
|
instance HasCategory leaf => HasCategory (Term leaf Info) where
|
2016-05-31 23:13:01 +03:00
|
|
|
toCategoryName = toCategoryName . category . extract
|
2016-05-24 19:49:16 +03:00
|
|
|
|
2016-05-17 20:09:14 +03:00
|
|
|
data DiffSummary a = DiffSummary {
|
2016-05-31 23:11:11 +03:00
|
|
|
patch :: Patch a,
|
|
|
|
parentAnnotations :: [a]
|
2016-05-17 20:09:14 +03:00
|
|
|
} deriving (Eq, Functor)
|
2016-05-13 18:44:03 +03:00
|
|
|
|
2016-05-31 23:11:11 +03:00
|
|
|
instance Show (DiffSummary DiffInfo) where
|
2016-05-26 21:04:27 +03:00
|
|
|
showsPrec _ DiffSummary{..} s = (++s) $ case patch of
|
2016-05-24 20:06:15 +03:00
|
|
|
(Insert termInfo) -> "Added the " ++ "'" ++ fromJust (termName termInfo) ++ "' " ++ categoryName termInfo
|
2016-05-24 19:37:44 +03:00
|
|
|
++ maybeParentContext parentAnnotations
|
2016-05-24 20:06:15 +03:00
|
|
|
(Delete termInfo) -> "Deleted the " ++ "'" ++ fromJust (termName termInfo) ++ "' " ++ categoryName termInfo
|
2016-05-24 19:37:44 +03:00
|
|
|
++ maybeParentContext parentAnnotations
|
2016-05-24 20:06:15 +03:00
|
|
|
(Replace t1 t2) -> "Replaced the " ++ "'" ++ fromJust (termName t1) ++ "' " ++ categoryName t1
|
|
|
|
++ " with the " ++ "'" ++ fromJust (termName t2) ++ "' " ++ categoryName t2
|
2016-05-24 19:37:44 +03:00
|
|
|
++ maybeParentContext parentAnnotations
|
|
|
|
where maybeParentContext parentAnnotations = if null parentAnnotations
|
|
|
|
then ""
|
2016-05-24 20:06:15 +03:00
|
|
|
else " in the " ++ intercalate "/" (categoryName <$> parentAnnotations) ++ " context"
|
2016-04-25 18:46:10 +03:00
|
|
|
|
2016-05-24 19:49:16 +03:00
|
|
|
diffSummary :: HasCategory leaf => Diff leaf Info -> [DiffSummary DiffInfo]
|
2016-05-16 17:54:05 +03:00
|
|
|
diffSummary = cata diffSummary' where
|
2016-05-26 20:46:33 +03:00
|
|
|
diffSummary' :: HasCategory leaf => Base (Diff leaf Info) [DiffSummary DiffInfo] -> [DiffSummary DiffInfo]
|
2016-05-16 21:29:36 +03:00
|
|
|
diffSummary' (Free (_ :< Leaf _)) = [] -- Skip leaves since they don't have any changes
|
2016-05-31 23:13:01 +03:00
|
|
|
diffSummary' (Free (infos :< Indexed children)) = prependSummary (DiffInfo (toCategoryName . category $ snd infos) Nothing) <$> join children
|
|
|
|
diffSummary' (Free (infos :< Fixed children)) = prependSummary (DiffInfo (toCategoryName . category $ snd infos) Nothing) <$> join children
|
|
|
|
diffSummary' (Free (infos :< Keyed children)) = prependSummary (DiffInfo (toCategoryName . category $ snd infos) Nothing) <$> join (Prologue.toList children)
|
2016-05-24 20:06:15 +03:00
|
|
|
diffSummary' (Pure (Insert term)) = [DiffSummary (Insert (DiffInfo (toCategoryName term) (maybeTermName term))) []]
|
|
|
|
diffSummary' (Pure (Delete term)) = [DiffSummary (Delete (DiffInfo (toCategoryName term) (maybeTermName term))) []]
|
|
|
|
diffSummary' (Pure (Replace t1 t2)) = [DiffSummary (Replace (DiffInfo (toCategoryName t1) (maybeTermName t1)) (DiffInfo (toCategoryName t2) (maybeTermName t2))) []]
|
2016-05-16 17:54:05 +03:00
|
|
|
|
2016-05-18 00:34:27 +03:00
|
|
|
prependSummary :: DiffInfo -> DiffSummary DiffInfo -> DiffSummary DiffInfo
|
2016-05-16 17:54:05 +03:00
|
|
|
prependSummary annotation summary = summary { parentAnnotations = annotation : parentAnnotations summary }
|