package app import ( "strings" "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)) } }) } // renderHover 复刻编辑器默认悬停卡片,把文档正文换成中文译文。 // 诊断消息保留英文:错误原文里的标识符/术语翻坏了反而误事。 func (a *App) renderHover(c *ui.Context, panel ui.Element, h *editor.HoverCard) { t := c.Theme() ct := h.Theme() 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(2)).Gap(t.Space(1.5)).MaxWidth(t.Space(150)).Margin(0, 0, t.Space(1), 0).Selectable() panel.Children(func() { for _, d := range h.Diagnostics() { ui.Row(c).Gap(t.Space(1.5)).Children(func() { ui.Box(c).Width(3).Radius(1.5).Background(ct.SeverityColor(d.Severity)).AlignSelf(ui.Stretch) msg := d.Message if d.Source != "" { msg += " (" + d.Source + ")" } ui.Text(c, msg).FontSize(t.FontSize - 1).Shrink(1) }) } if text := h.Text(); text != "" { ui.Text(c, a.hoverTranslation(text)).Font(h.Font().Family).FontSize(h.Font().Size).TextColor(ct.Text) } h.Content() }) } // hoverTranslation 翻译悬停文档:签名行(func ...)与 pkg.go.dev 链接行 // 原样保留,只把正文段落送去翻译,再按原行序拼回。 // 没有译文时(未配置 / 翻译中 / 失败)返回原文。 func (a *App) hoverTranslation(text string) string { docJoined, docFirst, protected := splitHoverDoc(text) if docFirst < 0 || !a.translateOn() { return text } tr := a.docTranslation(docJoined) if tr == docJoined { // docTranslation 只有拿到译文才返回不同的串,其余场景都应显示原文。 return text } var out []string for i, ln := range protected.lines { switch { case i == docFirst: out = append(out, tr) case !protected.mask[i]: out = append(out, ln) } } return strings.Join(out, "\n") } // hoverSplit 是 splitHoverDoc 的结果:原始行、需翻译行合并后的文本、 // 第一个正文行的下标、每行是否需要翻译。 type hoverSplit struct { lines []string mask []bool } // splitHoverDoc 拆悬停文本:func 开头的签名行、含 ](http 的链接行、空行 // 都原样保留,其余正文行合并成一段送去翻译。 func splitHoverDoc(text string) (string, int, hoverSplit) { lines := strings.Split(text, "\n") hs := hoverSplit{lines: lines, mask: make([]bool, len(lines))} var doc []string first := -1 for i, ln := range lines { s := strings.TrimSpace(ln) if s == "" || strings.HasPrefix(s, "func ") || strings.Contains(s, "](http") { continue } hs.mask[i] = true if first < 0 { first = i } doc = append(doc, s) } return strings.Join(doc, "\n"), first, hs }