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 失败)
This commit is contained in:
1 parent
e7de0bd93a
commit
5a87a61e29
5 files changed
+159
-1
No files matched your search
@@ -2,6 +2,7 @@ package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -112,6 +113,7 @@ func (a *App) lspOpen(d *Doc) {
|
||||
d.sent = d.buf.Version()
|
||||
d.prev = d.buf.Snapshot()
|
||||
_ = a.lsp.DidOpen(d.uri, "go", int(d.sent), d.prev.String())
|
||||
logf("diag: didOpen %s ver=%d", d.uri, d.sent)
|
||||
// 打开之后要等 gopls 分析完才会有提示,但请求本身是异步的,先发出去,
|
||||
// 服务端会等到有结果再回。
|
||||
a.scheduleInlays(d)
|
||||
@@ -150,6 +152,7 @@ func (a *App) lspSync(d *Doc) {
|
||||
|
||||
// applyDiagnostics 把 LSP 诊断转成编辑器诊断,须在主线程调用。
|
||||
func (a *App) applyDiagnostics(uri string, version int, ds []lsp.Diagnostic) {
|
||||
logf("diag: 推送 uri=%s ver=%d n=%d", uri, version, len(ds))
|
||||
var d *Doc
|
||||
for _, x := range a.docs {
|
||||
if x.uri == uri {
|
||||
@@ -158,6 +161,7 @@ func (a *App) applyDiagnostics(uri string, version int, ds []lsp.Diagnostic) {
|
||||
}
|
||||
}
|
||||
if d == nil {
|
||||
logf("diag: 丢弃——没有打开 uri=%s 的文档", uri)
|
||||
return
|
||||
}
|
||||
ver := uint64(version)
|
||||
@@ -166,10 +170,13 @@ func (a *App) applyDiagnostics(uri string, version int, ds []lsp.Diagnostic) {
|
||||
case ver == 0 || ver == d.buf.Version():
|
||||
text = d.buf.String()
|
||||
ver = d.buf.Version()
|
||||
logf("diag: 用当前缓冲 ver=%d", ver)
|
||||
case d.prev.Version() == ver:
|
||||
text = d.prev.String()
|
||||
logf("diag: 用快照 ver=%d", ver)
|
||||
default:
|
||||
// 诊断对应的版本已经过期,丢弃以免位置错乱。
|
||||
logf("diag: 丢弃过期 ver=%d buf=%d prev=%d", ver, d.buf.Version(), d.prev.Version())
|
||||
return
|
||||
}
|
||||
// 原样留一份:请求代码动作时要把服务端给的诊断(含 data)带回去。
|
||||
@@ -194,7 +201,35 @@ func (a *App) applyDiagnostics(uri string, version int, ds []lsp.Diagnostic) {
|
||||
})
|
||||
}
|
||||
d.problems = probs
|
||||
// gopls 的语法错误诊断常是零长 range(一个插入点,且常落在行尾),
|
||||
// 编辑器对零宽区间画不出波浪线——扩展成至少 1 字节:行尾向左扩
|
||||
//(标在行尾字符上),行首向右扩。
|
||||
for i := range out {
|
||||
if out[i].End > out[i].Start {
|
||||
continue
|
||||
}
|
||||
off := out[i].Start
|
||||
p := d.buf.Pos(off)
|
||||
if p.Col > 0 && off <= d.buf.Len() {
|
||||
out[i].Start, out[i].End = off-1, off
|
||||
} else if off < d.buf.Len() {
|
||||
out[i].Start, out[i].End = off, off+1
|
||||
}
|
||||
}
|
||||
d.st.SetDiagnostics(ver, out)
|
||||
got := d.st.Diagnostics()
|
||||
logf("diag: 已设置 %d 条诊断 ver=%d(编辑器现有 %d 条,doc=%s,docs=%d)",
|
||||
len(out), ver, len(got), filepath.Base(d.path), len(a.docs))
|
||||
// 2 秒后复查:诊断有没有被后续某处清掉/覆盖。
|
||||
doc, n := d, len(got)
|
||||
time.AfterFunc(2*time.Second, func() {
|
||||
a.win.Update(func() {
|
||||
after := doc.st.Diagnostics()
|
||||
if len(after) != n {
|
||||
logf("diag: 2 秒后诊断从 %d 变为 %d(被清或覆盖!)", n, len(after))
|
||||
}
|
||||
})
|
||||
})
|
||||
// 诊断到了通常说明这轮分析结束,顺手要一次内联提示。
|
||||
a.scheduleInlays(d)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ var CommonPkgs = []string{
|
||||
"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",
|
||||
"crypto/md5", "crypto/sha1", "crypto/sha256", "compress/gzip", "archive/zip",
|
||||
}
|
||||
|
||||
// Docs 提取一个标准库包所有导出符号的文档正文,返回去重后的清单。
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package lsp
|
||||
|
||||
// 探针:语法错误(go func 缺调用括号)gopls 会不会推诊断、带不带版本号。
|
||||
// 决定「写代码时实时标红」链路在哪一环断掉。
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestProbeSyntaxErrorDiagnostics(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("集成测试需要 gopls")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
writeFile(t, filepath.Join(dir, "go.mod"), "module demo\n\ngo 1.21\n")
|
||||
|
||||
// go func() { ... } 缺最后的 ()——运行时 go run 才报的语法错误。
|
||||
src := "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tgo func() {\n\t\tfmt.Println(\"123\")\n\t}\n}\n"
|
||||
path := filepath.Join(dir, "main.go")
|
||||
writeFile(t, path, src)
|
||||
|
||||
type push struct {
|
||||
ver int
|
||||
ds []Diagnostic
|
||||
}
|
||||
pushes := make(chan push, 16)
|
||||
c, err := Start(context.Background(), Options{
|
||||
Root: dir,
|
||||
Logf: t.Logf,
|
||||
OnDiagnostics: func(uri string, version int, ds []Diagnostic) {
|
||||
select {
|
||||
case pushes <- push{version, ds}:
|
||||
default:
|
||||
}
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Skipf("gopls 不可用: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = c.Close() })
|
||||
|
||||
uri := PathToURI(path)
|
||||
if err := c.DidOpen(uri, "go", 1, src); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
deadline := time.After(45 * time.Second)
|
||||
for {
|
||||
select {
|
||||
case p := <-pushes:
|
||||
t.Logf("推送: version=%d 条数=%d", p.ver, len(p.ds))
|
||||
for _, d := range p.ds {
|
||||
t.Logf(" L%d C%d: [%s] %s", d.Range.Start.Line+1, d.Range.Start.Character+1, d.Source, d.Message)
|
||||
}
|
||||
if len(p.ds) > 0 {
|
||||
if p.ver == 0 {
|
||||
t.Log("version=0:app 层走 buf 分支,应能画出")
|
||||
} else {
|
||||
t.Logf("version=%d:app 层要求 == buf.Version() 或 prev.Version()", p.ver)
|
||||
}
|
||||
return
|
||||
}
|
||||
case <-deadline:
|
||||
t.Fatal("45 秒内没收到任何诊断——gopls 对语法错误不推送")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user