-
Notifications
You must be signed in to change notification settings - Fork 106
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
Add Node.{children_by_field_id,children_by_field_name} #63
Conversation
Just making things easier to grep
d0bac13
to
9fdb632
Compare
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.
Thanks for doing all of this. I just left one change request.
tree_sitter/binding.c
Outdated
@@ -134,6 +134,45 @@ static PyObject *node_chield_by_field_name(Node *self, PyObject *args) { | |||
return node_new_internal(child, self->tree); | |||
} | |||
|
|||
static PyObject *node_children_by_field_id_internal(Node *self, TSFieldId field_id) { | |||
PyObject *result = PyList_New(0); | |||
TSTreeCursor cursor = ts_tree_cursor_new(self->node); |
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 that this TSTreeCursor
will be leaked. You would have to destroy it with ts_tree_cursor_delete
. But actually, you should probably use the same approach as node_get_children
does below, where it reused a global TSTreeCursor
called default_cursor
(to avoid unnecessary allocations).
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.
Indeed, updated code to use default_cursor
.
jsx_node = tree.root_node.children[0].children[0] | ||
|
||
attributes = jsx_node.children_by_field_name("attribute") | ||
self.assertEqual([a.type for a in attributes], ["jsx_attribute", "jsx_attribute"]) |
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.
These tests are great. Thank you.
9fdb632
to
e09f2be
Compare
This returns an iterator over multiple nodes for the specified field.
e09f2be
to
73f6621
Compare
This PR does multiple things but I kept the commits separately so it'd be easier to review. If needed — I can split this into multiple PRs.
What it does:
Node.{children_by_field_id,children_by_field_name}
as a convenience for getting a list of nodes by field.