补全列表底部的英文 godoc 翻译成中文: - internal/ai: OpenAI 兼容最小客户端 + TranslateDoc(保留代码标识符) - app 层 docTranslation: 内存+磁盘缓存、inflight 去重、失败哨兵防重复请求 - 自定义补全面板渲染(RenderCompletions 钩子),文档区异步换译文, 不重送候选列表、不打断选择状态 - 设置新增「文档翻译」:开关 + 接口地址/API Key/模型 三件套, 兼容 DeepSeek/GLM/Ollama/OpenAI 等,没配齐时静默关闭
53 lines
2.2 KiB
Go
53 lines
2.2 KiB
Go
package app
|
||
|
||
import (
|
||
"github.com/egoist/mygo/ui"
|
||
"github.com/egoist/mygo/ui/editor"
|
||
)
|
||
|
||
// completionRows 是候选列表最多显示的行数(与编辑器默认一致)。
|
||
const completionRows = 200
|
||
|
||
// renderCompletions 复刻编辑器默认的补全面板,把底部文档换成中文译文:
|
||
// 选中文档时异步发起 LLM 翻译(带缓存),译好后的下一帧自动显示。
|
||
// 没配 AI 或还没译回来就显示英文原文,不阻塞选择。
|
||
//
|
||
// 为什么整面板接管:编辑器只给了整面板的渲染钩子(RenderCompletions),
|
||
// 没有「只换文档区」的粒度;列表部分照抄 DefaultCompletions,
|
||
// 选择/点击/滚动的交互都还在 m(CompletionMenu)里,不丢功能。
|
||
func (a *App) renderCompletions(c *ui.Context, panel ui.Element, m *editor.CompletionMenu) {
|
||
t := c.Theme()
|
||
ct := m.Theme()
|
||
font := m.Font()
|
||
lh := m.LineHeight()
|
||
panel.Background(t.Surface).Border(1, t.Border).Radius(t.Radius).Shadow(0, 4, 16, 0, ui.RGBA(0, 0, 0, 0.2)).
|
||
Padding(t.Space(1)).MaxHeight(lh*9 + t.Space(2)).MinWidth(t.Space(60)).MaxWidth(t.Space(150))
|
||
items := m.Items()
|
||
panel.Children(func() {
|
||
ui.Scroll(c).Key("rows").MaxHeight(lh * 9).Children(func() {
|
||
for i, it := range items {
|
||
if i >= completionRows {
|
||
break
|
||
}
|
||
row := m.Item(i).Row().Justify(ui.Start).Gap(t.Space(2)).AlignItems(ui.Center).Padding(t.Space(0.5), t.Space(1.5)).
|
||
Radius(t.Radius - 2).Height(lh + 2)
|
||
if i == m.Selected() {
|
||
row.Background(t.Accent.Alpha(0.18))
|
||
}
|
||
row.Children(func() {
|
||
ui.Text(c, it.Kind.Icon()).Font("monospace").FontSize(font.Size - 1).TextColor(ct.TokenColor(it.Kind.Token())).Width(t.Space(3))
|
||
ui.Text(c, it.Label).Font(font.Family).FontSize(font.Size).TextColor(ct.Text).SingleLine().Shrink(1)
|
||
if it.Detail != "" {
|
||
ui.Text(c, it.Detail).FontSize(font.Size - 1).TextColor(t.TextMuted).SingleLine().Shrink(1).Grow(1).TextAlign(ui.End)
|
||
}
|
||
})
|
||
}
|
||
})
|
||
if s := m.Selected(); s < len(items) && items[s].Documentation != "" {
|
||
ui.Box(c).Height(1).Background(t.Border).Margin(t.Space(1), 0)
|
||
text := a.docTranslation(items[s].Documentation)
|
||
ui.Text(c, text).FontSize(t.FontSize - 1).TextColor(t.TextMuted).MaxWidth(t.Space(140)).Padding(t.Space(0.5), t.Space(1.5))
|
||
}
|
||
})
|
||
}
|