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

67 lines
2.7 KiB
Haskell
Raw Normal View History

2016-04-25 18:46:10 +03:00
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module DiffSummary where
import Diff
import Info
import Patch
import Term
import Syntax
import qualified Data.Text as T
import qualified Category
2016-05-03 22:50:38 +03:00
import Data.Functor.Both
2016-04-25 18:46:10 +03:00
import Data.Monoid
import Data.Maybe (listToMaybe)
import Data.Set (toList)
2016-05-03 22:50:38 +03:00
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
import qualified Control.Comonad.Cofree as Cofree
2016-05-03 19:17:38 +03:00
import Data.Functor.Foldable as Foldable
2016-04-25 18:46:10 +03:00
newtype DiffSummary = DiffSummary { diffChanges :: [Patch DiffEntry] }
2016-04-25 18:46:10 +03:00
deriving (Monoid)
emptyDiffSummary :: DiffSummary
emptyDiffSummary = DiffSummary { diffChanges = [] }
newtype DiffEntry = DiffEntry { termName :: String }
2016-04-25 18:46:10 +03:00
patchSummary :: (Term a Info -> DiffSummary) -> Patch (Term a Info) -> DiffSummary
patchSummary termSummary patch = memptyOrDiff (before patch) <> memptyOrDiff (after patch)
2016-04-25 18:46:10 +03:00
where
memptyOrDiff = maybe emptyDiffSummary termSummary
2016-04-25 18:46:10 +03:00
2016-05-03 22:50:38 +03:00
type DiffSummaryF leaf annotation = FreeF (CofreeF (Syntax leaf) (Both annotation))
diffSummary :: Diff leaf Info -> DiffSummary
-- histo :: Foldable t => (Base t (Cofree (Base t) a) -> a) -> t -> a
diffSummary = histo diffSummary' . fmap (patchSummary undefined) where
--diffSummary' :: DiffF leaf (Cofree.Cofree (DiffF leaf Info) DiffSummary) f -> DiffSummary
-- Skip any child that doesn't have any changes (that will always include leaves)
-- Skip any child that doesn't have any changes (that will always include leaves)
diffSummary' :: DiffSummaryF leaf annotation DiffSummary (Cofree.Cofree (DiffSummaryF leaf annotation DiffSummary) DiffSummary) -> DiffSummary
diffSummary' (Free (_ :< Leaf _)) = undefined
diffSummary' (Free (_ :< Indexed children)) = DiffSummary { diffChanges = children >>= diffChanges . extract }
diffSummary' (Free (_ :< Fixed children)) = undefined
diffSummary' (Free (_ :< Keyed children)) = undefined
diffSummary' (Pure diffSummary) = diffSummary :: DiffSummary
2016-05-03 19:17:38 +03:00
-- (patchSummary termSummary)
2016-04-25 18:46:10 +03:00
-- Syntax Text DiffSummary -> DiffSummary Text
2016-05-03 19:17:38 +03:00
-- If termSummary returns a DiffEntry that just contains the term name, we need to
-- Instead of foldMap we need a histomorphism
termSummary :: Term T.Text Info -> T.Text
2016-05-03 19:17:38 +03:00
termSummary = Foldable.cata summary where
summary :: TermF T.Text Info f -> T.Text
summary (info :< Leaf replace) = replace
summary (info :< Indexed children) = toCategory info
summary (info :< Fixed children) = toCategory info
summary (info :< Keyed _) = toCategory info
toCategory :: Info -> T.Text
2016-05-03 19:17:38 +03:00
toCategory term = T.pack $ case maybeFirstCategory term of
Just category -> show category
Nothing -> "Unknown"
2016-05-03 19:17:38 +03:00
maybeFirstCategory term = listToMaybe . toList $ Category.categories term