代码结构与调用图分析工具

tree-sitter-analyzer 是一款代码分析工具,通过 AST 提取代码结构、构建调用图并追踪符号引用,适用于多语言项目的静态分析与代码导航。

  • by X1r0z
  • Rust
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 X1r0z/tree-sitter-mcp

使用说明

tree-sitter-analyzer

A code analysis toolkit using tree-sitter for AST-based code structure extraction, call graph analysis, and symbol reference tracking.

Features

  • Code Structure Extraction - Extract all function/method and class/struct/interface/field definitions
  • Inheritance Analysis - Extract class inheritance relationships, including parent classes and child classes
  • Call Graph Analysis - Build call graphs showing caller-callee relationships
  • Import Analysis - Extract import statements and dependencies
  • Annotation Analysis - Extract Java annotations and Python decorators with their targets
  • Symbol Reference Tracking - Find identifier, type, property, field, and import-path references for a specific code symbol

Supported Languages

Language Extensions
Python .py, .pyw, .pyi
JavaScript .js, .mjs, .cjs, .jsx
TypeScript .ts, .tsx
Java .java
Go .go

Use -l, --language <LANGUAGE> to restrict AST parsing to one language. Allowed values: python, java, go, javascript, typescript, tsx.

All query commands run against the SQLite index in the current working directory. If tsa.db is missing or incompatible with the requested root/language, tsa rebuilds the index automatically before executing the query. tsa index remains available when you want to prebuild or refresh the cache explicitly.

Installation

cargo install --git https://github.com/X1r0z/tree-sitter-analyzer

Usage

# List all classes in a directory
tsa classes ./src/

# Queries auto-build `tsa.db` when needed
tsa functions ./src/

# You can still prebuild the index explicitly
tsa index .

# Only analyze Python files
tsa functions ./src/ -l python

# Find all callers of a function
tsa callers ./src/ --function process_data

# Trace callers backward in the call graph
tsa graph ./src/ --function process_data --depth 2 --backward

# Trace callees forward in the call graph
tsa graph ./src/ --function process_data --depth 2 --forward

# Get function definition
tsa definition ./src/ --function main

See USAGE.md for complete documentation.

Tools

Tool Description
index Build a persistent project index
functions Extract all function/method definitions
classes Extract all class/struct/interface definitions
fields Extract all field definitions
imports Extract all import statements
annotations Extract Java annotations and Python decorators
callers Find functions that call a specific function
callees Find functions called by a specific function
graph Trace callers backward or callees forward across multiple call levels
refs Find all references to a specific symbol
definition Extract source code of a function
super-classes Get all parent classes of a specific class
sub-classes Get all child classes that inherit from a specific class

Development

# Build
cargo build --release

# Lint
cargo clippy

# Format
cargo fmt

License

MIT