Note: This project is in early development. APIs and CLI flags may change.
tsq (tree-sitter query) is a command-line tool and Go library for exploring
code structure using tree-sitter.
Think of it as jq for code - query and extract structured information from
source files.
Currently supports: Go. Extensible to other languages.
- Query: Run custom tree-sitter queries on code
- Symbols: Extract functions, types, methods, variables, constants
- Outline: Get structural overview of a file (package, imports, symbols)
- Refs: Find references to symbols across your codebase
- Fast: Parallel processing with worker pools
- Library: Use as a Go library in your own projects
go install github.com/arjunmahishi/tsq/cmd/tsq@latestOr build from source:
git clone https://github.com/arjunmahishi/tsq.git
cd tsq
go build -o tsq ./cmd/tsq# Query all function declarations (captures whole function)
tsq query -q '(function_declaration) @fn' --path .
# Extract function names only
tsq query -q '(function_declaration name: (identifier) @name)' --path .
# Query from a file
tsq query --query-file myquery.scm --path ./src
# Query a single file
tsq query -q '(type_declaration) @type' --file main.goTip: Queries need
@namecaptures to return useful data. Without captures, you'll get matches with empty results. For example:
(function_declaration)- matches but returns"captures": null(function_declaration) @fn- captures the whole function node(function_declaration name: (identifier) @name)- captures just the function nameRun
tsq example-queriesfor more query patterns.
# Extract all symbols from current directory
tsq symbols --path .
# Extract symbols from a single file
tsq symbols --file main.go
# Filter by visibility
tsq symbols --path . --visibility public
# Include source code
tsq symbols --file main.go --include-source --max-source-lines 5# Get outline of a file
tsq outline --file main.go
# Include source snippets
tsq outline --file main.go --include-source# Find all references to a symbol
tsq refs --symbol MyFunc --path .
# Search in a single file
tsq refs --symbol MyType --file main.go
# Include surrounding code context
tsq refs --symbol MyVar --path . --include-contextMost commands support these flags:
--compact: Minimize JSON output--jobs,-j: Number of parallel workers (default: CPU count)--max-bytes: Skip files larger than this (default: 2MB)
Import tsq as a library in your Go projects:
package main
import (
"fmt"
"log"
"github.com/arjunmahishi/tsq/tsq"
)
func main() {
// Extract symbols from a file
results, err := tsq.Symbols(tsq.SymbolsOptions{
File: "main.go",
Visibility: "public",
})
if err != nil {
log.Fatal(err)
}
for _, result := range results {
fmt.Printf("File: %s\n", result.File)
for _, sym := range result.Symbols {
fmt.Printf(" - %s (%s)\n", sym.Name, sym.Kind)
}
}
}Run a custom tree-sitter query.
Extract symbols (functions, types, methods, etc.) from code.
Get the structural overview of a file (package, imports, symbols).
Find all references to a symbol.
See GoDoc for full API documentation.
All commands output JSON:
{
"file": "main.go",
"symbols": [
{
"name": "main",
"kind": "function",
"visibility": "private",
"file": "main.go",
"range": {
"start": {"line": 10, "column": 1},
"end": {"line": 15, "column": 2}
}
}
]
}