Files
MyGolandRelease/internal/godoc/godoc.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

110 lines
3.2 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 godoc 从本地 Go 源码(GOROOT/src)提取导出符号的文档正文,
// 给「AI 预热文档翻译」用:gopls 补全/悬停显示的文档与源码注释同源,
// 规范化空白后文本一致(internal/lsp/doc_probe_test.go 已实证),
// 所以预热产生的译文缓存能直接命中。
package godoc
import (
"fmt"
"go/doc"
"go/parser"
"go/token"
"os"
"path/filepath"
"runtime"
"strings"
)
// CommonPkgs 是预热的常用标准库清单:日常开发覆盖面最大的那些。
// 想加只改这里;第三方包仍走按需懒翻译(源码在 module cache,
// go/build 在 module 模式下找不到它们,第一版不碰)。
var CommonPkgs = []string{
"fmt", "strings", "strconv", "errors", "time", "os", "io", "bufio",
"bytes", "sort", "slices", "maps", "math", "math/rand", "path",
"path/filepath", "unicode", "unicode/utf8", "sync", "context",
"log", "regexp", "encoding/json", "encoding/base64", "encoding/hex",
"net/url", "net/http", "flag", "os/exec", "reflect", "hash/crc32",
"crypto/md5", "crypto/sha1", "crypto/sha256", "compress/gzip", "archive/zip",
}
// Docs 提取一个标准库包所有导出符号的文档正文,返回去重后的清单。
// 空文档(如纯结构体占位)跳过。
func Docs(pkgPath string) ([]string, error) {
bp, err := build0(pkgPath)
if err != nil {
return nil, err
}
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, bp, func(fi os.FileInfo) bool {
return !strings.HasSuffix(fi.Name(), "_test.go")
}, parser.ParseComments)
if err != nil {
return nil, err
}
seen := map[string]bool{}
var out []string
add := func(text string) {
text = strings.TrimSpace(text)
if text == "" || seen[text] {
return
}
seen[text] = true
out = append(out, text)
}
for _, p := range pkgs {
d := doc.New(p, "./", 0)
for _, c := range d.Consts {
add(c.Doc)
}
for _, v := range d.Vars {
add(v.Doc)
}
for _, fn := range d.Funcs {
add(fn.Doc)
}
for _, ty := range d.Types {
add(ty.Doc)
for _, c := range ty.Consts {
add(c.Doc)
}
for _, v := range ty.Vars {
add(v.Doc)
}
for _, fn := range ty.Funcs { // 方法
add(fn.Doc)
}
}
}
return out, nil
}
// build0 返回标准库包的源码目录。
func build0(pkgPath string) (string, error) {
if !isStandard(pkgPath) {
return "", fmt.Errorf("%s 不是标准库包(第一版只预热标准库)", pkgPath)
}
return filepath.Join(gorootSrc(), filepath.FromSlash(pkgPath)), nil
}
// NormKey 是文档翻译缓存的 key 规范:所有空白压成单个空格。
// gopls 给的文本与 go/doc 提取的文本只在换行/空格上有差异,
// 规范化后两边(以及预热工具写入的缓存)才能互相命中。
func NormKey(s string) string {
return strings.Join(strings.Fields(s), " ")
}
// isStandard 判断是否标准库导入路径:首段不含点
// (fmt 是;github.com/x/y 不是)。对 99% 的包够用。
func isStandard(pkgPath string) bool {
first := pkgPath
if i := strings.IndexByte(first, '/'); i >= 0 {
first = first[:i]
}
return first != "" && !strings.Contains(first, ".")
}
// gorootSrc 返回本地 Go 源码根目录。
func gorootSrc() string {
return filepath.Join(runtime.GOROOT(), "src")
}