Skip to content

Commit af6c73f

Browse files
committed
fix: Convert not update mappings.json id bug
--bug=1
1 parent d65f8b8 commit af6c73f

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed

cmd/codei18n/convert.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ func runConvert() {
9393
for _, file := range files {
9494
processFile(file, adapter, store, cfg)
9595
}
96+
97+
if err := store.Save(); err != nil {
98+
log.Error("保存映射文件失败: %v", err)
99+
} else {
100+
log.Info("Mapping store updated.")
101+
}
96102
}
97103

98104
func processFile(file string, adapter *golang.Adapter, store *mapping.Store, cfg *config.Config) {
@@ -161,6 +167,25 @@ func processFile(file string, adapter *golang.Adapter, store *mapping.Store, cfg
161167
targetText = enText
162168
found = true
163169
log.Info("Found by reverse lookup: ID=%s, ZH='%s' -> EN='%s'", id, zhText, enText)
170+
171+
// Key fix: When reverting to English, automatically generate IDs for new English comments and migrate existing translations to them.
172+
// This way, subsequent scan/map updates can directly recognize this English comment and will not treat it as an untranslated new comment.
173+
174+
// 1. Construct the English version of the Comment object (simulating the converted state)
175+
tempC := *c
176+
tempC.SourceText = enText // Assuming mapping stores plain text or marked text, Normalize will process it
177+
178+
// 2. Calculate new ID
179+
newID := utils.GenerateCommentID(&tempC)
180+
181+
// 3. If the new ID differs from the old ID (it definitely will, because the text has changed), then migrate the data
182+
if newID != id {
183+
log.Info("Migrating mapping for restored English comment: %s -> %s", id, newID)
184+
// Ensure the new ID has bilingual data
185+
store.Set(newID, cfg.SourceLanguage, enText)
186+
store.Set(newID, cfg.LocalLanguage, zhText)
187+
}
188+
164189
break
165190
}
166191
}

docs/jetbrains-plugin-guide.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# CodeI18n JetBrains IDE 插件开发指南
2+
3+
本文档为 CodeI18n JetBrains IDE 插件(如 GoLand, IntelliJ IDEA)的开发提供技术指导。该插件的核心职责是作为 CodeI18n CLI 工具的 GUI 前端,负责渲染注释翻译、触发翻译任务以及管理配置。
4+
5+
## 1. 架构概览
6+
7+
插件采用 **CLI 驱动架构**。所有核心逻辑(AST 解析、ID 生成、翻译管理)均由 `codei18n` CLI 处理,插件专注于 UI 渲染和进程交互。
8+
9+
```mermaid
10+
graph TD
11+
A[JetBrains Plugin] -->|Spawn Process| B[CodeI18n CLI]
12+
B -->|JSON Output| A
13+
A -->|"Stdin (Source Code)"| B
14+
B -->|Read/Write| C["Local Mappings (.codei18n/mappings.json)"]
15+
B -->|API Call| D["Translation Service (OpenAI/Google)"]
16+
```
17+
18+
## 2. 核心功能实现指南
19+
20+
### 2.1 初始化与配置检测
21+
22+
插件加载时,应检查项目根目录下是否存在 `.codei18n/config.json`
23+
24+
- **未初始化**: 提示用户运行初始化向导。
25+
- **Action**: 调用 `codei18n init --source-lang <src> --target-lang <target>`
26+
- **已初始化**: 读取配置,准备运行环境。
27+
28+
### 2.2 Inlay Hints (内嵌提示)
29+
30+
这是插件最核心的功能,用于在代码编辑器中直接显示中文注释。
31+
32+
#### 实现机制
33+
使用 IntelliJ Platform 的 `InlayHintsProvider` (或 `InlayModel`)。
34+
35+
#### 数据获取流程
36+
1. **监听文件变更**: 当用户停止输入(Debounce 300ms-500ms)或保存文件时触发。
37+
2. **调用 CLI**:
38+
```bash
39+
# 关键参数:
40+
# --file: 必须提供,用于生成稳定的语义 ID
41+
# --stdin: 必须使用,传入编辑器当前的"脏"缓冲区内容
42+
# --format json: 获取结构化数据
43+
# --with-translations: 一次性获取 ID 和对应的翻译
44+
45+
cat <current_buffer_content> | codei18n scan --file <relative_path> --stdin --format json --with-translations
46+
```
47+
3. **解析输出**:
48+
读取 CLI 的 `stdout`(忽略 `stderr` 中的日志)。输出格式如下:
49+
```json
50+
{
51+
"file": "main.go",
52+
"comments": [
53+
{
54+
"id": "a1b2c3d4...",
55+
"range": { "startLine": 10, "startCol": 1, "endLine": 10, "endCol": 20 },
56+
"sourceText": "Calculate sum",
57+
"translation": "计算总和" // 若为空,说明尚未翻译
58+
}
59+
]
60+
}
61+
```
62+
4. **渲染 Hints**:
63+
- 遍历 `comments` 列表。
64+
- 如果 `translation` 字段不为空,在 `range.endCol` 之后(或下一行)渲染 Inlay Hint。
65+
- **样式建议**: 使用灰色斜体,前缀 `// ↓``// ` 区分。
66+
67+
### 2.3 悬停提示 (Hover Documentation)
68+
69+
除了 Inlay Hints,还可以提供鼠标悬停显示的完整翻译。
70+
71+
- **触发**: `DocumentationProvider`
72+
- **逻辑**:
73+
- 获取鼠标位置的 AST 节点。
74+
- 计算或查找对应的 Comment ID (由于 ID 生成逻辑在 CLI,建议复用 Scan 结果的缓存)。
75+
- 显示 `translation` 内容。
76+
77+
### 2.4 触发翻译任务
78+
79+
当代码中出现新注释时,用户需要触发翻译。
80+
81+
- **Action**: "Translate Missing Comments"
82+
- **CLI 调用**:
83+
```bash
84+
codei18n translate --provider <config_provider>
85+
```
86+
- **UI 反馈**:
87+
- 显示后台任务进度条。
88+
- 监听 CLI 的 stdout/stderr 获取进度。
89+
- 完成后刷新 Inlay Hints。
90+
91+
### 2.5 映射更新
92+
93+
当用户修改了源码注释后,需要更新映射文件。
94+
95+
- **Action**: "Update Mappings" (或在保存文件时自动触发)
96+
- **CLI 调用**:
97+
```bash
98+
codei18n map update
99+
```
100+
101+
## 3. 关键技术点
102+
103+
### 3.1 进程交互 (Process Handling)
104+
- 使用 `com.intellij.execution.configurations.GeneralCommandLine`
105+
- **重要**: 必须正确设置 `WorkDirectory` 为项目根目录,否则 CLI 无法找到 `.codei18n` 配置。
106+
- **环境变量**: 确保 `PATH` 中包含 `codei18n`,或允许用户配置 CLI 的绝对路径。
107+
108+
### 3.2 性能优化
109+
- **Debounce**: 不要每次按键都调用 CLI。
110+
- **缓存**: 在 IDE 内存中缓存 Scan 结果,仅在 CLI 返回新数据时更新 UI。
111+
- **并发**: `scan` 操作通常很快 (<100ms),但在大项目中仍应在后台线程执行 (ReadAction / BackgroundTask)。
112+
113+
### 3.3 错误处理
114+
- 监控 CLI 的 Exit Code。
115+
- 如果 Exit Code != 0,读取 `stderr` 并通过 Notification 提示用户错误信息(如 "OpenAI API Key invalid")。
116+
117+
## 4. 开发排期建议
118+
119+
1. **P0**: 实现 `GeneralCommandLine` 封装,能够成功调用 `codei18n --version`
120+
2. **P0**: 实现 `InlayHintsProvider`,调用 `scan` 并解析 JSON 渲染简单的文本。
121+
3. **P1**: 支持 Stdin 输入,实现实时预览。
122+
4. **P1**: 集成 `translate` 命令动作。
123+
5. **P2**: 配置页面(设置 CLI 路径、翻译语言等)。
124+
125+
## 5. 参考资源
126+
127+
- [IntelliJ Platform SDK - Inlay Hints](https://plugins.jetbrains.com/docs/intellij/inlay-hints.html)
128+
- [IntelliJ Platform SDK - Running Processes](https://plugins.jetbrains.com/docs/intellij/process-execution.html)
129+
- [CodeI18n CLI Commands](contracts/cli_commands.md)

0 commit comments

Comments
 (0)