Skip to content

Provide examples of queries to search about: annotation, class, method, etc #228

Open

Description

@cmoulliard

Suggestion

As creating Tree-sitter S-Expressions - queries are not really simple, could you please provide some examples to allow the end users to figure out how they could query class, interface, enum, etc ?

Project used as example: https://github.com/ch007m/java-tree-sitter/tree/acc9a3108b4fd66c3147629db03081c24d8789b8/examples/spring-boot-todo-app

Example - Search about a class having as name

Query

(class_declaration name: (identifier) @class.name (#eq? @class.name "AppApplication"))

Result

./examples/spring-boot-todo-app/src/main/java/com/todo/app/AppApplication.java
  pattern: 0
    capture: 0 - class.name, start: (6, 13), end: (6, 27), text: `AppApplication`
./examples/spring-boot-todo-app/src/main/java/com/todo/app/controller/TaskController.java
./examples/spring-boot-todo-app/src/main/java/com/todo/app/entity/Task.java
./examples/spring-boot-todo-app/src/main/java/com/todo/app/repository/TaskRepository.java
./examples/spring-boot-todo-app/src/main/java/com/todo/app/service/TaskService.java
./examples/spring-boot-todo-app/src/main/java/com/todo/app/service/TaskServiceImpl.java

Example - Search about the annotation: @entity

[
  (marker_annotation
    name: (identifier) @annotation.name
    (#eq? @annotation.name "Entity")) @entity.annotation

  (annotation
    name: (identifier) @annotation.name
    (#eq? @annotation.name "Entity")) @entity.annotation
]

Result

cat ./examples/queries/annotation.txt | tree-sitter query /dev/stdin $SRC_PATH
Processing: /Users/cmoullia/code/quarkus/rewrite-mtool/_others/treesitter/aesh-tree-sitter/examples/spring-boot-todo-app/src/main/java/com/todo/app/repository/TaskRepository.java
Processing Java File: /Users/cmoullia/code/quarkus/rewrite-mtool/_others/treesitter/aesh-tree-sitter/examples/spring-boot-todo-app/src/main/java/com/todo/app/repository/TaskRepository.java
/Users/cmoullia/code/quarkus/rewrite-mtool/_others/treesitter/aesh-tree-sitter/examples/spring-boot-todo-app/src/main/java/com/todo/app/repository/TaskRepository.java
Processing: /Users/cmoullia/code/quarkus/rewrite-mtool/_others/treesitter/aesh-tree-sitter/examples/spring-boot-todo-app/src/main/java/com/todo/app/entity/Task.java
Processing Java File: /Users/cmoullia/code/quarkus/rewrite-mtool/_others/treesitter/aesh-tree-sitter/examples/spring-boot-todo-app/src/main/java/com/todo/app/entity/Task.java
/Users/cmoullia/code/quarkus/rewrite-mtool/_others/treesitter/aesh-tree-sitter/examples/spring-boot-todo-app/src/main/java/com/todo/app/entity/Task.java
  pattern: 0
    capture: 1 - entity.annotation, start: (7, 0), end: (7, 7), text: `@Entity`
    capture: 0 - annotation.name, start: (7, 1), end: (7, 7), text: `Entity`
Processing: /Users/cmoullia/code/quarkus/rewrite-mtool/_others/treesitter/aesh-tree-sitter/examples/spring-boot-todo-app/src/main/java/com/todo/app/controller/TaskController.java
Processing Java File: /Users/cmoullia/code/quarkus/rewrite-mtool/_others/treesitter/aesh-tree-sitter/examples/spring-boot-todo-app/src/main/java/com/todo/app/controller/TaskController.java

Activity

  1. wetneb commented on Jun 15, 2026

    @wetneb

    I think the simplest is that you look at examples of queries in https://github.com/tree-sitter/tree-sitter-java/tree/master/queries and of the expected AST structure in https://github.com/tree-sitter/tree-sitter-java/tree/master/test/corpus, no?

  2. cmoulliard commented on Jun 16, 2026

    @cmoulliard
    Author

    I think the simplest is that you look at examples of queries in https://github.com/tree-sitter/tree-sitter-java/tree/master/queries and of the expected AST structure in https://github.com/tree-sitter/tree-sitter-java/tree/master/test/corpus, no?

    Thanks. Sorry but I still don't grok the logic behind such a query (if this is a query !) =>

    (class_declaration
      name: (identifier) @name) @definition.class
    

    Why do we have to create the query as "(class_declaration name: (identifier) ????) ? This syntax is not intuitive and really hermetic.
    What id the purpose of "@name" ? Is it a keywork ?
    What is "@definition.class" ? Where is it defined/declared within the tree-sitter code ?

  3. wetneb commented on Jun 16, 2026

    @wetneb

    That all sounds like general questions about what tree-sitter queries are and how they work, independently of this grammar. I see you've been asking for support on Matrix/Discord, that's probably the best way to get help indeed.

  4. cmoulliard commented on Jun 16, 2026

    @cmoulliard
    Author

    That all sounds like general questions about what tree-sitter queries are and how they work, independently of this grammar. I see you've been asking for support on Matrix/Discord, that's probably the best way to get help indeed.

    Without good How To guides, many users will certainly not adopt tree-sitter and switch to another and perhaps less powerful technology !

  5. wetneb commented on Jun 16, 2026

    @wetneb

    I hear your frustration, @cmoulliard. We've been chatting with you on Matrix/Discord, so I propose not to duplicate the conversation in two places and keep it there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions