2016-06-03 23:02:06 +03:00
|
|
|
{-# LANGUAGE DataKinds, TypeFamilies, ScopedTypeVariables #-}
|
2016-06-07 02:41:07 +03:00
|
|
|
|
2016-07-16 00:06:41 +03:00
|
|
|
module DiffSummary (DiffSummary(..), diffSummary, DiffInfo(..), annotatedSummaries) where
|
2016-04-25 18:46:10 +03:00
|
|
|
|
2016-06-28 23:38:06 +03:00
|
|
|
import Prologue hiding (snd, intercalate)
|
2016-04-25 18:46:10 +03:00
|
|
|
import Diff
|
|
|
|
import Patch
|
2016-05-17 22:59:07 +03:00
|
|
|
import Term
|
2016-07-15 02:35:23 +03:00
|
|
|
import Info (category, Cost)
|
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-07-13 18:58:43 +03:00
|
|
|
import Data.Text as Text (intercalate)
|
2016-07-13 21:32:53 +03:00
|
|
|
import Test.QuickCheck hiding (Fixed)
|
|
|
|
import Patch.Arbitrary()
|
2016-07-14 18:52:03 +03:00
|
|
|
import Data.Record
|
2016-07-16 00:06:41 +03:00
|
|
|
import Text.PrettyPrint.Leijen.Text ((<+>), squotes, space, string, Doc, punctuate, pretty)
|
2016-07-13 18:58:43 +03:00
|
|
|
import qualified Text.PrettyPrint.Leijen.Text as P
|
2016-07-15 02:35:23 +03:00
|
|
|
import Data.Hashable
|
2016-04-25 18:46:10 +03:00
|
|
|
|
2016-07-15 20:18:45 +03:00
|
|
|
data DiffInfo = LeafInfo { categoryName :: Text, termName :: Text }
|
|
|
|
| BranchInfo { branches :: [ DiffInfo ], categoryName :: Text, branchType :: Branch }
|
|
|
|
deriving (Eq, Show)
|
2016-05-18 17:18:26 +03:00
|
|
|
|
2016-07-14 18:52:03 +03:00
|
|
|
toTermName :: (HasCategory leaf, HasField fields Category) => Term leaf (Record fields) -> Text
|
2016-06-14 23:50:34 +03:00
|
|
|
toTermName term = case unwrap term of
|
2016-07-15 02:35:23 +03:00
|
|
|
Syntax.Fixed children -> fromMaybe "branch" $ (toCategoryName . category) . extract <$> head children
|
|
|
|
Syntax.Indexed children -> fromMaybe "branch" $ (toCategoryName . category) . extract <$> head children
|
2016-06-15 18:06:13 +03:00
|
|
|
Leaf leaf -> toCategoryName leaf
|
2016-06-14 23:50:34 +03:00
|
|
|
Syntax.Assignment identifier value -> toTermName identifier <> toTermName value
|
2016-06-15 18:06:13 +03:00
|
|
|
Syntax.Function identifier _ _ -> (maybe "anonymous" toTermName identifier)
|
|
|
|
Syntax.FunctionCall i _ -> toTermName i
|
2016-06-14 23:50:34 +03:00
|
|
|
Syntax.MemberAccess base property -> case (unwrap base, unwrap property) of
|
2016-06-14 01:34:35 +03:00
|
|
|
(Syntax.FunctionCall{}, Syntax.FunctionCall{}) -> toTermName base <> "()." <> toTermName property <> "()"
|
|
|
|
(Syntax.FunctionCall{}, _) -> toTermName base <> "()." <> toTermName property
|
|
|
|
(_, Syntax.FunctionCall{}) -> toTermName base <> "." <> toTermName property <> "()"
|
|
|
|
(_, _) -> toTermName base <> "." <> toTermName property
|
2016-06-15 00:04:27 +03:00
|
|
|
Syntax.MethodCall targetId methodId _ -> toTermName targetId <> sep <> toTermName methodId <> "()"
|
|
|
|
where sep = case unwrap targetId of
|
|
|
|
Syntax.FunctionCall{} -> "()."
|
|
|
|
_ -> "."
|
2016-06-17 22:40:33 +03:00
|
|
|
Syntax.SubscriptAccess base element -> case (unwrap base, unwrap element) of
|
|
|
|
(Syntax.FunctionCall{}, Syntax.FunctionCall{}) -> toTermName base <> "()." <> toTermName element <> "()"
|
|
|
|
(Syntax.FunctionCall{}, _) -> toTermName base <> "()." <> toTermName element
|
|
|
|
(_, Syntax.FunctionCall{}) -> toTermName base <> "[" <> toTermName element <> "()" <> "]"
|
|
|
|
(_, _) -> toTermName base <> "[" <> toTermName element <> "]"
|
2016-06-15 03:47:51 +03:00
|
|
|
Syntax.VarAssignment varId _ -> toTermName varId
|
2016-06-15 18:06:13 +03:00
|
|
|
Syntax.VarDecl decl -> toTermName decl
|
2016-06-16 01:51:17 +03:00
|
|
|
-- TODO: We should remove Args from Syntax since I don't think we should ever
|
2016-06-15 21:15:09 +03:00
|
|
|
-- evaluate Args as a single toTermName Text - joshvera
|
|
|
|
Syntax.Args args -> mconcat $ toTermName <$> args
|
2016-06-16 01:51:17 +03:00
|
|
|
-- TODO: We should remove Case from Syntax since I don't think we should ever
|
|
|
|
-- evaluate Case as a single toTermName Text - joshvera
|
|
|
|
Syntax.Case expr _ -> toTermName expr
|
2016-06-16 01:57:55 +03:00
|
|
|
Syntax.Switch expr _ -> toTermName expr
|
2016-06-22 01:19:52 +03:00
|
|
|
Syntax.Ternary expr _ -> toTermName expr
|
|
|
|
Syntax.MathAssignment id _ -> toTermName id
|
|
|
|
Syntax.Operator syntaxes -> mconcat $ toTermName <$> syntaxes
|
2016-07-11 19:55:32 +03:00
|
|
|
Syntax.Object kvs -> "{" <> intercalate ", " (toTermName <$> kvs) <> "}"
|
|
|
|
Syntax.Pair a b -> toTermName a <> ": " <> toTermName b
|
|
|
|
Comment a -> toCategoryName a
|
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-07-14 18:52:03 +03:00
|
|
|
newtype Categorizable a = Categorizable a
|
|
|
|
instance (HasField fields Category) => HasCategory (Categorizable (Record fields)) where
|
|
|
|
toCategoryName (Categorizable a)= toCategoryName $ category a
|
2016-06-09 01:14:55 +03:00
|
|
|
|
2016-05-24 19:49:16 +03:00
|
|
|
instance HasCategory Category where
|
2016-06-10 22:24:37 +03:00
|
|
|
toCategoryName = \case
|
2016-06-15 18:06:13 +03:00
|
|
|
ArrayLiteral -> "array"
|
2016-05-18 00:34:27 +03:00
|
|
|
BinaryOperator -> "binary operator"
|
2016-06-16 17:54:20 +03:00
|
|
|
Boolean -> "boolean"
|
2016-05-18 20:37:02 +03:00
|
|
|
DictionaryLiteral -> "dictionary"
|
2016-06-15 18:06:13 +03:00
|
|
|
Error -> "error"
|
2016-06-10 22:24:37 +03:00
|
|
|
ExpressionStatements -> "expression statements"
|
2016-06-14 00:32:00 +03:00
|
|
|
Category.Assignment -> "assignment"
|
2016-06-15 18:06:13 +03:00
|
|
|
Category.Function -> "function"
|
|
|
|
Category.FunctionCall -> "function call"
|
2016-06-14 01:34:07 +03:00
|
|
|
Category.MemberAccess -> "member access"
|
2016-06-15 18:06:13 +03:00
|
|
|
Category.MethodCall -> "method call"
|
2016-06-15 21:15:29 +03:00
|
|
|
Category.Args -> "arguments"
|
|
|
|
Category.VarAssignment -> "var assignment"
|
2016-06-16 01:09:44 +03:00
|
|
|
Category.VarDecl -> "variable"
|
2016-06-16 02:51:48 +03:00
|
|
|
Category.Switch -> "switch statement"
|
|
|
|
Category.Case -> "case statement"
|
2016-06-17 22:40:33 +03:00
|
|
|
Category.SubscriptAccess -> "subscript access"
|
2016-06-17 22:57:51 +03:00
|
|
|
Category.MathAssignment -> "math assignment"
|
2016-06-17 23:33:22 +03:00
|
|
|
Category.Ternary -> "ternary"
|
2016-06-18 01:03:55 +03:00
|
|
|
Category.Operator -> "operator"
|
2016-06-15 18:06:13 +03:00
|
|
|
Identifier -> "identifier"
|
|
|
|
IntegerLiteral -> "integer"
|
2016-06-04 01:34:42 +03:00
|
|
|
Other s -> s
|
2016-07-08 18:17:46 +03:00
|
|
|
Category.Pair -> "pair"
|
2016-06-15 18:06:13 +03:00
|
|
|
Params -> "params"
|
|
|
|
Program -> "top level"
|
2016-06-15 19:09:52 +03:00
|
|
|
Regex -> "regex"
|
2016-06-15 18:06:13 +03:00
|
|
|
StringLiteral -> "string"
|
|
|
|
SymbolLiteral -> "symbol"
|
2016-06-15 18:39:18 +03:00
|
|
|
TemplateString -> "template string"
|
2016-06-28 23:38:06 +03:00
|
|
|
Category.Object -> "object"
|
2016-05-17 20:09:14 +03:00
|
|
|
|
2016-07-14 18:52:03 +03:00
|
|
|
instance (HasCategory leaf, HasField fields Category) => HasCategory (Term leaf (Record fields)) where
|
2016-05-31 23:13:01 +03:00
|
|
|
toCategoryName = toCategoryName . category . extract
|
2016-05-24 19:49:16 +03:00
|
|
|
|
2016-07-15 20:18:45 +03:00
|
|
|
data Branch = BIndexed | BFixed | BCommented deriving (Show, Eq, Generic)
|
2016-07-15 02:35:23 +03:00
|
|
|
instance Arbitrary Branch where
|
2016-07-15 20:18:45 +03:00
|
|
|
arbitrary = oneof [ pure BIndexed, pure BFixed ]
|
2016-07-15 02:35:23 +03:00
|
|
|
shrink = genericShrink
|
|
|
|
|
2016-05-17 20:09:14 +03:00
|
|
|
data DiffSummary a = DiffSummary {
|
2016-05-31 23:11:11 +03:00
|
|
|
patch :: Patch a,
|
2016-07-15 20:18:45 +03:00
|
|
|
parentAnnotations :: [Category]
|
2016-07-13 21:32:53 +03:00
|
|
|
} deriving (Eq, Functor, Show, Generic)
|
|
|
|
|
|
|
|
instance (Eq a, Arbitrary a) => Arbitrary (DiffSummary a) where
|
2016-07-15 20:18:45 +03:00
|
|
|
arbitrary = DiffSummary <$> arbitrary <*> arbitrary
|
2016-07-13 21:32:53 +03:00
|
|
|
shrink = genericShrink
|
2016-05-13 18:44:03 +03:00
|
|
|
|
2016-07-16 00:06:41 +03:00
|
|
|
instance P.Pretty DiffInfo where
|
|
|
|
pretty LeafInfo{..} = squotes (string $ toSL termName) <+> (string $ toSL categoryName)
|
|
|
|
pretty BranchInfo{..} = mconcat $ punctuate (string "," <> space) (pretty <$> branches)
|
|
|
|
|
|
|
|
annotatedSummaries :: DiffSummary DiffInfo -> [Text]
|
|
|
|
annotatedSummaries DiffSummary{..} = show . (P.<> maybeParentContext parentAnnotations) <$> summaries patch
|
|
|
|
|
|
|
|
summaries :: Patch DiffInfo -> [P.Doc]
|
|
|
|
summaries (Insert info) = (("Added" <+> "the") <+>) <$> toLeafInfos info
|
|
|
|
summaries (Delete info) = (("Deleted" <+> "the") <+>) <$> toLeafInfos info
|
|
|
|
summaries (Replace i1 i2) = zipWith (\a b -> "Replaced" <+> "the" <+> a <+> "with the" <+> b) (toLeafInfos i1) (toLeafInfos i2)
|
|
|
|
|
|
|
|
toLeafInfos :: DiffInfo -> [Doc]
|
|
|
|
toLeafInfos LeafInfo{..} = [ squotes (toDoc termName) <+> (toDoc categoryName) ]
|
|
|
|
toLeafInfos BranchInfo{..} = pretty <$> branches
|
|
|
|
|
|
|
|
maybeParentContext :: [Category] -> Doc
|
|
|
|
maybeParentContext annotations = if null annotations
|
|
|
|
then ""
|
|
|
|
else space <> "in the" <+> (toDoc . intercalate "/" $ toCategoryName <$> annotations) <+> "context"
|
|
|
|
toDoc :: Text -> Doc
|
|
|
|
toDoc = string . toS
|
2016-04-25 18:46:10 +03:00
|
|
|
|
2016-07-15 21:10:31 +03:00
|
|
|
diffSummary :: (HasCategory leaf, HasField fields Category) => Diff leaf (Record fields) -> [DiffSummary DiffInfo]
|
2016-06-06 21:45:45 +03:00
|
|
|
diffSummary = cata $ \case
|
2016-07-11 19:56:00 +03:00
|
|
|
-- Skip comments and leaves since they don't have any changes
|
|
|
|
(Free (_ :< Leaf _)) -> []
|
|
|
|
Free (_ :< (Syntax.Comment _)) -> []
|
2016-07-15 02:35:23 +03:00
|
|
|
(Free (infos :< Syntax.Indexed children)) -> prependSummary (category $ snd infos) <$> join children
|
|
|
|
(Free (infos :< Syntax.Fixed children)) -> prependSummary (category $ snd infos) <$> join children
|
2016-06-10 20:27:20 +03:00
|
|
|
(Free (infos :< Syntax.FunctionCall identifier children)) -> prependSummary (category $ snd infos) <$> join (Prologue.toList (identifier : children))
|
2016-06-10 22:10:37 +03:00
|
|
|
(Free (infos :< Syntax.Function id ps body)) -> prependSummary (category $ snd infos) <$> (fromMaybe [] id) <> (fromMaybe [] ps) <> body
|
2016-06-14 00:32:00 +03:00
|
|
|
(Free (infos :< Syntax.Assignment id value)) -> prependSummary (category $ snd infos) <$> id <> value
|
2016-06-14 01:34:07 +03:00
|
|
|
(Free (infos :< Syntax.MemberAccess base property)) -> prependSummary (category $ snd infos) <$> base <> property
|
2016-06-17 22:40:33 +03:00
|
|
|
(Free (infos :< Syntax.SubscriptAccess base property)) -> prependSummary (category $ snd infos) <$> base <> property
|
2016-06-13 00:26:31 +03:00
|
|
|
(Free (infos :< Syntax.MethodCall targetId methodId ps)) -> prependSummary (category $ snd infos) <$> targetId <> methodId <> ps
|
2016-06-15 21:15:46 +03:00
|
|
|
(Free (infos :< Syntax.VarAssignment varId value)) -> prependSummary (category $ snd infos) <$> varId <> value
|
|
|
|
(Free (infos :< Syntax.VarDecl decl)) -> prependSummary (category $ snd infos) <$> decl
|
|
|
|
(Free (infos :< Syntax.Args args)) -> prependSummary (category $ snd infos) <$> join args
|
2016-06-16 02:52:05 +03:00
|
|
|
(Free (infos :< Syntax.Switch expr cases)) -> prependSummary (category $ snd infos) <$> expr <> join cases
|
|
|
|
(Free (infos :< Syntax.Case expr body)) -> prependSummary (category $ snd infos) <$> expr <> body
|
2016-06-22 01:19:52 +03:00
|
|
|
Free (infos :< (Syntax.Ternary expr cases)) -> prependSummary (category $ snd infos) <$> expr <> join cases
|
|
|
|
Free (infos :< (Syntax.MathAssignment id value)) -> prependSummary (category $ snd infos) <$> id <> value
|
|
|
|
Free (infos :< (Syntax.Operator syntaxes)) -> prependSummary (category $ snd infos) <$> join syntaxes
|
2016-07-11 19:56:00 +03:00
|
|
|
Free (infos :< (Syntax.Object kvs)) -> prependSummary (category $ snd infos) <$> join kvs
|
|
|
|
Free (infos :< (Syntax.Pair a b)) -> prependSummary (category $ snd infos) <$> a <> b
|
|
|
|
Free (infos :< (Syntax.Commented cs leaf)) -> prependSummary (category $ snd infos) <$> join cs <> fromMaybe [] leaf
|
2016-07-15 20:18:45 +03:00
|
|
|
(Pure (Insert term)) -> [ DiffSummary (Insert $ termToDiffInfo term) [] ]
|
|
|
|
(Pure (Delete term)) -> [ DiffSummary (Delete $ termToDiffInfo term) [] ]
|
|
|
|
(Pure (Replace t1 t2)) -> [ DiffSummary (Replace (termToDiffInfo t1) (termToDiffInfo t2)) [] ]
|
|
|
|
|
|
|
|
termToDiffInfo :: (HasCategory leaf, HasField fields Category) => Term leaf (Record fields) -> DiffInfo
|
2016-07-22 21:20:03 +03:00
|
|
|
termToDiffInfo term = case unwrap term of
|
2016-07-22 21:39:54 +03:00
|
|
|
Leaf _ -> LeafInfo (toCategoryName term) (toTermName term)
|
|
|
|
Syntax.Indexed children -> BranchInfo (termToDiffInfo <$> children) (toCategoryName term) BIndexed
|
|
|
|
Syntax.Fixed children -> BranchInfo (termToDiffInfo <$> children) (toCategoryName term) BFixed
|
|
|
|
Syntax.FunctionCall identifier _ -> LeafInfo (toCategoryName term) (toTermName identifier)
|
|
|
|
Syntax.Ternary ternaryCondition _ -> LeafInfo (toCategoryName term) (toTermName ternaryCondition)
|
|
|
|
Syntax.Function identifier _ _ -> LeafInfo (toCategoryName term) (maybe "anonymous" toTermName identifier)
|
|
|
|
Syntax.Assignment identifier _ -> LeafInfo (toCategoryName term) (toTermName identifier)
|
|
|
|
Syntax.MathAssignment identifier _ -> LeafInfo (toCategoryName term) (toTermName identifier)
|
2016-06-18 01:03:55 +03:00
|
|
|
-- Currently we cannot express the operator for an operator production from TreeSitter. Eventually we should be able to
|
|
|
|
-- use the term name of the operator identifier when we have that production value. Until then, I'm using a placeholder value
|
|
|
|
-- to indicate where that value should be when constructing DiffInfos.
|
2016-07-22 21:39:54 +03:00
|
|
|
Syntax.Operator _ -> LeafInfo (toCategoryName term) "x"
|
|
|
|
Commented cs leaf -> BranchInfo (termToDiffInfo <$> cs <> maybeToList leaf) (toCategoryName term) BCommented
|
|
|
|
_ -> LeafInfo (toCategoryName term) (toTermName term)
|
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 }
|