Giter Site home page Giter Site logo

Comments (3)

sluongng avatar sluongng commented on May 14, 2024 1

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.

didroe avatar didroe commented on May 14, 2024

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.

vipmax avatar vipmax commented on May 14, 2024
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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.