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

feat: reverse iteration through node parents #3214

Merged
merged 3 commits into from
Apr 23, 2024

Conversation

vanaigr
Copy link
Contributor

@vanaigr vanaigr commented Mar 24, 2024

Currently, to iterate over all parents of a given node, one would need to repeatedly call ts_node_parent() on the node itself and on each of its parents.
This creates performance issues for deep trees (neovim/neovim#24965), as ts_node_parent() itself has to iterate over all the node's parents (in reverse order, from root to the node) to find the last one.
To address this performance issue, we can introduce a function that allows iterating over the node's parents in reverse order.

@vanaigr
Copy link
Contributor Author

vanaigr commented Mar 24, 2024

I wasn't able to add any tests because the make test command fails with this message:

Regenerating typescript parser
script/generate-fixtures: line 32: 111100 Killed                  "$tree_sitter" generate src/grammar.json --no-bindings --abi=latest
make: *** [Makefile:93: test] Error 137

lib/src/node.c Outdated Show resolved Hide resolved
@clason
Copy link
Contributor

clason commented Mar 27, 2024

Possible follow-up: expose this in the query syntax via an "anti-anchor" ..., e.g.,

(
  (struct_declaration)
  ...
  (identifier) @variable.member
)

so downstream doesn't need custom predicates like #has-ancestor?. (Although nvim-treesitter uses #not-has-ancestor? as well, so predicates are likely needed either way.)

@ObserverOfTime ObserverOfTime added this to the 0.24 milestone Apr 11, 2024
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.

Could you add a test for this function in node_test.rs?

@amaanq
Copy link
Member

amaanq commented Apr 14, 2024

run cargo fmt --all @vanaigr

@lewis6991 lewis6991 merged commit 90e0e28 into tree-sitter:master Apr 23, 2024
12 checks passed
@tree-sitter tree-sitter deleted a comment from DLEI253636863 Jun 10, 2024
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.

None yet

7 participants