第 9 项。原报告 4.5 判断「编辑器无右键菜单」是错的:框架 ui/editor 自带完整菜单(跳转定义、剪切/复制/粘贴、全选、切换注释、选区/转换/ 折叠三个子菜单),State.ContextMenu 只是留给应用层的追加点。所以本次 不重造菜单,只往那个追加点里填应用层独有的 16 项: - gopls:代码动作、查找引用、重命名符号(要求 Go 文件 + LSP 在线, 改文本的还要求非只读) - 编辑:格式化文档、在文件中查找 - 测试:运行光标处的测试 / 当前包(只在 _test.go 出现,普通文件里挂 一个永远点不动的项是噪音,而它一旦可点又会退化成跑全部测试) - 定位:在项目树中定位、在资源管理器中显示、本地历史 - 剪贴板:复制相对路径 / 绝对路径 菜单项造在纯函数 editorMenuItems(editorMenuCap) 里,可用性由 goFile/lspReady/inRoot/onDisk/readOnly 五个事实决定;条件插入容易留下 连着两条分隔线或末尾挂一条,统一由 compactEditorMenu 收口。 验证不止单测:TestEditorMenuShowsInEditor 用 ui.NewTester 真把编辑器 渲染出来、真右击、读 tt.Menu() 的菜单文案,再 ChooseMenuItem 走完 「菜单 → 动作 → 剪贴板」整条链路,并断言灰掉的项点不动。框架文档明确 说右键菜单不能用 automation 插座驱动,所以走 Tester 这条路。 顺带一次 gofmt -w 把全仓 gofmt -l 清空(13 个文件,多是 CRLF 与文档 注释空格),并更正报告 4.5、补齐执行状态。 其余"本周做"项(回收站删除、参数引号切分 + Env/WorkDir、日志轮转、 标签同名区分)一并入库。
133 lines
4.8 KiB
Go
133 lines
4.8 KiB
Go
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
|
||
}
|