fix: 修通代码补全——fmt. 弹成员列表
补全链路(编辑器框架 + app.complete + lsp.Client.Completion)早已存在, 断的是时序:OnCompletion 回调在输入处理那一帧,lspSync(didChange) 在渲染帧末才跑,补全请求先到,gopls 拿旧文本解释光标位置(旧文本里 没有那个点)返回空。 真 gopls 探针对比:不同步 = 0 项;先 didChange = 29 项 fmt 成员。 修复: - app.complete 发请求前同步调 lspSync(notify+call 按 stdio 序到达) - 点号触发的成员补全(prefix 空)不弹词表兜底,免得一坨无关词添乱 回归测试:completion_timing_test.go 证明同步后含 Printf、 不同步拿不到(lspSync 必要性的证据测试)。
This commit is contained in:
1 parent
745ed1f04a
commit
83abecd607
3 files changed
+171
-3
No files matched your search
@@ -0,0 +1,106 @@
|
||||
package lsp
|
||||
|
||||
// 补全时序的回归测试:证明 complete 里必须先 didChange 再请求。
|
||||
//
|
||||
// 编辑器的 OnCompletion 回调发生在输入处理那一帧,而 lspSync(didChange)
|
||||
// 在渲染帧末才跑——若 complete 里不先同步,gopls 拿着旧文本解释光标位置。
|
||||
// 实测:敲 fmt. 后不同步,gopls 对该位置返回空(成员列表弹不出来);
|
||||
// 先 didChange 再请求,返回完整的 fmt 成员。app.complete 里的 lspSync 由此而来。
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// completionRetry 是个重试包装:gopls 首次分析慢,多试几次。
|
||||
func completionRetry(t *testing.T, c *Client, uri string, pos Position) []CompletionItem {
|
||||
t.Helper()
|
||||
for i := 0; i < 10; i++ {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
items, err := c.Completion(ctx, uri, pos)
|
||||
cancel()
|
||||
if err == nil && len(items) > 0 {
|
||||
return items
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// completionLabels 取补全项标签,最多 n 个,报错时用。
|
||||
func completionLabels(items []CompletionItem, n int) []string {
|
||||
var out []string
|
||||
for i, it := range items {
|
||||
if i >= n {
|
||||
break
|
||||
}
|
||||
out = append(out, it.Label)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func hasLabel(items []CompletionItem, name string) bool {
|
||||
for _, it := range items {
|
||||
if it.Label == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// TestGoplsCompletionAfterDidChange 正确时序:先 didChange 再请求,
|
||||
// fmt. 之后的补全应当给出 fmt 包的成员。
|
||||
func TestGoplsCompletionAfterDidChange(t *testing.T) {
|
||||
c, dir, _ := startTestClient(t)
|
||||
|
||||
old := "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt\n}\n"
|
||||
path := filepath.Join(dir, "main.go")
|
||||
writeFile(t, path, old)
|
||||
uri := PathToURI(path)
|
||||
if err := c.DidOpen(uri, "go", 1, old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 用户敲了点:先同步给 gopls(对齐 app.complete 里的 lspSync),再请求。
|
||||
cur := "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.\n}\n"
|
||||
r, text, ok := DiffChange(old, cur)
|
||||
if !ok {
|
||||
t.Fatal("预期有改动")
|
||||
}
|
||||
if err := c.DidChange(uri, 2, []ContentChange{{Range: &r, Text: text}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 光标在 `.` 之后:第 5 行 "\tfmt." 长 5。
|
||||
items := completionRetry(t, c, uri, Position{Line: 5, Character: 5})
|
||||
if items == nil {
|
||||
t.Fatal("同步后补全仍然为空")
|
||||
}
|
||||
if !hasLabel(items, "Printf") {
|
||||
t.Fatalf("fmt. 的补全里没有 Printf,前 10 项: %v", completionLabels(items, 10))
|
||||
}
|
||||
}
|
||||
|
||||
// TestGoplsStaleCompletion 没有 didChange 直接请求时,gopls 在旧文本上
|
||||
// 解释位置,拿不到 fmt 的成员——这就是 complete 里必须 lspSync 的证据。
|
||||
// gopls 将来若改了行为(比如对越界位置返回垃圾),这里会提醒重新审视。
|
||||
func TestGoplsStaleCompletion(t *testing.T) {
|
||||
c, dir, _ := startTestClient(t)
|
||||
|
||||
old := "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt\n}\n"
|
||||
path := filepath.Join(dir, "main.go")
|
||||
writeFile(t, path, old)
|
||||
uri := PathToURI(path)
|
||||
if err := c.DidOpen(uri, "go", 1, old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// 直接用「敲点之后」的位置请求,不发 didChange。
|
||||
items := completionRetry(t, c, uri, Position{Line: 5, Character: 5})
|
||||
if hasLabel(items, "Printf") {
|
||||
t.Fatalf("未同步竟也拿到了成员补全(gopls 行为变了,app.complete 的 lspSync 或可移除),前 10 项: %v", completionLabels(items, 10))
|
||||
}
|
||||
t.Logf("未同步时返回 %d 项: %v(拿不到成员,符合预期)", len(items), completionLabels(items, 10))
|
||||
}
|
||||
Reference in new issue
Block a user