AgentSkillsCN

codemap

利用结构化索引高效导航代码库。在查找符号定义(类、函数、方法)、探索文件结构,或按名称定位代码时使用。通过有针对性地进行行区间读取而非整文件扫描,可将令牌消耗降低60%至80%。

SKILL.md
--- frontmatter
name: codemap
description: Navigate codebases efficiently using structural indexes. Use when finding symbol definitions (classes, functions, methods), exploring file structure, or locating code by name. Reduces token consumption by 60-80% through targeted line-range reads instead of full file scans.
allowed-tools: Read, Bash, Glob, Grep
user-invocable: true

CodeMap - Codebase Structural Index

Navigate codebases efficiently using pre-built structural indexes stored in .codemap/ directories.

When to Use This Skill

USE CodeMap when:

  • Finding where a class, function, method, or type is defined
  • Understanding a file's structure before reading it
  • Searching for symbols by name
  • Reducing token usage during codebase exploration

READ full files when:

  • Understanding implementation details
  • Making edits to code
  • The symbol isn't in the index (new/untracked file)

Quick Reference

bash
# Find a symbol by name (case-insensitive)
codemap find "SymbolName"

# Filter by type
codemap find "handle" --type method
codemap find "User" --type class
codemap find "Config" --type interface

# Show file structure with all symbols
codemap show path/to/file.ts

# Check if index is up-to-date
codemap validate

# View index statistics
codemap stats

Workflow: Finding Code

Step 1: Find Symbol Location

bash
codemap find "UserService"

Output:

code
src/services/user.ts:15-189 [class] UserService
  (config: Config)

Step 2: Read Only Relevant Lines

Instead of reading the entire file, read just lines 15-189:

code
Read src/services/user.ts lines 15-189

Step 3: Explore Nested Symbols

bash
codemap show src/services/user.ts

Output:

code
File: src/services/user.ts (hash: a3f2b8c1)
Lines: 542
Language: typescript

Symbols:
- UserService [class] L15-189
  - constructor [method] L20-35
  - getUser [method] L37-98
    (userId: string) : Promise<User>
  - createUser [async_method] L100-145
    (data: CreateUserDto) : Promise<User>

Symbol Types

TypeDescription
classClass declaration
functionFunction declaration
methodClass method
async_functionAsync function
async_methodAsync class method
interfaceTypeScript interface
typeTypeScript type alias
enumEnum declaration

Index Structure

The .codemap/ directory mirrors the project structure:

code
.codemap/
├── .codemap.json              # Root manifest
├── _root.codemap.json         # Files in project root
├── src/
│   ├── .codemap.json          # Files in src/
│   └── services/
│       └── .codemap.json      # Files in src/services/

Direct JSON Access

For programmatic access, read the JSON files directly:

bash
cat .codemap/src/services/.codemap.json

Validation

Before trusting cached line numbers (especially after context compaction):

bash
codemap validate path/to/file.ts
  • "up to date": Line ranges are valid
  • "stale": File changed, re-read or run codemap update

Setup (If Not Initialized)

If a project doesn't have a .codemap/ directory:

Prerequisites

  • Python 3.10+ and pip must be installed
  • Verify with: python3 --version && pip --version

Installation

bash
# Install codemap from GitHub (NOT from PyPI - there's a different package there)
pip install git+https://github.com/AZidan/codemap.git

# Initialize index
codemap init .

# Optional: Install git hooks for auto-updates
codemap install-hooks

IMPORTANT: Do NOT use pip install codemap - that installs a different package from PyPI. Always use the GitHub URL above.

Best Practices

  1. Search before scanning: Always try codemap find before grep/glob
  2. Use line ranges: Read specific line ranges instead of full files
  3. Check freshness: Use codemap validate before trusting cached line numbers
  4. Explore structure first: Use codemap show to understand file layout before diving in

Example Session

Task: "Fix the authentication bug in the login handler"

bash
# 1. Find relevant symbols
codemap find "login"
# → src/auth/handlers.ts:45-92 [function] handleLogin

# 2. Check file structure
codemap show src/auth/handlers.ts
# Shows handleLogin and related functions with line ranges

# 3. Read only the relevant function (lines 45-92)
# ... make your fix ...

# 4. If you need related code, find it
codemap find "validateToken"
# → src/auth/utils.ts:12-38 [function] validateToken