代码结构元数据提取
tree-sitter-language-pack-extracting-code-structure
当用户需要从源文件获取结构化代码元数据时使用——函数、类、导入、导出、符号、文档字符串、注释或语法诊断。涵盖 `ts-pack process` 的功能标志、JSON 结果结构以及默认功能集。
- License 许可
- MIT
- 最近更新
- 2026-11-15
- Stars
- ★ 485
- Forks
- 71
01 前言 Front Matter
- type
- Playbook
- title
- Tree Sitter Language Pack Extracting Code Structure
- x-ai-rulez
- { "id": "tree-sitter-language-pack-extracting-code-structure", "kind": "skill", "metadata": { "name": "tree-sitter-language-pack-extracting-code-structure" } }
- description
- Use when the user wants structured code metadata from a source file — functions, classes, imports, exports, symbols, docstrings, comments, or syntax diagnostics. Covers `ts-pack process` feature flags, the JSON result shape, and the default feature set.
02 资源文件 2
- 参考文档agents/openai.yaml144 B
- 参考文档index.md336 B
03 SKILL.md 正文
Extracting code structure
ts-pack process <file> runs the code-intelligence pipeline over a source
file and prints JSON. Use it when the user wants structured metadata —
"list the functions", "what does this file import", "find the exported
symbols", "pull the docstrings" — rather than a raw syntax tree.
Quick recipe
# Default features: structure + imports + exports
ts-pack process src/app.ts
# Pick features explicitly (only what you ask for is computed)
ts-pack process src/app.ts --structure --imports --symbols --docstrings
# Everything
ts-pack process src/app.ts --all
When no feature flags are given, process defaults to
--structure --imports --exports. Passing any single extraction flag
(--structure --imports --exports --comments --docstrings --symbols --diagnostics) turns off that default set, so list every feature you want.
--chunk-size and --language are modifiers, not extraction flags: they do
not disable the defaults (e.g. --chunk-size 500 alone still emits
structure+imports+exports and adds chunks).
Feature flags
| Flag | Extracts |
|---|---|
--structure |
Functions, classes, methods, modules (spans, nesting, visibility). |
--imports |
Import statements and their sources. |
--exports |
Exported symbols and their kinds. |
--comments |
Inline and block comments. |
--docstrings |
Docstrings attached to definitions. |
--symbols |
All identifiers (for search/indexing). |
--diagnostics |
Syntax errors and error nodes with positions. |
--all |
Enable every feature above. |
--chunk-size <bytes> |
Syntax-aware chunks — see tree-sitter-language-pack-chunking-for-llms. |
--language <name> (-l) |
Override language (auto-detected from extension otherwise). |
Result shape
process always prints JSON. Top-level keys:
{
"language": "python",
"metrics": { "total_lines": 0, "code_lines": 0, "comment_lines": 0, "blank_lines": 0, "total_bytes": 0, "node_count": 0, "error_count": 0, "max_depth": 0 },
"structure": [],
"imports": [],
"exports": [],
"comments": [],
"docstrings": [],
"symbols": [],
"diagnostics": [],
"chunks": []
}
metrics is populated whenever any processing runs. chunks appears only
when --chunk-size is set. Empty collections are omitted from the JSON
(the skip_serializing_if wire contract), so absent keys mean "nothing
found", not an error.
Structure items
Each structure entry has kind (a capitalized string —
"Function", "Class", "Method", "Module", "Struct", "Trait",
…; language-specific kinds serialize as {"Other": "<kind>"}), name,
visibility, and a nested span object with start_byte/end_byte and
zero-indexed start_line/start_column/end_line/end_column.
Nesting is expressed via children (e.g. methods inside a class), not a
parent pointer. Optional fields: decorators, doc_comment (the
attached doc comment), signature, and body_span.
Examples
# Function and class names with line numbers
ts-pack process src/service.py --structure \
| jq '.structure[] | {kind, name, line: .span.start_line}'
# Import sources only
ts-pack process src/app.ts --imports \
| jq '.imports[].source'
# Syntax error count (also available via metrics.error_count)
ts-pack process broken.go --diagnostics \
| jq '.diagnostics | length'
# Build a symbol index across a tree of files
for f in $(git ls-files '*.rs'); do
ts-pack process "$f" --symbols | jq -c --arg f "$f" '{file: $f, symbols: .symbols}'
done
SDK equivalent
from tree_sitter_language_pack import process, ProcessConfig
# ProcessConfig is a frozen dataclass — set fields in the constructor.
# structure/imports/exports default to True; add the rest you want.
config = ProcessConfig("python", symbols=True, docstrings=True)
result = process(source_code, config)
for item in result.structure: # ProcessResult is an object, not a dict
print(item.kind, item.name, item.span.start_line)
The SDK also exposes the parsed tree (get_parser(...) → Parser/Tree/
Node) for running your own tree-sitter queries, and a data_extraction
config flag that pulls a hierarchical key/value tree from data-format files
(JSON, YAML, TOML, …) into the result's data field — neither is exposed
on the CLI.
When to reach for parse instead
If the user wants the raw syntax tree rather than extracted metadata, use
ts-pack parse — see the tree-sitter-language-pack-parsing-source skill.
04 同仓库更多 Skills
tree-sitter 代码智能解析
tree-sitter-language-pack
使用 tree-sitter 语法从 371 种编程语言中解析并提取代码智能信息。适用于编写解析源代码、提取结构/导入/导出/符号/文档字符串/注释、检测语言、运行语法诊断或为大模型(LLM)生成语法感知分块的代码——支持 Rust、Python、Node.js/TypeScript 或 ts-pack CLI。涵盖安装、CLI 接口、SDK 接口以及解析器缓存管理。
tree-sitter MCP 服务使用
tree-sitter-language-pack-using-the-mcp-server
当需要通过 tree-sitter-language-pack 的 MCP 服务器工具来解析源代码、提取代码结构或检测语言(而非调用 ts-pack CLI)时使用。涵盖工具接口、自动安装启动器,以及 MCP 相较于 CLI 或 SDK 的优势场景。
源代码语法树解析
tree-sitter-language-pack-parsing-source
当用户需要为源文件生成 tree-sitter 语法树时使用——以 s-表达式转储或 JSON 树的形式呈现。涵盖 `ts-pack parse`、语言自动检测与 `--language` 的对比、标准输入(stdin)输入以及读取 `has_errors`。
解析器缓存管理
tree-sitter-language-pack-managing-parsers
当用户需要管理 tree-sitter 解析器缓存时使用——为离线或 CI 运行预取解析器、列出已下载内容、检查某种语言、查找缓存目录或清理缓存。涵盖 `ts-pack download`、`list`、`info`、`cache-dir`、`clean` 和 `init`。
代码结构提取
tree-sitter-language-pack-extracting-code-structure
当用户需要从源文件获取结构化代码元数据时使用——包括函数、类、导入、导出、符号、文档字符串、注释或语法诊断。涵盖 `ts-pack process` 的功能开关、JSON 结果结构以及默认功能集。
编程语言检测
tree-sitter-language-pack-detecting-languages
当用户想知道某个文件或代码片段属于哪种编程语言时使用。涵盖 `ts-pack parse`/`process` 中的隐式检测、通过 `ts-pack list`/`info` 确认是否支持某种语言,以及用于路径、扩展名和原始内容的 SDK 检测函数。