feat: QueryCursor iterators#31
Open
bennypowers wants to merge 4 commits into
Open
Conversation
Author
|
I'm not sure about the tests here, there's probably a better way to test IterCaptures |
bennypowers
marked this pull request as ready for review
May 29, 2025 11:44
topi314
reviewed
May 29, 2025
Author
|
I tried something similar in a project i'm working on and got some super weird errors func allMatches(qc *ts.QueryCursor, q *ts.Query, node *ts.Node, text []byte) iter.Seq2[*ts.QueryMatch, *ts.Query] {
qm := qc.Matches(q, node, text)
return func(yield func (qm *ts.QueryMatch, q *ts.Query) bool) {
for {
m := qm.Next()
if m == nil {
break
}
if !yield(m, q) {
return
}
}
}
}in the yield call i got a panic: edit: ok i need to learn more about go resource management. for now I'm doing this so i can close the resources when finished at the call site func QueryMatches(queryName string,
language *ts.Language,
text []byte,
node *ts.Node) (*ts.Query, *ts.QueryCursor, iter.Seq2[*ts.QueryMatch, *ts.Query]) {
queryText, err := loadQueryFile(queryName)
if err != nil {
log.Fatal(nil)
}
q, qerr := ts.NewQuery(language, queryText)
if qerr != nil {
log.Fatal(qerr)
}
qc := ts.NewQueryCursor()
qm := qc.Matches(q, node, text)
return q, qc, func(yield func (qm *ts.QueryMatch, q *ts.Query) bool) {
for {
m := qm.Next()
if m == nil {
break
}
if !yield(m, q) {
return
}
}
}
}I'm demoting this back to draft until i grok this better. |
bennypowers
marked this pull request as draft
May 29, 2025 16:00
bennypowers
marked this pull request as ready for review
June 1, 2025 08:52
Author
|
Ok having played around with this, it was pebcak. I believe this code does what it says it does, I was just not properly managing the treesitter c object lifetimes |
Fix tests to use the correct method names (AllMatches/AllCaptures instead of IterMatches/IterCaptures) per Go iter naming conventions. Add tests for early break, empty results, and multiple patterns. Assisted-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
bennypowers
force-pushed
the
feat/querycursor-iterators
branch
from
March 28, 2026 20:04
e16aa6d to
305e1df
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #30
Adds
AllMatchesandAllCapturesiterator methods toQueryCursor, following Go's iter naming conventions as suggested in #30.AllMatchesreturnsiter.Seq[*QueryMatch]AllCapturesreturnsiter.Seq2[*QueryMatch, uint]The existing
Matches/Capturesmethods are unchanged.