Comments (3)
Changed it to
cap, _, ok := qc.NextCapture()
if !ok {
break
}
name := q.CaptureNameForId(cap.Captures[0].Index)
which worked ok... But feels like a hack, especially around cap.Captures[0]
part.
Am I wrong to expect NextCapture to return the index of the capture?
from go-tree-sitter.
The NextCapture
API returns the index within the match (always zero in your case), not within the query. This is the same behaviour as the underlying C API. I've not used this before, but it looks like the intention is to allow iterating over multiple captures in order, when they might not be in order in the Captures
slice.
For your use case I would just use NextMatch
and use .Captures[0]
as you are doing.
from go-tree-sitter.
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
{1 0} {1 8} keyword function function
{2 1} {2 17} comment comment // comment line
{4 1} {4 3} keyword if if
{5 1} {5 7} keyword return return
example
from go-tree-sitter.
Related Issues (20)
- Lua parser does not compile on current master HOT 1
- go mod error HOT 1
- Upstreaming generation of bindings HOT 6
- Update javascript grammer
- Unable to build for windows HOT 6
- How can I support Objective-C and Swift? HOT 1
- صباح HOT 1
- add vue
- ld: library not found for -lcrt0.o for python
- Cannot build library with MacOS HOT 1
- Grammar update script not working HOT 1
- Panic when parsing C++ code in tree-sitter > 0.20.0 HOT 3
- Why do I need to call FilterPredicates to apply the predicates?
- An error occurred when parsing the JS file
- Tags support? HOT 1
- Querying does not always work as it should HOT 1
- parse c is oom, please take a look. HOT 1
- Missing typescript module HOT 1
- Suggested update for Custom grammars section in the README
- Child and ChildCount use different different types
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-tree-sitter.