Files
MyGolandRelease/internal/app/editor_menu.go
T
zerorigin c35564b65e 编辑器右键菜单补应用层动作,并做完评估报告"本周做"剩余项
第 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、日志轮转、
标签同名区分)一并入库。
2026-11-05 16:06:17 +08:00

249 lines
8.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package app
import (
"path/filepath"
"strings"
"github.com/egoist/mygo"
"github.com/egoist/mygo/transfer"
"github.com/egoist/mygo/ui"
"github.com/egoist/mygo/ui/editor"
"mygoland/internal/lsp"
)
// 编辑器右键菜单里应用层动作的 ID。
//
// 为什么用字符串 ID 而不是直接塞闭包:菜单项要在单测里断言(哪几项该灰、
// 哪个 ID 对应哪个快捷键),闭包里装着 *App 就没法比对了。
const (
emCodeActions = "codeActions" // 代码动作(gopls 的 quick fix)
emFindRefs = "findRefs" // 查找引用
emRename = "rename" // 重命名符号
emFormat = "format" // 格式化文档
emFind = "find" // 在文件中查找
emRunTest = "runTest" // 运行光标处的测试
emTestPkg = "testPkg" // 运行当前包的测试
emRevealTree = "revealTree" // 在项目树中定位
emRevealOS = "revealOS" // 在资源管理器中显示
emHistory = "history" // 本地历史
emCopyRel = "copyRel" // 复制相对路径
emCopyAbs = "copyAbs" // 复制绝对路径
)
// editorMenuItem 是菜单里的一项。sep 为真时是分隔线,其余字段无意义。
//
// 用自己的一份结构而不是直接造 ui.MenuItem,是为了让「该有哪些项、哪几项
// 可用」变成纯函数,能在没有窗口、没有渲染帧的测试里跑。
type editorMenuItem struct {
id string
label string
sep bool
enabled bool
}
// editorMenuCap 是决定菜单项可用与否的事实集合,由 editorMenuCap 从文档现状
// 取出来。单独一个结构体,是为了单测能任选组合,不必真造 App 与 LSP 客户端。
type editorMenuCap struct {
goFile bool // Go 文件:代码动作 / 引用 / 重命名 / 格式化
testFile bool // _test.go:才显示「运行测试」
lspReady bool // 语言服务器在线
inRoot bool // 在工作区内:相对路径与「在项目树中定位」才有意义
onDisk bool // 有绝对路径:资源管理器与本地历史才可用
readOnly bool // 只读(超大文件):会改文本的项一律灰掉
}
// attachEditorMenu 把应用层菜单项挂到文档编辑器上。
//
// 这个钩子加在框架自带菜单的**后面**:框架的 build.go 里先放跳转定义、
// 剪切/复制/粘贴、全选、切换注释,以及选区/转换/折叠三个子菜单,末尾
// `m.Separator()` 之后才调 State.ContextMenu(见 editor/build.go:278)。
// 所以这里只管补框架没有的、应用层才知道的东西——gopls 的几个请求、
// 跑测试、路径复制、定位文件。
func (a *App) attachEditorMenu(d *Doc) {
d.st.ContextMenu = func(m *ui.Menu) { a.buildEditorMenu(m, d) }
}
// buildEditorMenu 按当前文档的现状把菜单画出来。
func (a *App) buildEditorMenu(m *ui.Menu, d *Doc) {
if d == nil {
return
}
for _, it := range editorMenuItems(a.editorMenuCap(d)) {
if it.sep {
m.Separator()
continue
}
item := m.Item(it.label).Disabled(!it.enabled)
if mods, key, ok := editorMenuShortcut(it.id); ok {
item.Shortcut(mods, key)
}
// Chosen 只在「用户点完之后重跑 build」那一遍返回 true,
// 收集菜单项那一遍是 false,所以这里不会误触发。
if item.Chosen() {
a.editorMenuAction(d, it.id)
}
}
}
// editorMenuCap 读出当前文档决定菜单可用性的事实。
func (a *App) editorMenuCap(d *Doc) editorMenuCap {
return editorMenuCap{
goFile: lsp.IsGoFile(d.path),
testFile: strings.HasSuffix(d.path, "_test.go"),
lspReady: a.lsp != nil,
inRoot: a.root != "" && within(a.root, d.path),
onDisk: filepath.IsAbs(d.path),
readOnly: d.st.ReadOnly,
}
}
// editorMenuItems 产出菜单项列表。纯函数,便于单测。
//
// 测试那两项只在 _test.go 里出现——普通文件里挂着一个永远点不动的
// 「运行光标处的测试」纯属噪音,而它一旦可点又会在光标不在测试函数里时
// 偷偷退化成「跑全部测试」,那是更糟的意外。
func editorMenuItems(c editorMenuCap) []editorMenuItem {
item := func(id, label string, on bool) editorMenuItem {
return editorMenuItem{id: id, label: label, enabled: on}
}
sep := editorMenuItem{sep: true}
canGo := c.goFile && c.lspReady // gopls 在,且是 Go 文件
canEdit := canGo && !c.readOnly // 再加上「这份缓冲区可以改」
canRun := c.testFile && c.inRoot // 跑测试要有包路径,得在工作区内
items := []editorMenuItem{
item(emCodeActions, "代码动作(Alt+Enter)", canEdit),
item(emFindRefs, "查找引用(Shift+F2)", canGo),
item(emRename, "重命名符号(F2)", canEdit),
sep,
item(emFormat, "格式化文档", canEdit),
item(emFind, "在文件中查找(Ctrl+F)", true),
}
if canRun {
items = append(items,
sep,
item(emRunTest, "运行光标处的测试", true),
item(emTestPkg, "运行当前包的测试", true),
)
}
items = append(items,
sep,
item(emRevealTree, "在项目树中定位", c.inRoot),
item(emRevealOS, "在资源管理器中显示", c.onDisk),
item(emHistory, "本地历史…", c.onDisk),
sep,
item(emCopyRel, "复制相对路径", c.inRoot),
item(emCopyAbs, "复制绝对路径", c.onDisk),
)
return compactEditorMenu(items)
}
// compactEditorMenu 去掉开头、结尾和连续的分隔线。
//
// 条件插入(测试项、以及将来别的可选组)很容易留下「分隔线接分隔线」
// 或者末尾挂一条线,菜单上看就是一道多出来的横杠。统一在这里收口,
// 免得每加一个条件组都要自己数前后有没有线。
func compactEditorMenu(items []editorMenuItem) []editorMenuItem {
out := make([]editorMenuItem, 0, len(items))
for _, it := range items {
if it.sep && (len(out) == 0 || out[len(out)-1].sep) {
continue
}
out = append(out, it)
}
for len(out) > 0 && out[len(out)-1].sep {
out = out[:len(out)-1]
}
return out
}
// editorMenuShortcut 返回菜单项右侧该显示的快捷键。
//
// 只列**应用里真的绑了**的组合:这里只是画个字样,按下去由 OnInput 或
// 菜单 Accelerator 负责,写错等于骗人。
func editorMenuShortcut(id string) (ui.Modifiers, ui.Key, bool) {
switch id {
case emCodeActions:
return ui.Alt, ui.KeyEnter, true
case emFindRefs:
return ui.Shift, ui.KeyF2, true
case emRename:
return 0, ui.KeyF2, true
case emFormat:
return ui.Ctrl | ui.Alt, ui.KeyL, true
case emFind:
return ui.Cmd, ui.KeyF, true
}
return 0, 0, false
}
// editorMenuAction 执行菜单里选中(或被键盘触发)的那一项。
func (a *App) editorMenuAction(d *Doc, id string) {
if d == nil {
return
}
switch id {
case emCodeActions:
a.CodeActions()
case emFindRefs:
a.FindReferences()
case emRename:
a.RenameSymbol()
case emFormat:
a.FormatCurrent()
case emFind:
// 走 pendingCmd:菜单回调里直接 Run 会被编辑器按「正在用」拒掉,
// 和快捷键那条路一样推到下一帧再跑(见 editcmd.go)。
a.RunEditorCmd(editor.Find)
case emRunTest:
a.runTestAtCursor()
case emTestPkg:
a.runCurrentPackageTests()
case emRevealTree:
a.revealDir(filepath.Dir(d.path))
case emRevealOS:
mygo.Shell.ShowItemInFolder(d.path)
case emHistory:
a.OpenHistoryView(d.path, false, histLocal)
case emCopyRel:
a.copyToClipboard(menuRelPath(a.root, d.path))
case emCopyAbs:
a.copyToClipboard(d.path)
}
}
// menuRelPath 返回相对工作区的路径(统一成正斜杠),不在工作区内时退回绝对路径。
func menuRelPath(root, path string) string {
if root == "" || path == "" || !within(root, path) {
return path
}
rel, err := filepath.Rel(root, path)
if err != nil || rel == "" {
return path
}
return filepath.ToSlash(rel)
}
// clipWrite 是「写系统剪贴板」那一步。
//
// 抽成变量只为测试:单测里换成记录用的函数,免得跑一次测试就把用户剪贴板
// 上的东西覆盖掉。
var clipWrite = func(s string) error {
return mygo.Clipboard.Write(transfer.TextData(s))
}
// copyToClipboard 写系统剪贴板。
//
// 剪贴板是主线程资源,但这里来自菜单选择,已经站在 UI 线程上,直接写即可;
// 失败只记日志——复制个路径而已,弹窗打断反而烦。
func (a *App) copyToClipboard(s string) {
if s == "" {
return
}
if err := clipWrite(s); err != nil {
logf("写剪贴板失败:%v", err)
}
}