feat: hover 悬停文档中文翻译
补全的翻译只接了 RenderCompletions,悬停卡片还是英文。本轮接上 RenderHover 钩子,与补全共用同一翻译缓存: - splitHoverDoc 按行拆分:func 签名行、pkg.go.dev 链接行、空行 原样保留,只把正文段落送去翻译,译文插回原位置 - 诊断消息保留英文(错误原文翻坏反而误事) - 未配置/翻译中/失败时显示英文原文 - 4 个单测覆盖拆行、拼回、未配置、翻译中四个场景
This commit is contained in:
1 parent
f401c3de85
commit
a7d53f25d4
4 files changed
+193
No files matched your search
@@ -1,6 +1,8 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
"github.com/egoist/mygo/ui/editor"
|
||||
)
|
||||
@@ -50,3 +52,81 @@ func (a *App) renderCompletions(c *ui.Context, panel ui.Element, m *editor.Compl
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
Reference in new issue
Block a user