bonede / tree-sitter-ng

Next generation Tree Sitter Java binding.
MIT License
61 stars 8 forks source link

Query Match but can not capture node #17

Closed JJLin63 closed 4 months ago

JJLin63 commented 5 months ago

I have got a problem when using query-match syntax, which cursor.nextMatch(match) whill return Ture but match.getCaptures() always empty.

source code:

public class Main {
    public static void main(String[] args) throws IOException {
        TreeSitterJava treeSitterJava = new TreeSitterJava();
        TSParser javaParser = new TSParser();
        javaParser.setLanguage(treeSitterJava);

        // This file
        String filePath = "./Main.java";
        TSTree tsTree = javaParser.parseString(null, new String(Files.readAllBytes(Paths.get(filePath))));

        TSQuery query = new TSQuery(treeSitterJava, "(class_declaration name: (identifier))");
        TSQueryCursor cursor = new TSQueryCursor();
        cursor.exec(query, tsTree.getRootNode());
        TSQueryMatch match = new TSQueryMatch();
        while (cursor.nextMatch(match)) {
            // ERROR: captures always empty
            TSQueryCapture[] captures = match.getCaptures();
            for (TSQueryCapture capture : captures) {
                // Do something
            }
        }
    }
}

pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven-dependency-plugin.version>3.5.0</maven-dependency-plugin.version>
        <maven-jar-plugin.version>3.4.0</maven-jar-plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.github.bonede</groupId>
            <artifactId>tree-sitter</artifactId>
            <version>0.22.5</version>
        </dependency>

        <dependency>
            <groupId>io.github.bonede</groupId>
            <artifactId>tree-sitter-java</artifactId>
            <version>0.21.0</version>
        </dependency>
    </dependencies>
</project>
bonede commented 5 months ago

Hi there.

You must use '@' annotation in the s-expression if you want capture the node. Please reference the tree sitter docs.

JJLin63 commented 4 months ago

sorry to bother you, I will close this issue

aaswan commented 3 months ago

@JJLin63 In the following query TreeSitterQuery query = new TSQuery(json, "((document) @root)"); https://github.com/bonede/tree-sitter-ng?tab=readme-ov-file#api-tour

How can I find the capture with the name '@root' inside the for loop

while (cursor.nextMatch(match)) { TSQueryCapture[] captures = match.getCaptures(); for (TSQueryCapture capture : captures) { // How can i find the capture with the name "root", that is specified in the TSQuery @root } } The wasm tree-sitter binding provides a feature to search for capture by the tag provided in the query(@root). Is this feature not supported? or there is some way to find the capture by name?

AnimeshKoratana commented 2 months ago

I have the same question ☝️ Is there a way to get the tag associated to each capture in query results?