py-tree-sitter icon indicating copy to clipboard operation
py-tree-sitter copied to clipboard

Add Node.edit binding

Open Sh3Rm4n opened this issue 3 years ago • 1 comments

I was playing around and was missing the method to edit nodes without having to recapture nodes after a source file edit. This is almost a copy of the tree_edit implementation.

Regarding the reference counting logic, I have no idea what I am doing. This is an adjustment from a gut feeling comparing to the tree_edit function. This is my first time working with python c bindings.

TODO:

  • [ ] Unit Tests

Sh3Rm4n avatar Apr 25 '22 14:04 Sh3Rm4n

Setting both self->tree and self->children to None seems weird: https://github.com/tree-sitter/py-tree-sitter/pull/106/commits/621e281105f3698c0a2b66b1fa1ce35f5c59ae1c#diff-a6adb5252016d665f70ccc5cc08b965138ee83044685c0ffe064d8ba4a6a927cR189-R190

I think there's nowhere else in the binding where self->tree is None on a Node, and both node_get_children and node_walk assume self->tree is a valid Tree object.

Referencing the tree sitter docs: http://tree-sitter.github.io/tree-sitter/using-parsers#advanced-parsing

This ts_node_edit function is only needed in the case where you have retrieved TSNode instances before editing the tree, and then after editing the tree, you want to continue to use those specific node instances. Often, you’ll just want to re-fetch nodes from the edited tree, in which case ts_node_edit is not needed.

It looks like the rust bindings don't invalidate node->tree after the edit: https://github.com/tree-sitter/tree-sitter/blob/master/lib/binding_rust/lib.rs#L1090 but I haven't looked deeper into the API to see if there's something I'm missing.

lunixbochs avatar May 06 '22 22:05 lunixbochs