feat: 补全文档中文翻译(LLM,OpenAI 兼容接口)
补全列表底部的英文 godoc 翻译成中文: - internal/ai: OpenAI 兼容最小客户端 + TranslateDoc(保留代码标识符) - app 层 docTranslation: 内存+磁盘缓存、inflight 去重、失败哨兵防重复请求 - 自定义补全面板渲染(RenderCompletions 钩子),文档区异步换译文, 不重送候选列表、不打断选择状态 - 设置新增「文档翻译」:开关 + 接口地址/API Key/模型 三件套, 兼容 DeepSeek/GLM/Ollama/OpenAI 等,没配齐时静默关闭
This commit is contained in:
1 parent
83abecd607
commit
f401c3de85
7 files changed
+467
No files matched your search
@@ -2048,6 +2048,49 @@ UTF16 列精确手算,越界位置 gopls 的行为不可预期。
|
||||
| `go test ./... -timeout 500s` | ✓ 全绿 |
|
||||
| windowsgui 构建 | ✓ 22.4MB |
|
||||
|
||||
## 64. 补全文档中文翻译(2026-11-04)
|
||||
|
||||
### 64.1 需求与方案
|
||||
|
||||
用户看到补全列表底部的英文 godoc(Append formats using...),要求翻译成中文。
|
||||
翻译必须 LLM(词典/规则对代码文档不可用)。
|
||||
|
||||
**关键机制**:编辑器补全面板提供整面板渲染钩子 `State.RenderCompletions(c, panel, m)`
|
||||
(没有「只换文档区」的粒度),且即时模式每帧重绘——正好做「异步翻译 + 缓存 +
|
||||
下一帧自动换译文」,**不打断候选选择状态**(对比:重送 ShowCompletions 会重置
|
||||
选中项与过滤)。
|
||||
|
||||
### 64.2 实现
|
||||
|
||||
| 文件 | 内容 |
|
||||
|---|---|
|
||||
| `internal/ai/llm.go`(新) | OpenAI 兼容最小客户端:`/chat/completions` 非流式 + `TranslateDoc`(系统提示:保留代码标识符、Go 社区术语、只输出译文)。兼容 DeepSeek/GLM/Moonshot/通义/OpenAI/本地 Ollama |
|
||||
| `internal/app/translate.go`(新) | `docTranslation`:主线程查缓存(`trans` map),miss 则后台翻译(`transInflight` 去重),先返回原文;失败缓存空串哨兵(同会话不再烧 API,重启可重试);缓存持久化 `translations.json`(跨工作区,启动装载,标准库文档固定 → 重启秒出译文) |
|
||||
| `internal/app/completion.go`(新) | `renderCompletions` 复刻 `DefaultCompletions`(列表交互全在 CompletionMenu,不丢功能),文档区换成 `docTranslation(...)` |
|
||||
| `internal/app/settings.go` | 新字段:`TranslateDocs`(默认开)/ `AIBaseURL` / `AIAPIKey` / `AIModel`;设置弹窗加「文档翻译」区(开关 + 三件套输入框 + 示例提示) |
|
||||
| `internal/app/app.go` | App 加 `trans`/`transInflight`;`newDoc` 挂 `d.st.RenderCompletions`;`New()` 装载译文缓存 |
|
||||
|
||||
时序安全:`docTranslation` 在渲染帧(主线程)读写 map;后台 goroutine 只做 HTTP,
|
||||
结果经 `win.Update` 回主线程写缓存。三件套没配齐时功能静默关闭(`translateOn`)。
|
||||
|
||||
### 64.3 使用
|
||||
|
||||
设置 → 文档翻译:填接口地址 + API Key + 模型。示例:
|
||||
- DeepSeek:`https://api.deepseek.com/v1` + `deepseek-chat`
|
||||
- 本地 Ollama:`http://localhost:11434/v1`(Key 随便填)
|
||||
|
||||
### 64.4 验证
|
||||
|
||||
| 项 | 结果 |
|
||||
|---|---|
|
||||
| `internal/ai` 单测(httptest 模拟服务端:请求装配/鉴权头/响应解析/错误透出/翻译提示) | ✓ 4 个全过 |
|
||||
| `go test ./...` | ✓ 全绿 |
|
||||
| windowsgui 构建 | ✓ 22.4MB |
|
||||
| 真实效果 | 待用户填 Key 后截图确认(弹出的文档先英文,1-2 秒后换中文) |
|
||||
|
||||
后续可做:hover 悬停文档接同一个翻译服务(ShowHover 同样是静态文本+可重发)。
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user