-
-
Notifications
You must be signed in to change notification settings - Fork 32
feat: QueryCursor iterators #31
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
base: master
Are you sure you want to change the base?
feat: QueryCursor iterators #31
Conversation
|
I'm not sure about the tests here, there's probably a better way to test IterCaptures |
|
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. |
|
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 |
Closes #30