-
Notifications
You must be signed in to change notification settings - Fork 123
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
NextCapture return inaccurate index #97
Comments
Changed it to
which worked ok... But feels like a hack, especially around Am I wrong to expect NextCapture to return the index of the capture? |
The For your use case I would just use |
import (
"context"
"fmt"
sitter "github.com/smacker/go-tree-sitter"
"github.com/smacker/go-tree-sitter/javascript"
"testing"
)
func TestTreeSitterQueries(t *testing.T) {
code := []byte(`
function hello() {
// comment line
console.log('hello')
if (true) { console.log('true') }
return "value"
}
`)
query := `
[
"function"
"if"
"return"
] @keyword
(comment) @comment
`
// Parse source code
lang := javascript.GetLanguage()
n, _ := sitter.ParseCtx(context.Background(), code, lang)
// Execute the query
q, _ := sitter.NewQuery([]byte(query), lang)
qc := sitter.NewQueryCursor()
qc.Exec(q, n)
for {
m, ok := qc.NextMatch()
if !ok { break }
m = qc.FilterPredicates(m, code)
for _, c := range m.Captures {
name := q.CaptureNameForId(c.Index)
content := c.Node.Content(code)
fmt.Println(c.Node.StartPoint(), c.Node.EndPoint(), name, c.Node.Type(), content)
}
}
} output
example |
I have a query with multiple captures like this
Then I would use it to query over a
.tsx
file as followThe expected result is that I would eventually get all 3 capture groups.
Actual result is
Using the same query on
tree-sitter-cli
yielded expected resultThe text was updated successfully, but these errors were encountered: