1
1
mirror of https://github.com/github/semantic.git synced 2024-12-01 09:15:01 +03:00

Better begin and method declaration parsing and summaries

This commit is contained in:
Timothy Clem 2016-11-03 14:24:05 -07:00
parent 947a7f1602
commit 769413813c
10 changed files with 752 additions and 7 deletions

View File

@ -228,7 +228,7 @@ toTermName source term = case unwrap term of
S.Try expr _ _ -> termNameFromSource expr
S.Array _ -> termNameFromSource term
S.Class identifier _ _ -> toTermName' identifier
S.Method identifier _ _ -> toTermName' identifier
S.Method identifier args _ -> toTermName' identifier <> paramsToArgNames args
S.Comment a -> toCategoryName a
S.Commented _ _ -> termNameFromChildren term (toList $ unwrap term)
S.Module identifier _ -> toTermName' identifier
@ -249,6 +249,7 @@ toTermName source term = case unwrap term of
_ -> ""
S.RescueModifier _ rhs -> termNameFromSource rhs
S.LastException expr -> termNameFromSource expr
S.Params args -> mconcat $ toTermName' <$> args
where toTermName' = toTermName source
termNameFromChildren term children = termNameFromRange (unionRangesFrom (range term) (range <$> children))
termNameFromSource term = termNameFromRange (range term)
@ -355,7 +356,7 @@ instance HasCategory Category where
NumberLiteral -> "number"
Other s -> s
C.Pair -> "pair"
Params -> "params"
C.Params -> "params"
Program -> "top level"
Regex -> "regex"
StringLiteral -> "string"

View File

@ -43,6 +43,7 @@ termConstructor source sourceSpan name range children
("conditional_assignment", _ ) -> S.Error children
("conditional", condition : cases) -> S.Ternary condition cases
("conditional", _ ) -> S.Error children
("formal_parameters", _) -> S.Params children
("function_call", _) -> case runCofree <$> children of
[ _ :< S.MemberAccess{..}, _ :< S.Args args ] -> S.MethodCall memberId property args
[ _ :< S.MemberAccess{..} ] -> S.MethodCall memberId property []
@ -64,9 +65,12 @@ termConstructor source sourceSpan name range children
("math_assignment", _ ) -> S.Error children
("member_access", [ base, property ]) -> S.MemberAccess base property
("member_access", _ ) -> S.Error children
("method_declaration", [ identifier, params, exprs ]) -> S.Method identifier (toList (unwrap params)) (toList (unwrap exprs))
("method_declaration", [ identifier, exprs ]) -> S.Method identifier [] (toList (unwrap exprs))
("method_declaration", _ ) -> S.Error children
("method_declaration", _) -> case runCofree <$> children of
[ identifier, params@(_ :< S.Params _) ] -> S.Method (cofree identifier) (toList params) []
[ identifier ] -> S.Method (cofree identifier) [] []
( identifier : params@(_ :< S.Params _ ) : exprs) -> S.Method (cofree identifier) (toList params) (cofree <$> exprs)
( identifier : exprs ) -> S.Method (cofree identifier) [] (cofree <$> exprs)
_ -> S.Error children
("module_declaration", identifier : body ) -> S.Module identifier body
("module_declaration", _ ) -> S.Error children
("rescue_block", _) -> case runCofree <$> children of

View File

@ -57,7 +57,7 @@ styleName category = "category-" <> case category of
TemplateString -> "template_string"
Regex -> "regex"
Identifier -> "identifier"
Params -> "parameters"
C.Params -> "parameters"
ExpressionStatements -> "expression_statements"
C.MathAssignment -> "math_assignment"
C.SubscriptAccess -> "subscript_access"

View File

@ -92,6 +92,8 @@ data Syntax a f
| RescueModifier f f
-- | The last exception captured in a rescue block to a local variable (e.g. in Ruby rescue => x).
| LastException f
-- | Parameters in a method/function definition
| Params [f]
deriving (Eq, Foldable, Functor, Generic, Generic1, Mergeable, Ord, Show, Traversable, ToJSON)

View File

@ -0,0 +1,150 @@
[{
"testCaseDescription": "ruby-begin-setup-test",
"expectedResult": {
"changes": {
"begin.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
2,
4
]
}
},
"summary": "Added the 'foo' method"
}
]
},
"errors": {}
},
"filePaths": [
"begin.rb"
],
"sha1": "96b7131c6fa55f1ebaa280af12a41a99741fd54b",
"gitDir": "test/corpus/repos/ruby",
"sha2": "030c90377373b9b48cc12118c676fa8a83b3e2b7"
}
,{
"testCaseDescription": "ruby-begin-insert-test",
"expectedResult": {
"changes": {},
"errors": {}
},
"filePaths": [
"begin.rb"
],
"sha1": "030c90377373b9b48cc12118c676fa8a83b3e2b7",
"gitDir": "test/corpus/repos/ruby",
"sha2": "ef1e4587d09eb124f6e32e40b1403d5055b62dd2"
}
,{
"testCaseDescription": "ruby-begin-replacement-test",
"expectedResult": {
"changes": {
"begin.rb": [
{
"span": {
"insert": {
"start": [
3,
3
],
"end": [
3,
12
]
}
},
"summary": "Added the 'puts('hi')' function call in the foo method"
}
]
},
"errors": {}
},
"filePaths": [
"begin.rb"
],
"sha1": "ef1e4587d09eb124f6e32e40b1403d5055b62dd2",
"gitDir": "test/corpus/repos/ruby",
"sha2": "ed584aace513a8f89e799c0b90da108e3261e28c"
}
,{
"testCaseDescription": "ruby-begin-delete-replacement-test",
"expectedResult": {
"changes": {
"begin.rb": [
{
"span": {
"delete": {
"start": [
3,
3
],
"end": [
3,
12
]
}
},
"summary": "Deleted the 'puts('hi')' function call in the foo method"
}
]
},
"errors": {}
},
"filePaths": [
"begin.rb"
],
"sha1": "ed584aace513a8f89e799c0b90da108e3261e28c",
"gitDir": "test/corpus/repos/ruby",
"sha2": "ad0da4d00535256004bb73da0111e84c828a58d6"
}
,{
"testCaseDescription": "ruby-begin-delete-insert-test",
"expectedResult": {
"changes": {},
"errors": {}
},
"filePaths": [
"begin.rb"
],
"sha1": "ad0da4d00535256004bb73da0111e84c828a58d6",
"gitDir": "test/corpus/repos/ruby",
"sha2": "214074e2e71e4f70e0357eb7eb371a7f7f8be320"
}
,{
"testCaseDescription": "ruby-begin-teardown-test",
"expectedResult": {
"changes": {
"begin.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
2,
4
]
}
},
"summary": "Deleted the 'foo' method"
}
]
},
"errors": {}
},
"filePaths": [
"begin.rb"
],
"sha1": "214074e2e71e4f70e0357eb7eb371a7f7f8be320",
"gitDir": "test/corpus/repos/ruby",
"sha2": "dc248ad29a694f47fbba18a5b1addde1e2f9de7a"
}]

View File

@ -0,0 +1,216 @@
[{
"testCaseDescription": "ruby-method-declaration-params-setup-test",
"expectedResult": {
"changes": {
"method-declaration-params.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
2,
4
]
}
},
"summary": "Added the 'foo()' method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration-params.rb"
],
"sha1": "889c05df9a650c94dd7949b8f72390180ccb0235",
"gitDir": "test/corpus/repos/ruby",
"sha2": "8f592d5202cdb294f41d177b22ae57cda75f5fb2"
}
,{
"testCaseDescription": "ruby-method-declaration-params-insert-test",
"expectedResult": {
"changes": {
"method-declaration-params.rb": [
{
"span": {
"insert": {
"start": [
1,
9
],
"end": [
1,
10
]
}
},
"summary": "Added the 'a' identifier in the foo(a) method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration-params.rb"
],
"sha1": "8f592d5202cdb294f41d177b22ae57cda75f5fb2",
"gitDir": "test/corpus/repos/ruby",
"sha2": "8e6c85a5cc3b1b45d9c6f807d76202e0b789abb4"
}
,{
"testCaseDescription": "ruby-method-declaration-params-replacement-test",
"expectedResult": {
"changes": {
"method-declaration-params.rb": [
{
"span": {
"insert": {
"start": [
1,
12
],
"end": [
1,
13
]
}
},
"summary": "Added the 'b' identifier in the foo(a, b, c) method"
},
{
"span": {
"insert": {
"start": [
1,
15
],
"end": [
1,
16
]
}
},
"summary": "Added the 'c' identifier in the foo(a, b, c) method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration-params.rb"
],
"sha1": "8e6c85a5cc3b1b45d9c6f807d76202e0b789abb4",
"gitDir": "test/corpus/repos/ruby",
"sha2": "e01ce7a23ef50f94b90c1326beaa2609a5ebadf0"
}
,{
"testCaseDescription": "ruby-method-declaration-params-delete-replacement-test",
"expectedResult": {
"changes": {
"method-declaration-params.rb": [
{
"span": {
"delete": {
"start": [
1,
12
],
"end": [
1,
13
]
}
},
"summary": "Deleted the 'b' identifier in the foo(a) method"
},
{
"span": {
"delete": {
"start": [
1,
15
],
"end": [
1,
16
]
}
},
"summary": "Deleted the 'c' identifier in the foo(a) method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration-params.rb"
],
"sha1": "e01ce7a23ef50f94b90c1326beaa2609a5ebadf0",
"gitDir": "test/corpus/repos/ruby",
"sha2": "1be244f66a5e4a57e18cdc6cb59b12149011b680"
}
,{
"testCaseDescription": "ruby-method-declaration-params-delete-insert-test",
"expectedResult": {
"changes": {
"method-declaration-params.rb": [
{
"span": {
"delete": {
"start": [
1,
9
],
"end": [
1,
10
]
}
},
"summary": "Deleted the 'a' identifier in the foo() method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration-params.rb"
],
"sha1": "1be244f66a5e4a57e18cdc6cb59b12149011b680",
"gitDir": "test/corpus/repos/ruby",
"sha2": "ea9d6ce956ce8aef4ec177b17069183fcbcf426f"
}
,{
"testCaseDescription": "ruby-method-declaration-params-teardown-test",
"expectedResult": {
"changes": {
"method-declaration-params.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
2,
4
]
}
},
"summary": "Deleted the 'foo()' method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration-params.rb"
],
"sha1": "ea9d6ce956ce8aef4ec177b17069183fcbcf426f",
"gitDir": "test/corpus/repos/ruby",
"sha2": "d8cf8ed3178a413a5273cc21f3e29a671546ff84"
}]

View File

@ -0,0 +1,346 @@
[{
"testCaseDescription": "ruby-method-declaration-insert-test",
"expectedResult": {
"changes": {
"method-declaration.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
2,
4
]
}
},
"summary": "Added the 'foo' method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration.rb"
],
"sha1": "2b8a209b5182b90d44688005575420851d868f62",
"gitDir": "test/corpus/repos/ruby",
"sha2": "152400cb111d1381378c9aa80fe7441fdc0b84d2"
}
,{
"testCaseDescription": "ruby-method-declaration-replacement-insert-test",
"expectedResult": {
"changes": {
"method-declaration.rb": [
{
"span": {
"insert": {
"start": [
1,
1
],
"end": [
3,
4
]
}
},
"summary": "Added the 'bar' method"
},
{
"span": {
"insert": {
"start": [
4,
1
],
"end": [
5,
4
]
}
},
"summary": "Added the 'foo' method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration.rb"
],
"sha1": "152400cb111d1381378c9aa80fe7441fdc0b84d2",
"gitDir": "test/corpus/repos/ruby",
"sha2": "fbb8c869988afa65b0d233d3ea7b9298ed422fa3"
}
,{
"testCaseDescription": "ruby-method-declaration-delete-insert-test",
"expectedResult": {
"changes": {
"method-declaration.rb": [
{
"span": {
"replace": [
{
"start": [
1,
5
],
"end": [
1,
8
]
},
{
"start": [
1,
5
],
"end": [
1,
8
]
}
]
},
"summary": "Replaced the 'bar' identifier with the 'foo' identifier in the foo method"
},
{
"span": {
"delete": {
"start": [
1,
9
],
"end": [
1,
10
]
}
},
"summary": "Deleted the 'a' identifier in the foo method"
},
{
"span": {
"delete": {
"start": [
2,
3
],
"end": [
2,
6
]
}
},
"summary": "Deleted the 'baz' identifier in the foo method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration.rb"
],
"sha1": "fbb8c869988afa65b0d233d3ea7b9298ed422fa3",
"gitDir": "test/corpus/repos/ruby",
"sha2": "fbcfd55e62585d839a93fefcfe4911907e5753fb"
}
,{
"testCaseDescription": "ruby-method-declaration-replacement-test",
"expectedResult": {
"changes": {
"method-declaration.rb": [
{
"span": {
"replace": [
{
"start": [
1,
5
],
"end": [
1,
8
]
},
{
"start": [
1,
5
],
"end": [
1,
8
]
}
]
},
"summary": "Replaced the 'foo' identifier with the 'bar' identifier in the bar method"
},
{
"span": {
"insert": {
"start": [
1,
9
],
"end": [
1,
10
]
}
},
"summary": "Added the 'a' identifier in the bar method"
},
{
"span": {
"insert": {
"start": [
2,
3
],
"end": [
2,
6
]
}
},
"summary": "Added the 'baz' identifier in the bar method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration.rb"
],
"sha1": "fbcfd55e62585d839a93fefcfe4911907e5753fb",
"gitDir": "test/corpus/repos/ruby",
"sha2": "80b67427ee52635b3f00f9d329e2f8dd36821c8c"
}
,{
"testCaseDescription": "ruby-method-declaration-delete-replacement-test",
"expectedResult": {
"changes": {
"method-declaration.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
3,
4
]
}
},
"summary": "Deleted the 'bar' method"
},
{
"span": {
"delete": {
"start": [
4,
1
],
"end": [
5,
4
]
}
},
"summary": "Deleted the 'foo' method"
},
{
"span": {
"insert": {
"start": [
3,
1
],
"end": [
5,
4
]
}
},
"summary": "Added the 'bar' method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration.rb"
],
"sha1": "80b67427ee52635b3f00f9d329e2f8dd36821c8c",
"gitDir": "test/corpus/repos/ruby",
"sha2": "3de5e0f1d49ad36843ca4d89e1b54b1077c3c6ef"
}
,{
"testCaseDescription": "ruby-method-declaration-delete-test",
"expectedResult": {
"changes": {
"method-declaration.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
2,
4
]
}
},
"summary": "Deleted the 'foo' method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration.rb"
],
"sha1": "3de5e0f1d49ad36843ca4d89e1b54b1077c3c6ef",
"gitDir": "test/corpus/repos/ruby",
"sha2": "71bdd986420b21efd04056017ac0f06eb539a699"
}
,{
"testCaseDescription": "ruby-method-declaration-delete-rest-test",
"expectedResult": {
"changes": {
"method-declaration.rb": [
{
"span": {
"delete": {
"start": [
1,
1
],
"end": [
3,
4
]
}
},
"summary": "Deleted the 'bar' method"
}
]
},
"errors": {}
},
"filePaths": [
"method-declaration.rb"
],
"sha1": "71bdd986420b21efd04056017ac0f06eb539a699",
"gitDir": "test/corpus/repos/ruby",
"sha2": "25348d220869101b20b073b5f39a60076612326b"
}]

View File

@ -0,0 +1,15 @@
[
{
"language": "ruby",
"fileExt": ".rb",
"repoUrl": "https://github.com/diff-fixtures/ruby.git",
"syntaxes": [
{
"syntax": "method-declaration-params",
"template": "def foo{0}\nend",
"insert": "(a)",
"replacement": "(a, b, c)"
}
]
}
]

View File

@ -94,6 +94,11 @@
"insert": "condition ? case1 : case2",
"replacement": "if condition then case 1 else case2"
},
{
"syntax": "method-declaration",
"insert": "def foo\nend",
"replacement": "def bar(a)\n baz\nend"
},
{
"syntax": "method-invocation",
"insert": "print\nfoo.bar\nbar",
@ -184,6 +189,12 @@
"insert": "foo[bar]\nfoo[:bar]\nfoo[bar] = 1",
"replacement": "x[\"b\"]\nx[:\"c\"]"
},
{
"syntax": "begin",
"template": "def foo\n{0}end",
"insert": "begin\nend\n",
"replacement": "begin\n puts 'hi'\nend\n"
},
{
"syntax": "else",
"template": "begin\n foo()\n{0}end",

@ -1 +1 @@
Subproject commit c4e3bd3c2f9f845c116b7b567b28eaa7a2ac6327
Subproject commit d8cf8ed3178a413a5273cc21f3e29a671546ff84