Skip to content
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

Closed
wants to merge 4 commits into from

Conversation

andreypopp
Copy link
Contributor

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:

  • Some typo fixing + adding CONTRIBUTING.md
  • I've added Node.{children_by_field_id,children_by_field_name} as a convenience for getting a list of nodes by field.
  • Add type stubs (tested with pyright type checker)

@andreypopp andreypopp changed the title Andreypopp/patch Adding Node.children_by_field_name Mar 23, 2021
@andreypopp andreypopp changed the title Adding Node.children_by_field_name Adding Node.{children_by_field_id,children_by_field_name} Mar 23, 2021
@andreypopp andreypopp changed the title Adding Node.{children_by_field_id,children_by_field_name} Add Node.{children_by_field_id,children_by_field_name} Mar 23, 2021
Copy link
Contributor

@maxbrunsfeld maxbrunsfeld left a 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.

@@ -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);
Copy link
Contributor

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).

Copy link
Contributor Author

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"])
Copy link
Contributor

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.

This returns an iterator over multiple nodes for the specified field.
@lunixbochs
Copy link
Collaborator

merged manually in a241d08 without the type stub commit, I'll figure out type stubs as part of #68

@lunixbochs lunixbochs closed this Feb 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants