Files
MyGolandRelease/internal/godoc/godoc.go
T
zerorigin 5a87a61e29 fix: 实时诊断波浪线不显示——零长 range 扩展 1 字节后画线
根因:gopls 对语法错误常推零长 range(start==end,插入点),
且常落在行尾,编辑器按零长画线=画不出,用户看不到任何提示。
修复:诊断入编辑器前把零长 range 扩展为 1 字节(行首且列>0 向左扩,否则向右扩),
与 VS Code 显示行为一致。e2e 截图验证波浪线+悬浮提示正常。

附带:
- internal/lsp/syntax_diag_probe_test.go 真 gopls 语法错误诊断推送探针
- internal/godoc CommonPkgs 清单修正(hash/md5 -> crypto/md5 等不存在的包路径)
- .gitignore 增加 *.sock / e2e_shot*.png(Windows 上 socket 文件删不掉导致 add 失败)
2026-11-04 18:12:31 +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")
}