From aa26c5472edd9d4f095e3923b81736aaab7897d2 Mon Sep 17 00:00:00 2001 From: Timothy Clem Date: Wed, 7 Dec 2016 10:45:55 -0800 Subject: [PATCH] Properly parse classes (with new tree-sitter trees) --- src/Category.hs | 6 ++++++ src/DiffSummary.hs | 2 ++ src/Language/Ruby.hs | 10 +++++++--- src/Renderer/Split.hs | 2 ++ vendor/tree-sitter-parsers | 2 +- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Category.hs b/src/Category.hs index 7d8843f89..6d1c3aa7f 100644 --- a/src/Category.hs +++ b/src/Category.hs @@ -167,6 +167,10 @@ data Category | Binary -- | A unary statement, e.g. !a in Ruby. | Unary + -- | A constant, e.g `Foo::Bar` in Ruby. + | Constant + -- | A superclass, e.g `< Foo` in Ruby. + | Superclass deriving (Eq, Generic, Ord, Show) -- Instances @@ -256,6 +260,8 @@ instance Arbitrary Category where , pure Continue , pure Binary , pure Unary + , pure Constant + , pure Superclass , Other <$> arbitrary ] diff --git a/src/DiffSummary.hs b/src/DiffSummary.hs index d561a5d93..6b940c984 100644 --- a/src/DiffSummary.hs +++ b/src/DiffSummary.hs @@ -428,6 +428,8 @@ instance HasCategory Category where C.Continue -> "continue statement" C.Binary -> "binary statement" C.Unary -> "unary statement" + C.Constant -> "constant" + C.Superclass -> "superclass" instance HasField fields Category => HasCategory (SyntaxTerm leaf fields) where toCategoryName = toCategoryName . category . extract diff --git a/src/Language/Ruby.hs b/src/Language/Ruby.hs index c0a2d8164..b6c488de6 100644 --- a/src/Language/Ruby.hs +++ b/src/Language/Ruby.hs @@ -71,12 +71,14 @@ termConstructor source sourceSpan name range children allChildren ("case", _ ) -> S.Error children ("when", condition : body ) -> S.Case condition body ("when", _ ) -> S.Error children - ("class", [ identifier, superclass, definitions ]) -> S.Class identifier (Just superclass) (toList (unwrap definitions)) - ("class", [ identifier, definitions ]) -> S.Class identifier Nothing (toList (unwrap definitions)) + ("class", constant : rest ) -> case rest of + ( superclass : body ) | Superclass <- category (extract superclass) -> S.Class constant (Just superclass) body + _ -> S.Class constant Nothing rest ("class", _ ) -> S.Error children ("comment", _ ) -> S.Comment . toText $ slice range source ("conditional", condition : cases) -> S.Ternary condition cases ("conditional", _ ) -> S.Error children + ("constant", _ ) -> S.Fixed children ("method_call", _ ) -> case children of member : args | MemberAccess <- category (extract member) -> case toList (unwrap member) of [target, method] -> S.MethodCall target method (toList . unwrap =<< args) @@ -106,7 +108,7 @@ termConstructor source sourceSpan name range children allChildren identifier : params : body | Params <- category (extract params) -> S.Method identifier (toList (unwrap params)) body identifier : body -> S.Method identifier [] body _ -> S.Error children - ("module", identifier : body ) -> S.Module identifier body + ("module", constant : body ) -> S.Module constant body ("module", _ ) -> S.Error children ("rescue", _ ) -> case children of exceptions : exceptionVar : rest @@ -149,6 +151,7 @@ categoryForRubyName = \case "class" -> Class "comment" -> Comment "conditional" -> Ternary + "constant" -> Constant "element_reference" -> SubscriptAccess "else" -> Else "elsif" -> Elsif @@ -183,6 +186,7 @@ categoryForRubyName = \case "splat_parameter" -> SplatParameter "string" -> StringLiteral "subshell" -> Subshell + "superclass" -> Superclass "symbol" -> SymbolLiteral "true" -> Boolean "unary" -> Unary diff --git a/src/Renderer/Split.hs b/src/Renderer/Split.hs index ee059cd33..dc58721bd 100644 --- a/src/Renderer/Split.hs +++ b/src/Renderer/Split.hs @@ -116,6 +116,8 @@ styleName category = "category-" <> case category of C.Continue -> "continue_statement" C.Binary -> "binary" C.Unary -> "unary" + C.Constant -> "constant" + C.Superclass -> "superclass" -- | Pick the class name for a split patch. splitPatchToClassName :: SplitPatch a -> AttributeValue diff --git a/vendor/tree-sitter-parsers b/vendor/tree-sitter-parsers index 1709d9799..831c0f3a3 160000 --- a/vendor/tree-sitter-parsers +++ b/vendor/tree-sitter-parsers @@ -1 +1 @@ -Subproject commit 1709d9799d4e05799fd9ae1d9ee22bf8f0214c79 +Subproject commit 831c0f3a302e1bfc6439bb7f6be5ea557aeec03a