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

Prepare for bindings PR, add Query.matches #159

Merged
merged 6 commits into from
Feb 26, 2024
Merged

Conversation

amaanq
Copy link
Member

@amaanq amaanq commented Sep 25, 2023

Closes #108
Closes #151

@jhandley
Copy link
Contributor

jhandley commented Oct 6, 2023

I'm really looking forward to the query matches API. Right now I have a mess of ugliness that tries organize the captures into matches in my code.

One request would be to return a list of nodes for the match capture for patterns like ()* and ()+ that can match multiple nodes. So instead of matches() returning List[Tuple[int, List[Tuple[Node, str]]]] have it return List[Tuple[int, List[Tuple[Union[Node, List[Node]], str]]]]. This is how the Lua wrapper has implemented this API.

@jhandley
Copy link
Contributor

jhandley commented Oct 8, 2023

I created this PR to illustrate what I mean. I ended up storing the captures in a dict[str, Union[Node, list[Node]]] where the key is the capture name and the value is either the single node captured or a list of all the nodes in the case of a capture in a * or + pattern. That seemed more natural than using tuples.

Py_XDECREF(captures_for_match);
goto error;
}
if (satisfies_text_predicates(self, _match, (Tree *)node->tree)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this check be moved outside of the capture loop to line 2011? The result of satisfies_text_predicates() should be the same the for all captures in the match so it could be checked just once per match.

@kylebebak
Copy link

@amaanq @jhandley

Any ideas on when this might get merged? If it fixes #182 (returns captures in groups, like the Tree-sitter CLI does) it would be useful to me in building Tree-sitter integration for Sublime Text

@ObserverOfTime ObserverOfTime merged commit a0e2b96 into master Feb 26, 2024
34 of 36 checks passed
@ObserverOfTime ObserverOfTime deleted the prepare-bindings branch March 19, 2024 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants