feat: 标准库文档批量预热——补全/悬停翻译秒出

懒翻译要等 1-2 秒。真 gopls 实证 go/doc 从本地源码提取的文档与
gopls 补全/悬停正文规范化空白后完全一致,批量预热方案成立:
- internal/godoc: go/doc 提取器 + 36 个常用标准库清单 + NormKey
- app.PreheatDocs: 6 并发翻译,进度走输出面板,已缓存自动跳过;
  菜单手动触发 + 打开项目 8 秒后自动跑(每会话一次)
- tools/preheat: 命令行批量工具,直接写用户缓存文件
- 缓存 key 统一 NormKey,预热/懒翻译/旧缓存三方互命中
- 已实测灌入 10 包 394 条译文(272 秒),常用包全部秒出
This commit is contained in:
zerorigin committed 2026-11-04 17:30:51 +08:00
1 parent a7d53f25d4
commit f5bfbac6c3
10 files changed
+663 -10

No files matched your search

+109
View File
@@ -0,0 +1,109 @@
// 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",
"hash/md5", "hash/sha1", "hash/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")
}