diff --git a/src/DiffSummary.hs b/src/DiffSummary.hs index 887949fb3..500fee238 100644 --- a/src/DiffSummary.hs +++ b/src/DiffSummary.hs @@ -59,19 +59,21 @@ toTermName source term = case unwrap term of S.Switch expr _ -> toTermName' expr S.Ternary expr _ -> toTermName' expr S.MathAssignment id _ -> toTermName' id - S.Operator syntaxes -> mconcat $ toTermName' <$> syntaxes + S.Operator _ -> termNameFromSource term S.Object kvs -> "{" <> intercalate ", " (toTermName' <$> kvs) <> "}" S.Pair a b -> toTermName' a <> ": " <> toTermName' b S.Return expr -> maybe "empty" toTermName' expr - S.For exprs _ -> toText $ Source.slice (unionRangesFrom forRange forClauseRanges) source - where forRange = characterRange $ extract term - forClauseRanges = characterRange . extract <$> exprs + S.For exprs _ -> termNameFromChildren term exprs S.While expr _ -> toTermName' expr S.DoWhile _ expr -> toTermName' expr S.Class identifier _ _ -> toTermName' identifier S.Method identifier _ _ -> toTermName' identifier Comment a -> toCategoryName a where toTermName' = toTermName source + termNameFromChildren term cs = termNameFromRange (unionRangesFrom (range term) (range <$> cs)) + termNameFromSource term = termNameFromRange (range term) + termNameFromRange range = toText $ Source.slice range source + range = characterRange . extract class HasCategory a where toCategoryName :: a -> Text @@ -213,7 +215,6 @@ termToDiffInfo blob term = case unwrap term of -- 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. - S.Operator _ -> LeafInfo (toCategoryName term) "x" Commented cs leaf -> BranchInfo (termToDiffInfo' <$> cs <> maybeToList leaf) (toCategoryName term) BCommented S.Error sourceSpan _ -> ErrorInfo sourceSpan (toCategoryName term) _ -> LeafInfo (toCategoryName term) (toTermName' term) diff --git a/src/Parser.hs b/src/Parser.hs index 01cf22bfb..2dc79941a 100644 --- a/src/Parser.hs +++ b/src/Parser.hs @@ -17,13 +17,9 @@ import SourceSpan -- | and aren't pure. type Parser fields = SourceBlob -> IO (Term Text (Record fields)) --- | Categories that are treated as fixed nodes. -fixedCategories :: Set.Set Category -fixedCategories = Set.fromList [ BinaryOperator, Pair ] - --- | Should these categories be treated as fixed nodes? -isFixed :: Category -> Bool -isFixed = flip Set.member fixedCategories +-- | Whether a category is an Operator Category +isOperator :: Category -> Bool +isOperator = flip Set.member (Set.fromList [ Operator, BinaryOperator ]) -- | Given a function that maps production names to sets of categories, produce -- | a Constructor. @@ -49,7 +45,7 @@ termConstructor source sourceSpan info = cofree . construct construct children | SubscriptAccess == category info = case children of (base:element:[]) -> withDefaultInfo $ S.SubscriptAccess base element _ -> withDefaultInfo $ S.Error sourceSpan children - construct children | Operator == category info = withDefaultInfo $ S.Operator children + construct children | isOperator (category info) = withDefaultInfo $ S.Operator children construct children | Function == category info = case children of (body:[]) -> withDefaultInfo $ S.Function Nothing Nothing body (params:body:[]) | (info :< _) <- runCofree params, Params == category info -> @@ -92,7 +88,7 @@ termConstructor source sourceSpan info = cofree . construct toTuple child | S.Leaf c <- unwrap child = [cofree (extract child :< S.Comment c)] toTuple child = pure child - construct children | isFixed (category info) = withDefaultInfo $ S.Fixed children + construct children | Pair == (category info) = withDefaultInfo $ S.Fixed children construct children | C.Error == category info = withDefaultInfo $ S.Error sourceSpan children construct children | For == (category info), Just (exprs, body) <- unsnoc children = diff --git a/src/TreeSitter.hs b/src/TreeSitter.hs index 13b14a8f9..e3e69179b 100644 --- a/src/TreeSitter.hs +++ b/src/TreeSitter.hs @@ -30,17 +30,20 @@ treeSitterParser language grammar blob = do categoriesForLanguage :: Language -> Text -> Category categoriesForLanguage language name = case (language, name) of (JavaScript, "object") -> Object - (JavaScript, "rel_op") -> BinaryOperator -- relational operator, e.g. >, <, <=, >=, ==, != - (JavaScript, "bool_op") -> BinaryOperator (JavaScript, "expression_statement") -> ExpressionStatements (JavaScript, "this_expression") -> Identifier (JavaScript, "null") -> Identifier (JavaScript, "undefined") -> Identifier (JavaScript, "arrow_function") -> Function (JavaScript, "generator_function") -> Function - (JavaScript, "delete_op") -> Operator - (JavaScript, "type_op") -> Operator - (JavaScript, "void_op") -> Operator + (JavaScript, "math_op") -> BinaryOperator -- bitwise operator, e.g. +, -, *, /. + (JavaScript, "bool_op") -> BinaryOperator -- boolean operator, e.g. ||, &&. + (JavaScript, "bitwise_op") -> BinaryOperator -- bitwise operator, e.g. ^, &, etc. + (JavaScript, "rel_op") -> BinaryOperator -- relational operator, e.g. >, <, <=, >=, ==, !=. + (JavaScript, "comma_op") -> Operator -- comma operator, e.g. expr1, expr2. + (JavaScript, "delete_op") -> Operator -- delete operator, e.g. delete x[2]. + (JavaScript, "type_op") -> Operator -- type operator, e.g. typeof Object. + (JavaScript, "void_op") -> Operator -- void operator, e.g. void 2. (JavaScript, "for_in_statement") -> For (JavaScript, "for_of_statement") -> For (JavaScript, "class") -> Class