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

95 lines
4.2 KiB
Haskell
Raw Normal View History

2016-05-18 17:18:26 +03:00
{-# LANGUAGE DataKinds, TypeFamilies, ScopedTypeVariables, FlexibleInstances, RecordWildCards #-}
2016-05-17 20:09:14 +03:00
module DiffSummary (DiffSummary(..), diffSummary, DiffInfo(..)) where
2016-04-25 18:46:10 +03:00
import Prelude hiding (fst, snd)
2016-04-25 18:46:10 +03:00
import Diff
import Info
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
import Control.Comonad
2016-05-03 19:17:38 +03:00
import Control.Comonad.Trans.Cofree
2016-05-03 22:50:38 +03:00
import Control.Monad.Trans.Free
2016-05-16 20:19:30 +03:00
import Control.Monad
2016-05-18 17:18:26 +03:00
import Data.Maybe
2016-05-18 00:34:27 +03:00
import Data.List
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-16 17:54:05 +03:00
import qualified Data.Foldable as F
2016-05-18 19:01:16 +03:00
import Data.Text as Text (unpack, Text)
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) -> Just (toCategoryName . toCategory . head $ extract <$> children)
(_ :< Fixed children) -> Just (toCategoryName . toCategory . 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
toCategoryName = id
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
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-05-18 00:34:27 +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
toCategoryName term = toCategoryName $ case runCofree term of
(info :< Leaf _) -> toCategory info
(info :< Indexed _) -> toCategory info
(info :< Fixed _) -> toCategory info
(info :< Keyed _) -> toCategory info
2016-05-24 19:49:16 +03:00
2016-05-17 20:09:14 +03:00
data DiffSummary a = DiffSummary {
patch :: Patch DiffInfo,
2016-05-18 00:34:27 +03:00
parentAnnotations :: [DiffInfo]
2016-05-17 20:09:14 +03:00
} deriving (Eq, Functor)
2016-05-13 18:44:03 +03:00
2016-05-17 20:09:14 +03:00
instance Show a => Show (DiffSummary a) where
2016-05-18 17:18:26 +03:00
show DiffSummary{..} = 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 . toCategory $ snd infos) Nothing) <$> join children
diffSummary' (Free (infos :< Fixed children)) = prependSummary (DiffInfo (toCategoryName . toCategory $ snd infos) Nothing) <$> join children
diffSummary' (Free (infos :< Keyed children)) = prependSummary (DiffInfo (toCategoryName . toCategory $ snd infos) Nothing) <$> join (F.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 }
2016-05-17 22:59:07 +03:00
toCategory :: Info -> Category
toCategory info = fromMaybe (Other "Unknown") (maybeFirstCategory info)