Repository navigation
Provide examples of queries to search about: annotation, class, method, etc #228
Copy link
Copy link
Open
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
wetneb commentedon Jun 15, 2026
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?
cmoulliard commentedon Jun 16, 2026
Thanks. Sorry but I still don't grok the logic behind such a query (if this is a query !) =>
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 ?
wetneb commentedon Jun 16, 2026
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.
cmoulliard commentedon Jun 16, 2026
Without good
How To guides, many users will certainly not adopt tree-sitter and switch to another and perhaps less powerful technology !wetneb commentedon Jun 16, 2026
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.
ObserverOfTime commentedon Jun 16, 2026
The guides are right here: