find_code
Search code in a project folder using AST patterns to locate specific code structures across multiple programming languages.
Instructions
Find code in a project folder that matches the given ast-grep pattern.
Pattern is good for simple and single-AST node result.
For more complex usage, please use YAML by find_code_by_rule.
Internally calls: ast-grep run --pattern [--json] <project_folder>
Output formats:
text (default): Compact text format with file:line-range headers and complete match text Example: Found 2 matches:
path/to/file.py:10-15 def example_function(): # function body return result
path/to/file.py:20-22 def another_function(): pass
json: Full match objects with metadata including ranges, meta-variables, etc.
The max_results parameter limits the number of complete matches returned (not individual lines). When limited, the header shows "Found X matches (showing first Y of Z)".
Example usage: find_code(pattern="class $NAME", max_results=20) # Returns text format find_code(pattern="class $NAME", output_format="json") # Returns JSON with metadata
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| project_folder | Yes | The absolute path to the project folder. It must be absolute path. | |
| pattern | Yes | The ast-grep pattern to search for. Note, the pattern must have valid AST structure. | |
| language | No | The language of the code. Supported: bash, c, cpp, csharp, css, elixir, go, haskell, html, java, javascript, json, jsx, kotlin, lua, nix, php, python, ruby, rust, scala, solidity, swift, tsx, typescript, yaml. If not specified, will be auto-detected based on file extensions. | |
| max_results | No | Maximum results to return | |
| output_format | No | 'text' or 'json' | text |