ast-grep-find

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

AST-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 python
bash
uv run python -m runtime.harness scripts/ast_grep_find.py \
    --pattern "import asyncio" --language python

Search 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 javascript
bash
uv run python -m runtime.harness scripts/ast_grep_find.py \
    --pattern "console.log(\$MSG)" --replace "logger.info(\$MSG)" \
    --language javascript

Dry 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-run
bash
uv run python -m runtime.harness scripts/ast_grep_find.py \
    --pattern "print(\$X)" --replace "logger.info(\$X)" \
    --language python --dry-run

Parameters

参数说明

ParameterDescription
--pattern
AST pattern to search (required)
--language
Language:
python
,
javascript
,
typescript
,
go
, etc.
--path
Directory to search (default:
.
)
--glob
File glob pattern (e.g.,
**/*.py
)
--replace
Replacement pattern for refactoring
--dry-run
Preview changes without applying
--context
Lines of context (default: 2)
参数描述
--pattern
要搜索的AST模式(必填)
--language
目标语言:
python
javascript
typescript
go
--path
要搜索的目录(默认:
.
--glob
文件匹配模式(例如:
**/*.py
--replace
重构时使用的替换模式
--dry-run
预览变更而不实际应用
--context
上下文行数(默认:2)

Pattern Syntax

模式语法

SyntaxMeaning
$NAME
Match single node (variable, expression)
$$$
Match multiple nodes (arguments, statements)
$_
Match any single node (wildcard)
语法含义
$NAME
匹配单个节点(变量、表达式)
$$$
匹配多个节点(参数、语句)
$_
匹配任意单个节点(通配符)

Examples

示例

bash
undefined
bash
undefined

Find all function definitions

查找所有函数定义

uv run python -m runtime.harness scripts/ast_grep_find.py
--pattern "def $FUNC($$$):" --language python
uv run python -m runtime.harness scripts/ast_grep_find.py
--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
uv run python -m runtime.harness scripts/ast_grep_find.py
--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
undefined
uv run python -m runtime.harness scripts/ast_grep_find.py
--pattern "print($X)" --replace "logging.info($X)"
--language python --dry-run
undefined

vs morph/warpgrep

与morph/warpgrep的对比

ToolBest For
ast-grepStructural patterns (understands code syntax)
warpgrepFast 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
ast-grep
server in mcp_config.json.
需要在mcp_config.json中配置
ast-grep
服务器。