-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrap class, module, method, and block bodies in a named node #224
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think wrapping class/module/method/(do-)block/begin bodies as a named node makes sense. However, I'm not too keen on having different names for each one of them. In Ruby, the syntax of a class, method or do-block body is the same.
Therefore, I think having one name to wrap all bodies is better.
I noticed that you added a field body
in one of the commits but later removed it again. I think adding body
fields would actually be quite nice. Would your use-case be addressed by just adding body
fields without wrapping the body statements in a named node?
@aibaars Yea I originally had that but it made targeting just the class/module/method, etc. body hard because it was also capturing all other bodies that were a child and such. I reverted back to having just the body named node like before for everything but block but wrapped them in a named field so specific targeting is still preserved. Let me know what you think! |
grammar.js
Outdated
@@ -157,7 +159,7 @@ module.exports = grammar({ | |||
seq( | |||
field('parameters', alias($.parameters, $.method_parameters)), | |||
choice( | |||
seq(optional($._terminator), $._body_statement), | |||
seq(optional($._terminator), optional(field("method_body", $.body)), 'end'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use the field name 'body'
instead of including the type of the enclosing node. Same for do_block
, class
, module
, singleton_class
, etc.
I can see that having namespace_body
makes it easy to capture the bodies of all classes/modules/singleton classes. However, this causes problems for tools that generate data models based on the grammar. For example https://github.com/github/codeql/blob/main/ruby/ql/lib/codeql/ruby/ast/internal/TreeSitter.qll#L1104-L1124 would end up having things likeMethod::getMethodBody()
and DoBlock::getDoBlockBody()
which look unnecessarily repetitive.
It might be worth trying if defining a rule named _namespace: $ => choice($.module, $.class, $.singleton_class)
and adding _namespace
to the supertypes
list. I don't know much about the queries you are writing, but hopefully the query syntax allows selecting the body
field of all nodes of type _namespace
.
I agree, we should revert that revert; the fields should all just be One other change I would suggest is to rename |
@maxbrunsfeld I had suggested the name |
Ah yeah, I see Ripper calls it that. Good point. Let's keep it. |
@npezza93 are we ready this awesome pull? |
I'll finish this up this weekend |
@aibaars So changed to use a general body name but I don't think it is working correctly. Using a query of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me. Thanks a lot @npezza93 !
@maxbrunsfeld could you have a look at this too, please?
I think the query would need to have the
@npezza93 Does that work for you? |
@maxbrunsfeld Yep, totally works! Thanks! |
Ok, so just checking - this PR is done and working as intended, right? |
@maxbrunsfeld Yep! This is ready to go |
Great, thanks for this @npezza93. |
Thanks @npezza93 and everyone involved. This makes writing Ruby queries so much easier. Some of the things we're doing in Neovim with Treesitter are beyond what I ever thought we'd see. What are the time frames for it to be reflected in the Treesitter Playground? |
A majority of other languages I noticed wrap class and function bodies in their own named node that can be easily queried(checked javascript, Lua, java, etc). Without this named node it makes targeting the contents of these structures difficult, cumbersome, and realllyyyy slow.
Checklist: