ast-grep

Translate natural-language code search requests into ast-grep rule configurations.

8|Updated Mar 28, 2024
One-click install
npx skills add https://github.com/joncrangle/.dotfiles --skill ast-grep-joncrangle
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ast-grep
Source: https://github.com/joncrangle/.dotfiles/tree/main/dot_config/opencode/skills/ast-grep
Command: npx skills add https://github.com/joncrangle/.dotfiles --skill ast-grep-joncrangle

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Structural code search can be tedious when queries must account for AST patterns rather than plain text. ast-grep translates natural-language requests into precise AST-rule configurations that match code structure.

Core Features & Use Cases

  • Rule-based matching with atomic, relational, and composite rules to express complex patterns.
  • Language-agnostic structure using Tree-sitter ASTs and metavariables for flexible captures.
  • Use cases include finding specific language constructs, refactoring opportunities, or enforcing code patterns across a codebase.

Quick Start

Describe a code pattern in natural language and I will generate an ast-grep rule to match its AST structure.

Frequently Asked Questions about ast-grep

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I search for code patterns using AST structures instead of plain text?

AST-based code search matches tree-sitter structures rather than text strings. You define YAML rules with metavariables to locate specific language constructs or refactoring opportunities across your codebase.

What is the best way to find specific language constructs across a codebase?

Finding specific language constructs requires translating structural patterns into ast-grep YAML rules. These rules utilize tree-sitter ASTs and metavariables to flexibly capture and match code structures across multiple languages.

How do I write YAML rules with metavariables for structural code search?

Writing YAML rules for structural code search involves defining atomic, relational, and composite rules. You use metavariables within the rule configuration to express complex AST patterns and capture code structures.

Can I use tree-sitter ASTs to enforce code patterns across multiple languages?

Tree-sitter ASTs enable language-agnostic structural code search. By defining pattern rules with metavariables, you can enforce code patterns and locate refactoring opportunities across different programming languages.

Does structural code search support relational and composite rules for complex patterns?

Structural code search supports rule-based matching with atomic, relational, and composite rules. This allows you to express complex AST patterns and accurately locate structural code matches within a codebase.

Why use AST-based code search instead of regular text search for refactoring?

AST-based code search identifies refactoring opportunities by matching structural patterns rather than plain text. Using tree-sitter ASTs and metavariables ensures accurate pattern matching across codebases, avoiding false positives from text searches.