2016-06-03 23:02:06 +03:00
|
|
|
{-# LANGUAGE DataKinds, TypeFamilies, ScopedTypeVariables #-}
|
2016-06-07 02:41:07 +03:00
|
|
|
|
2016-05-17 20:09:14 +03:00
|
|
|
module DiffSummary (DiffSummary(..), diffSummary, DiffInfo(..)) where
|
2016-04-25 18:46:10 +03:00
|
|
|
|
2016-06-07 02:41:07 +03:00
|
|
|
import Prologue hiding (fst, snd, intercalate)
|
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-06-07 02:41:07 +03:00
|
|
|
import Data.Text as Text (intercalate, unpack)
|
2016-04-25 18:46:10 +03:00
|
|
|
|
2016-06-08 21:50:33 +03:00
|
|
|
data DiffInfo = DiffInfo { categoryName :: Text, termName :: Text } deriving (Eq, Show)
|
2016-05-18 17:18:26 +03:00
|
|
|
|
2016-06-08 21:50:33 +03:00
|
|
|
toTermName :: HasCategory leaf => Term leaf Info -> Text
|
|
|
|
toTermName term = case runCofree term of
|
|
|
|
(_ :< Leaf leaf) -> toCategoryName leaf
|
|
|
|
(_ :< Keyed children) -> mconcat $ keys children
|
|
|
|
(_ :< Indexed children) -> fromMaybe "EmptyIndexedNode" $ (toCategoryName . category) . extract <$> head children
|
|
|
|
(_ :< Fixed children) -> fromMaybe "EmptyFixedNode" $ (toCategoryName . category) . extract <$> head children
|
2016-06-09 22:50:36 +03:00
|
|
|
(_ :< Syntax.FunctionCall i _) -> toTermName i
|
2016-06-10 20:11:32 +03:00
|
|
|
(_ :< Syntax.Function _) -> "anonymous function"
|
2016-05-18 20:27:19 +03:00
|
|
|
|
2016-05-24 19:49:16 +03:00
|
|
|
class HasCategory a where
|
2016-06-06 22:45:44 +03:00
|
|
|
toCategoryName :: a -> Text
|
2016-05-18 00:34:27 +03:00
|
|
|
|
2016-05-24 19:49:16 +03:00
|
|
|
instance HasCategory Text where
|
2016-06-06 22:45:44 +03:00
|
|
|
toCategoryName = identity
|
2016-05-18 19:01:16 +03:00
|
|
|
|
2016-06-09 01:14:55 +03:00
|
|
|
instance HasCategory Info where
|
|
|
|
toCategoryName = toCategoryName . category
|
|
|
|
|
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-06-04 01:37:40 +03:00
|
|
|
Error -> "error"
|
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"
|
2016-06-09 00:18:30 +03:00
|
|
|
Category.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,
|
2016-06-08 21:46:38 +03:00
|
|
|
parentAnnotations :: [Category]
|
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-06-08 21:46:38 +03:00
|
|
|
showsPrec _ DiffSummary{..} s = (++s) . unpack $ case patch of
|
|
|
|
(Insert diffInfo) -> "Added the " <> "'" <> termName diffInfo <> "' " <> categoryName diffInfo <> maybeParentContext parentAnnotations
|
|
|
|
(Delete diffInfo) -> "Deleted the " <> "'" <> termName diffInfo <> "' " <> categoryName diffInfo <> maybeParentContext parentAnnotations
|
|
|
|
(Replace t1 t2) ->
|
|
|
|
"Replaced the " <> "'" <> termName t1 <> "' " <> categoryName t1
|
|
|
|
<> " with the " <> "'" <> termName t2 <> "' " <> categoryName t2
|
2016-06-06 22:45:44 +03:00
|
|
|
<> maybeParentContext parentAnnotations
|
2016-05-24 19:37:44 +03:00
|
|
|
where maybeParentContext parentAnnotations = if null parentAnnotations
|
|
|
|
then ""
|
2016-06-08 21:46:38 +03:00
|
|
|
else " in the " <> intercalate "/" (toCategoryName <$> 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-06-06 21:45:45 +03:00
|
|
|
diffSummary = cata $ \case
|
|
|
|
(Free (_ :< Leaf _)) -> [] -- Skip leaves since they don't have any changes
|
2016-06-08 21:46:38 +03:00
|
|
|
(Free (infos :< Indexed children)) -> prependSummary (category $ snd infos) <$> join children
|
|
|
|
(Free (infos :< Fixed children)) -> prependSummary (category $ snd infos) <$> join children
|
|
|
|
(Free (infos :< Keyed children)) -> prependSummary (category $ snd infos) <$> join (Prologue.toList children)
|
2016-06-10 20:27:20 +03:00
|
|
|
(Free (infos :< Syntax.FunctionCall identifier children)) -> prependSummary (category $ snd infos) <$> join (Prologue.toList (identifier : children))
|
|
|
|
(Free (infos :< Syntax.Function children)) -> prependSummary (category $ snd infos) <$> join (Prologue.toList children)
|
2016-06-08 22:18:44 +03:00
|
|
|
(Pure (Insert term)) -> (\info -> DiffSummary (Insert info) []) <$> termToDiffInfo term
|
|
|
|
(Pure (Delete term)) -> (\info -> DiffSummary (Delete info) []) <$> termToDiffInfo term
|
|
|
|
(Pure (Replace t1 t2)) -> (\(info1, info2) -> DiffSummary (Replace info1 info2) []) <$> zip (termToDiffInfo t1) (termToDiffInfo t2)
|
|
|
|
|
|
|
|
termToDiffInfo :: HasCategory leaf => Term leaf Info -> [DiffInfo]
|
|
|
|
termToDiffInfo term = case runCofree term of
|
|
|
|
(_ :< Leaf _) -> [ DiffInfo (toCategoryName term) (toTermName term) ]
|
|
|
|
(_ :< Indexed children) -> join $ termToDiffInfo <$> children
|
|
|
|
(_ :< Fixed children) -> join $ termToDiffInfo <$> children
|
|
|
|
(_ :< Keyed children) -> join $ termToDiffInfo <$> Prologue.toList children
|
2016-06-09 01:14:55 +03:00
|
|
|
(info :< Syntax.FunctionCall identifier _) -> [ DiffInfo (toCategoryName info) (toTermName identifier) ]
|
2016-05-16 17:54:05 +03:00
|
|
|
|
2016-06-08 21:46:38 +03:00
|
|
|
prependSummary :: Category -> DiffSummary DiffInfo -> DiffSummary DiffInfo
|
2016-05-16 17:54:05 +03:00
|
|
|
prependSummary annotation summary = summary { parentAnnotations = annotation : parentAnnotations summary }
|