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
@@ -536,6 +536,8 @@ func (a *App) newDoc(abs, text string) *Doc {
|
||||
d.st.OnDefinition = func(off int) { a.definition(d, off) }
|
||||
// 补全面板自定义渲染:底部文档接 LLM 中文翻译(见 completion.go)。
|
||||
d.st.RenderCompletions = a.renderCompletions
|
||||
// 悬停卡片同理:文档正文翻译,签名与链接行保留。
|
||||
d.st.RenderHover = a.renderHover
|
||||
// 编辑器级的快捷键在这里拦。OnInput 在编辑器处理之前收到按键,
|
||||
// 返回 true 表示已消费。
|
||||
//
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const hoverSample = "func fmt.Println(a ...any) (n int, err error)\n" +
|
||||
"Println formats using the default formats for its operands and writes to standard output.\n" +
|
||||
"Spaces are always added between operands and a newline is appended.\n" +
|
||||
"\n" +
|
||||
"[`fmt.Println` on pkg.go.dev](https://pkg.go.dev/fmt#Println)"
|
||||
|
||||
// TestSplitHoverDoc 验证悬停文本的拆行保护:签名行与链接行不进翻译段。
|
||||
// 结构对齐 gopls 的真实 hover 输出(markdownToText 之后)。
|
||||
func TestSplitHoverDoc(t *testing.T) {
|
||||
joined, first, hs := splitHoverDoc(hoverSample)
|
||||
|
||||
want := "Println formats using the default formats for its operands and writes to standard output.\n" +
|
||||
"Spaces are always added between operands and a newline is appended."
|
||||
if joined != want {
|
||||
t.Fatalf("翻译段不对:\n got %q\nwant %q", joined, want)
|
||||
}
|
||||
if first != 1 {
|
||||
t.Fatalf("第一个正文行 = %d, want 1", first)
|
||||
}
|
||||
wantMask := []bool{false, true, true, false, false}
|
||||
if len(hs.mask) != len(wantMask) {
|
||||
t.Fatalf("行数 = %d, want %d", len(hs.mask), len(wantMask))
|
||||
}
|
||||
for i, m := range wantMask {
|
||||
if hs.mask[i] != m {
|
||||
t.Errorf("行 %d (%q) mask = %v, want %v", i, hs.lines[i], hs.mask[i], m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestHoverTranslationAssembly 验证拼回:译文插到正文位置,签名与链接原样。
|
||||
// 预填翻译缓存绕过 LLM 与窗口依赖。
|
||||
func TestHoverTranslationAssembly(t *testing.T) {
|
||||
a := &App{trans: map[string]string{
|
||||
"Println formats using the default formats for its operands and writes to standard output.\nSpaces are always added between operands and a newline is appended.":
|
||||
"Println 用其操作数的默认格式进行格式化并写入标准输出,操作数之间总会添加空格,并附加换行。",
|
||||
}, transInflight: map[string]bool{}, set: settings{TranslateDocs: true, AIBaseURL: "x", AIAPIKey: "y", AIModel: "z"}}
|
||||
|
||||
got := a.hoverTranslation(hoverSample)
|
||||
want := "func fmt.Println(a ...any) (n int, err error)\n" +
|
||||
"Println 用其操作数的默认格式进行格式化并写入标准输出,操作数之间总会添加空格,并附加换行。\n" +
|
||||
"\n" +
|
||||
"[`fmt.Println` on pkg.go.dev](https://pkg.go.dev/fmt#Println)"
|
||||
if got != want {
|
||||
t.Fatalf("拼回结果不对:\n got %q\nwant %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHoverTranslationUnconfigured 未配置时必须原样返回。
|
||||
func TestHoverTranslationUnconfigured(t *testing.T) {
|
||||
a := &App{trans: map[string]string{}, transInflight: map[string]bool{}}
|
||||
if got := a.hoverTranslation(hoverSample); got != hoverSample {
|
||||
t.Fatalf("未配置应返回原文, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestHoverTranslationInProgress 翻译中(缓存未命中且已在请求里)返回原文。
|
||||
func TestHoverTranslationInProgress(t *testing.T) {
|
||||
a := &App{trans: map[string]string{}, transInflight: map[string]bool{}, set: settings{TranslateDocs: true, AIBaseURL: "x", AIAPIKey: "y", AIModel: "z"}}
|
||||
joined, _, _ := splitHoverDoc(hoverSample)
|
||||
a.transInflight[joined] = true
|
||||
a.win = nil // 若误触后台请求立即暴露;docTranslation 发请求前不应用到 win
|
||||
got := a.hoverTranslation(hoverSample)
|
||||
if !strings.Contains(got, "Println formats") {
|
||||
t.Fatalf("翻译中应显示原文, got %q", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user