ast-grep-find
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAST-Grep Find
AST-Grep 查找
Structural code search that understands syntax. Find patterns like function calls, imports, class definitions - not just text.
理解语法的结构化代码搜索。可查找函数调用、导入语句、类定义等模式——而非仅仅是文本匹配。
When to Use
适用场景
- Find code patterns (ignores strings/comments)
- Search for function calls, class definitions, imports
- Refactor code with AST precision
- Rename variables/functions across codebase
- 查找代码模式(忽略字符串/注释)
- 搜索函数调用、类定义、导入语句
- 以AST级精度重构代码
- 在整个代码库中重命名变量/函数
Usage
使用方法
Search for a pattern
搜索指定模式
bash
uv run python -m runtime.harness scripts/ast_grep_find.py \
--pattern "import asyncio" --language pythonbash
uv run python -m runtime.harness scripts/ast_grep_find.py \
--pattern "import asyncio" --language pythonSearch in specific directory
在指定目录中搜索
bash
uv run python -m runtime.harness scripts/ast_grep_find.py \
--pattern "async def \$FUNC(\$\$\$)" --language python --path "./src"bash
uv run python -m runtime.harness scripts/ast_grep_find.py \
--pattern "async def \$FUNC(\$\$\$)" --language python --path "./src"Refactor/replace pattern
重构/替换模式
bash
uv run python -m runtime.harness scripts/ast_grep_find.py \
--pattern "console.log(\$MSG)" --replace "logger.info(\$MSG)" \
--language javascriptbash
uv run python -m runtime.harness scripts/ast_grep_find.py \
--pattern "console.log(\$MSG)" --replace "logger.info(\$MSG)" \
--language javascriptDry run (preview changes)
试运行(预览变更)
bash
uv run python -m runtime.harness scripts/ast_grep_find.py \
--pattern "print(\$X)" --replace "logger.info(\$X)" \
--language python --dry-runbash
uv run python -m runtime.harness scripts/ast_grep_find.py \
--pattern "print(\$X)" --replace "logger.info(\$X)" \
--language python --dry-runParameters
参数说明
| Parameter | Description |
|---|---|
| AST pattern to search (required) |
| Language: |
| Directory to search (default: |
| File glob pattern (e.g., |
| Replacement pattern for refactoring |
| Preview changes without applying |
| Lines of context (default: 2) |
| 参数 | 描述 |
|---|---|
| 要搜索的AST模式(必填) |
| 目标语言: |
| 要搜索的目录(默认: |
| 文件匹配模式(例如: |
| 重构时使用的替换模式 |
| 预览变更而不实际应用 |
| 上下文行数(默认:2) |
Pattern Syntax
模式语法
| Syntax | Meaning |
|---|---|
| Match single node (variable, expression) |
| Match multiple nodes (arguments, statements) |
| Match any single node (wildcard) |
| 语法 | 含义 |
|---|---|
| 匹配单个节点(变量、表达式) |
| 匹配多个节点(参数、语句) |
| 匹配任意单个节点(通配符) |
Examples
示例
bash
undefinedbash
undefinedFind all function definitions
查找所有函数定义
uv run python -m runtime.harness scripts/ast_grep_find.py
--pattern "def $FUNC($$$):" --language python
--pattern "def $FUNC($$$):" --language python
uv run python -m runtime.harness scripts/ast_grep_find.py
--pattern "def $FUNC($$$):" --language python
--pattern "def $FUNC($$$):" --language python
Find console.log calls
查找console.log调用
uv run python -m runtime.harness scripts/ast_grep_find.py
--pattern "console.log($$$)" --language javascript
--pattern "console.log($$$)" --language javascript
uv run python -m runtime.harness scripts/ast_grep_find.py
--pattern "console.log($$$)" --language javascript
--pattern "console.log($$$)" --language javascript
Replace print with logging
将print替换为logging
uv run python -m runtime.harness scripts/ast_grep_find.py
--pattern "print($X)" --replace "logging.info($X)"
--language python --dry-run
--pattern "print($X)" --replace "logging.info($X)"
--language python --dry-run
undefineduv run python -m runtime.harness scripts/ast_grep_find.py
--pattern "print($X)" --replace "logging.info($X)"
--language python --dry-run
--pattern "print($X)" --replace "logging.info($X)"
--language python --dry-run
undefinedvs morph/warpgrep
与morph/warpgrep的对比
| Tool | Best For |
|---|---|
| ast-grep | Structural patterns (understands code syntax) |
| warpgrep | Fast text/regex search (20x faster grep) |
Use ast-grep when you need syntax-aware matching. Use warpgrep for raw speed.
| 工具 | 最佳适用场景 |
|---|---|
| ast-grep | 结构化模式匹配(理解代码语法) |
| warpgrep | 快速文本/正则搜索(速度是grep的20倍) |
当你需要语法感知的匹配时使用ast-grep。追求原始速度时使用warpgrep。
MCP Server Required
需要MCP服务器
Requires server in mcp_config.json.
ast-grep需要在mcp_config.json中配置服务器。
ast-grep