1
1
mirror of https://github.com/github/semantic.git synced 2024-12-23 14:54:16 +03:00
semantic/src/DiffSummary.hs

83 lines
3.8 KiB
Haskell
Raw Normal View History

{-# 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
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
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
data DiffInfo = DiffInfo { categoryName :: String, termName :: Maybe String } deriving (Eq, Show)
2016-05-18 17:18:26 +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)
(_ :< Indexed children) -> toCategoryName . category <$> head (extract <$> children)
(_ :< Fixed children) -> toCategoryName . category <$> head (extract <$> children)
2016-05-24 19:49:16 +03:00
class HasCategory a where
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
toCategoryName = unpack
2016-05-18 19:01:16 +03:00
2016-05-24 19:49:16 +03:00
instance HasCategory Category where
toCategoryName category = case category of
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"
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
toCategoryName = toCategoryName . category . extract
2016-05-24 19:49:16 +03:00
2016-05-17 20:09:14 +03:00
data DiffSummary a = DiffSummary {
patch :: Patch a,
parentAnnotations :: [a]
2016-05-17 20:09:14 +03:00
} deriving (Eq, Functor)
2016-05-13 18:44:03 +03:00
instance Show (DiffSummary DiffInfo) where
2016-05-26 21:04:27 +03:00
showsPrec _ DiffSummary{..} s = (++s) $ case patch of
(Insert termInfo) -> "Added the " ++ "'" ++ fromJust (termName termInfo) ++ "' " ++ categoryName termInfo
++ maybeParentContext parentAnnotations
(Delete termInfo) -> "Deleted the " ++ "'" ++ fromJust (termName termInfo) ++ "' " ++ categoryName termInfo
++ maybeParentContext parentAnnotations
(Replace t1 t2) -> "Replaced the " ++ "'" ++ fromJust (termName t1) ++ "' " ++ categoryName t1
++ " with the " ++ "'" ++ fromJust (termName t2) ++ "' " ++ categoryName t2
++ maybeParentContext parentAnnotations
where maybeParentContext parentAnnotations = if null parentAnnotations
then ""
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
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)
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 }