Use Ast Grep Rule
Use `ast-grep` (if installed) instead of plain regex or text search when:
AST-Grep Integration Protocol for Cursor Agent
When to Use AST-Grep
Use ast-grep (if installed) instead of plain regex or text search when:
- Structural code patterns are involved (e.g., finding all function calls, class definitions, or method implementations)
- Language-aware refactoring is required (e.g., renaming variables, updating function signatures, or changing imports)
- Complex code analysis is needed (e.g., finding all usages of a pattern across different syntactic contexts)
- Cross-language searches are necessary (e.g., working with both Ruby and TypeScript in a monorepo)
- Semantic code understanding is important (e.g., finding patterns based on code structure, not just text)
AST-Grep Command Patterns
Basic Search Template:
ast-grep --pattern '$PATTERN' --lang $LANGUAGE $PATH
Common Use Cases
- Find function calls:
ast-grep --pattern 'functionName($$$)' --lang js . - Find class definitions:
ast-grep --pattern 'class $NAME { $$$ }' --lang ts . - Find variable assignments:
ast-grep --pattern '$VAR = $$$' --lang ruby . - Find import statements:
ast-grep --pattern 'import { $$$ } from "$MODULE"' --lang js . - Find method calls on objects:
ast-grep --pattern '$OBJ.$METHOD($$$)' --lang ts . - Find React hooks:
ast-grep --pattern 'const [$STATE, $SETTER] = useState($$$)' --lang tsx . - Find Ruby class definitions:
ast-grep --pattern 'class $NAME < $$$; $$$; end' --lang ruby .
Pattern Syntax Reference
$VAR— matches any single node and captures it$$$— matches zero or more nodes (wildcard)$$— matches one or more nodes- Literal code — matches exactly as written
Supported Languages
Complete list of ast-grep supported languages (25 total):
System Programming:
- C:
c - C++:
cpp - C#:
csharp - Go:
go - Rust:
rust
Web Development:
- JavaScript:
javascript(aliases:js,jsx) - TypeScript:
typescript(aliases:ts) - TSX:
tsx - HTML:
html - CSS:
css - PHP:
php
General Purpose:
- Python:
python - Java:
java - Kotlin:
kotlin - Ruby:
ruby - Swift:
swift - Scala:
scala - Lua:
lua - Elixir:
elixir - Haskell:
haskell
Configuration & Data:
- JSON:
json - YAML:
yaml
Other:
- Bash:
bash - Nix:
nix - Solidity:
solidity
Note: Language aliases (like
jsforjavascript) and file extension mappings can be customized using thelanguageGlobsconfiguration.
Integration Workflow
Before using ast-grep:
- Check if ast-grep is installed:
If not, skip and fall back to regex/semantic search.
command -v ast-grep >/dev/null 2>&1 || echo "ast-grep not installed, skipping AST search" - Identify if the task involves structural code patterns or language-aware refactoring.
- Determine the appropriate language(s) to search.
- Construct the pattern using ast-grep syntax.
- Run ast-grep to gather precise structural information.
- Use results to inform code edits, refactoring, or further analysis.
Example Workflow
When asked to "find all Ruby service objects that call perform":
- Check for ast-grep:
command -v ast-grep >/dev/null 2>&1 && ast-grep --pattern 'perform($$$)' --lang ruby app/services/ - Analyze results structurally.
- Use codebase semantic search for additional context if needed.
- Make informed edits based on structural understanding.
Combine ast-grep with Internal Tools
- codebase_search for semantic context and documentation
- read_file for examining specific files found by ast-grep
- edit_file for making precise, context-aware code changes
Advanced Usage
- JSON output for programmatic processing:
ast-grep --pattern '$PATTERN' --lang $LANG $PATH --json - Replace patterns:
ast-grep --pattern '$OLD_PATTERN' --rewrite '$NEW_PATTERN' --lang $LANG $PATH - Interactive mode:
ast-grep --pattern '$PATTERN' --lang $LANG $PATH --interactive
Key Benefits Over Regex
- Language-aware — understands syntax and semantics
- Structural matching — finds patterns regardless of formatting
- Cross-language — works consistently across different languages
- Precise refactoring — makes structural changes safely
- Context-aware — understands code hierarchy and scope
Decision Matrix: When to Use Each Tool
| Task Type | Tool Choice | Reason | |--------------------------|----------------------|-------------------------------| | Find text patterns | grep_search | Simple text matching | | Find code structures | ast-grep | Syntax-aware search | | Understand semantics | codebase_search | AI-powered context | | Make edits | edit_file | Precise file editing | | Structural refactoring | ast-grep + edit_file | Structure + precision |
Always prefer ast-grep for code structure analysis over regex-based approaches, but only if it is installed and available.