home / skills / brettatoms / agent-skills / code-symbols

code-symbols skill

/code-symbols

This skill uses ast-grep to search and edit code symbols across languages, enabling finding definitions, usages, and renaming.

npx playbooks add skill brettatoms/agent-skills --skill code-symbols

Review the files below or copy the command above to add this skill to your agents.

Files (4)
SKILL.md
3.6 KB
---
name: code-symbols
description: Find and edit symbols in code using ast-grep (tree-sitter based). Use when finding function definitions, class definitions, symbol usages, renaming symbols, or replacing code in JavaScript, TypeScript, Python, Go, Rust, and other languages. NOT for Clojure - use clj-symbols instead.
allowed-tools: Bash, Read, Edit, Task
---

# Code Symbols Skill

Use **ast-grep (`sg`)** for structural code search based on tree-sitter parsing. Works with 31 languages.

**Important:** Clojure is NOT supported. Use the `clj-symbols` skill for Clojure code.

## Prerequisites

```bash
# npm
npm install --global @ast-grep/cli

# Homebrew
brew install ast-grep

# Cargo
cargo install ast-grep --locked
```

Verify: `ast-grep --version` or `sg --version`

## Supported Languages

| Category | Languages |
|----------|-----------|
| **Web** | JavaScript, TypeScript, HTML, CSS, JSON |
| **Systems** | C, C++, Go, Rust |
| **Scripting** | Python, Ruby, PHP, Lua, Bash |
| **JVM** | Java, Kotlin, Scala |
| **Other** | Swift, Haskell, Elixir, Nix |

**Not supported:** Clojure, Erlang, R, MATLAB

## Quick Reference

| Task | Command |
|------|---------|
| Find pattern | `ast-grep run --pattern 'PATTERN' --lang LANG path/` |
| Find with JSON | `ast-grep run --pattern 'PATTERN' --lang LANG path/ --json` |
| Rename symbol | `ast-grep run --pattern 'old' --rewrite 'new' --lang LANG path/ --update-all` |

## Pattern Syntax

Patterns must be **valid code** that tree-sitter can parse.

### Meta-Variables

| Syntax | Meaning | Example |
|--------|---------|---------|
| `$NAME` | Match single node | `function $NAME() {}` |
| `$$$` | Match zero or more nodes | `function foo($$$)` |
| `$_` | Match but don't capture | `$_($$$)` |

### Basic Examples

```bash
# Match any function call
ast-grep run --pattern '$FUNC($$$)' --lang js path/

# Match specific function
ast-grep run --pattern 'myFunction($$$)' --lang js path/

# Match variable assignment
ast-grep run --pattern 'const $NAME = $VALUE' --lang js path/
```

## Common Operations

### Find Function Definition

```bash
# JavaScript
ast-grep run --pattern 'function $NAME($$$) { $$$ }' --lang js path/

# Python
ast-grep run --pattern 'def $NAME($$$):' --lang py path/

# Go
ast-grep run --pattern 'func $NAME($$$)' --lang go path/
```

### Find Symbol Usages

```bash
ast-grep run --pattern 'myFunction($$$)' --lang js path/
ast-grep run --pattern '$OBJ.myMethod($$$)' --lang js path/
```

### Rename Symbol

```bash
# Preview
ast-grep run --pattern 'oldName' --rewrite 'newName' --lang js src/

# Apply
ast-grep run --pattern 'oldName' --rewrite 'newName' --lang js src/ --update-all

# Interactive
ast-grep run --pattern 'oldName' --rewrite 'newName' --lang js src/ --interactive
```

### JSON Output

```bash
ast-grep run --pattern '$FUNC($$$)' --lang js path/ --json
```

Parse with jq:
```bash
ast-grep run --pattern 'function $NAME($$$) { $$$ }' --lang js path/ --json \
  | jq '.[] | {name: .metaVariables.single.NAME.text, file, line: .range.start.line}'
```

## When to Use ast-grep vs ripgrep

| Scenario | Tool |
|----------|------|
| Find by **name** (text pattern) | ripgrep (`rg`) |
| Find by **structure** (function, class) | ast-grep |
| Language-specific patterns | ast-grep |
| Maximum speed | ripgrep |
| Accurate symbol boundaries | ast-grep |

## Additional References

- **Language patterns**: See [references/languages.md](references/languages.md) for JS, TS, Python, Go, Rust patterns
- **Editing**: See [references/editing.md](references/editing.md) for rewrite, rename, and YAML rules
- **Workflows**: See [references/workflows.md](references/workflows.md) for common workflows

Overview

This skill uses ast-grep (tree-sitter based) to find and edit symbols across many programming languages. It locates function and class definitions, symbol usages, and performs structural renames or code rewrites. Use it for language-aware searches and automated, syntax-safe edits in JavaScript, TypeScript, Python, Go, Rust, and more. Clojure is not supported by this skill.

How this skill works

The skill runs ast-grep (sg) patterns that are valid code snippets parsed by tree-sitter. Patterns can include meta-variables to capture nodes, match zero-or-more nodes, or ignore matches, and results can be emitted as JSON for tooling. It supports previewing matches, performing automated rewrites (rename or replace), and updating files interactively or in batch.

When to use it

  • Find all function or class definitions by structure rather than name
  • Locate symbol usages across a codebase with correct syntax boundaries
  • Rename a symbol safely across files with a structural rewrite
  • Extract or replace code patterns that depend on AST shape
  • Generate structured JSON output for further automation or reporting

Best practices

  • Install ast-grep globally and verify with `ast-grep --version` before running searches
  • Write patterns as valid source code for the target language so tree-sitter can parse them
  • Preview matches first using --json or dry-run flags before applying --update-all
  • Use meta-variables ($NAME, $$$, $_) to capture or ignore nodes and avoid overbroad matches
  • Restrict searches to relevant directories or file extensions to reduce noise and speed up runs

Example use cases

  • Find every JavaScript function declaration and extract names into a report using --json and jq
  • Rename a Python function across a repo with a structural pattern and --update-all to ensure syntax-safe replacement
  • Search for Go method usages on a receiver type and refactor their calls
  • Locate and replace a repeated JSX/TSX pattern across a frontend codebase using tree-sitter patterns
  • Identify Rust trait implementations and list files and line numbers for audit

FAQ

Which languages are supported?

ast-grep supports dozens of languages including JavaScript, TypeScript, Python, Go, Rust, Java, Kotlin, C, C++, Swift, and more; Clojure is not supported by this skill.

How do I rename a symbol safely?

Run ast-grep with --pattern for the old symbol and --rewrite for the new one. Preview results first, then apply changes with --update-all or use --interactive for manual confirmation.