Skip to content

Commit 8ea3017

Browse files
invalidclaude
andcommitted
feat(v0.2.4): add /codemap:prompts command to inject usage rules into CLAUDE.md
New command that reads project graph overview and writes codemap best-practice rules (auto-scan, session load, query-first search, incremental update, impact analysis, staleness detection) into the target project's CLAUDE.md. Idempotent: creates, appends, or replaces the ## CodeMap section as needed. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 69825d6 commit 8ea3017

4 files changed

Lines changed: 92 additions & 4 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ CodeMap/
193193
│ │ ├── load.md # /codemap:load
194194
│ │ ├── update.md # /codemap:update
195195
│ │ ├── query.md # /codemap:query
196-
│ │ └── impact.md # /codemap:impact
196+
│ │ ├── impact.md # /codemap:impact
197+
│ │ └── prompts.md # /codemap:prompts
197198
│ ├── skills/ # 自动触发 Skill / Auto-triggering skill
198199
│ │ └── codemap/SKILL.md # 统一入口,智能路由 / Unified entry, smart routing
199200
│ ├── hooks/ # 事件钩子 / Event hooks
@@ -285,6 +286,7 @@ The `codemap` skill auto-activates based on conversation context and intelligent
285286
| `/codemap:update` | 增量更新图谱 / Incremental update |
286287
| `/codemap:query <symbol>` | 查询符号定义和调用关系 / Query symbol definitions and call relations |
287288
| `/codemap:impact <target>` | 分析变更影响范围 / Analyze change impact |
289+
| `/codemap:prompts` | 将 codemap 使用规范注入项目 CLAUDE.md / Inject codemap usage rules into project CLAUDE.md |
288290

289291
### 典型工作流 / Typical Workflow
290292

@@ -295,6 +297,7 @@ The `codemap` skill auto-activates based on conversation context and intelligent
295297
4. 深入模块 / Dive into module: /codemap:load auth → 加载 auth 模块 (~2-5k tokens)
296298
5. 代码修改后 / After changes: /codemap:update → 增量更新图谱
297299
6. 重构前 / Before refactor: /codemap:impact auth → 查看影响范围
300+
7. 注入规范 / Inject rules: /codemap:prompts → 写入使用规范到 CLAUDE.md
298301
```
299302

300303
---

ccplugin/.claude-plugin/plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codemap",
3-
"version": "0.2.3",
3+
"version": "0.2.4",
44
"description": "AST-based code graph mapping plugin for Claude Code — scan once, persist structural graph, load compact slices to save ~95% tokens",
55
"author": {
66
"name": "killvxk",

ccplugin/commands/prompts.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
description: 将 codemap 使用规范注入到项目的 CLAUDE.md 中
3+
---
4+
5+
# CodeMap Prompts — 注入使用规范到 CLAUDE.md
6+
7+
将 codemap 的使用规范写入当前项目的 `CLAUDE.md`,使每次会话自动遵循图谱优先的工作流。
8+
9+
## 执行步骤
10+
11+
### 1. 检测图谱是否存在
12+
13+
```bash
14+
ls .codemap/graph.json 2>/dev/null && echo "CODEMAP_EXISTS" || echo "NO_CODEMAP"
15+
```
16+
17+
如果不存在,提示用户先执行 `/codemap:scan` 生成图谱,然后**中止**
18+
19+
### 2. 读取项目概览
20+
21+
使用 Read 工具读取 `.codemap/slices/_overview.json`,提取以下动态信息:
22+
23+
- `project.name` — 项目名称
24+
- `summary.totalFiles` — 源文件总数
25+
- `summary.languages` — 语言分布(如 `rust: 34, typescript: 2`
26+
- `summary.modules` — 模块列表
27+
28+
### 3. 生成规范段落
29+
30+
用上一步的动态信息填充以下模板:
31+
32+
```markdown
33+
## CodeMap
34+
35+
本项目已启用 [CodeMap](https://github.com/killvxk/CodeMap) 代码图谱。
36+
37+
**项目概览**: {project_name} | {total_files} 个源文件 | 语言: {languages} | 模块: {modules}
38+
39+
### 使用规范
40+
41+
-`.codemap/` 不存在,先执行 `/codemap:scan` 生成图谱
42+
- 会话开始时执行 `/codemap:load` 加载项目上下文
43+
- 代码结构搜索(函数、类、调用关系)优先使用 `/codemap:query`,仅当需要纯文本/正则搜索时才回退到 Grep/Glob
44+
- 代码变更后执行 `/codemap:update` 增量更新图谱
45+
- 重构前执行 `/codemap:impact <目标>` 评估影响范围
46+
- 图谱过期时(commit hash 不匹配或长时间未更新)应先更新再查询
47+
```
48+
49+
将模板中的占位符替换为实际值:
50+
- `{project_name}``project.name`
51+
- `{total_files}``summary.totalFiles`
52+
- `{languages}` → 将 `summary.languages` 格式化为 `lang1(N), lang2(N)` 的形式
53+
- `{modules}` → 将 `summary.modules` 数组用逗号连接
54+
55+
### 4. 写入 CLAUDE.md
56+
57+
检查当前项目根目录下的 `CLAUDE.md`
58+
59+
#### 情况 A:文件不存在
60+
61+
使用 Write 工具创建 `CLAUDE.md`,内容为生成的规范段落。
62+
63+
#### 情况 B:文件存在但无 `## CodeMap` 段落
64+
65+
使用 Read 工具读取现有内容,确认不包含 `## CodeMap` 行。然后用 Edit 工具在文件末尾追加规范段落(前面加一个空行分隔)。
66+
67+
#### 情况 C:文件存在且有 `## CodeMap` 段落
68+
69+
使用 Read 工具读取现有内容,定位 `## CodeMap` 行的起始位置。找到该段落的结束位置(下一个同级或更高级标题 `## ` 的前一行,或文件末尾)。用 Edit 工具将旧段落替换为新生成的段落,实现幂等更新。
70+
71+
### 5. 展示结果
72+
73+
向用户输出:
74+
75+
```
76+
✓ CodeMap 使用规范已写入 CLAUDE.md
77+
项目: {project_name}
78+
模块: {modules}
79+
规则: 6 条
80+
81+
下次会话将自动遵循图谱优先的工作流。
82+
```

ccplugin/skills/codemap/SKILL.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ description: >
66
before making code changes, when the user asks about functions/classes/modules,
77
when planning refactoring, or when .codemap/ directory exists or needs to be created.
88
Keywords: 代码图谱, 项目结构, 架构, 代码地图, 模块, 扫描, 索引, 加载, 查询,
9-
影响分析, 重构, scan, load, query, impact, update, codemap, code graph,
9+
影响分析, 重构, scan, load, query, impact, update, prompts, codemap, code graph,
1010
understand codebase, project overview, code structure, 了解代码, 开始工作,
11-
查找函数, 哪里定义, 谁调用了, 影响范围, 依赖分析, 更新图谱, 刷新.
11+
查找函数, 哪里定义, 谁调用了, 影响范围, 依赖分析, 更新图谱, 刷新,
12+
CLAUDE.md, 使用规范, 注入规则.
1213
---
1314

1415
# CodeMap — 代码图谱智能路由
@@ -47,6 +48,7 @@ ls .codemap/graph.json 2>/dev/null && echo "CODEMAP_EXISTS" || echo "NO_CODEMAP"
4748
| 谈到重构、改动影响、风险评估 | 执行 `/codemap:impact <目标>` |
4849
| 说代码改了、图谱过期、要刷新 | 执行 `/codemap:update` |
4950
| 要重新全量扫描 | 执行 `/codemap:scan` |
51+
| 想把 codemap 规范写入 CLAUDE.md | 执行 `/codemap:prompts` |
5052

5153
### Step 4: 执行路由
5254

@@ -61,3 +63,4 @@ ls .codemap/graph.json 2>/dev/null && echo "CODEMAP_EXISTS" || echo "NO_CODEMAP"
6163
| `/codemap:query <symbol>` | 查询符号定义和调用关系 |
6264
| `/codemap:update` | 增量更新图谱 |
6365
| `/codemap:impact <target>` | 分析变更影响范围 |
66+
| `/codemap:prompts` | 将 codemap 使用规范注入项目 CLAUDE.md |

0 commit comments

Comments
 (0)