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:
zerorigin committed 2026-11-04 16:56:51 +08:00
1 parent 745ed1f04a
commit 83abecd607
3 files changed
+171 -3

No files matched your search

+13 -3
View File
@@ -216,9 +216,18 @@ func severityOf(s int) editor.Severity {
func (a *App) complete(d *Doc, r editor.CompletionRequest) {
c := a.lsp
if c == nil {
d.st.ShowCompletions(r.Offset-len(r.Prefix), wordsOf(d.buf, r.Prefix))
// 点号触发的成员补全(prefix 为空)没有词表可兜底:
// 弹一坨缓冲区里的无关词只会添乱。
if r.Prefix != "" {
d.st.ShowCompletions(r.Offset-len(r.Prefix), wordsOf(d.buf, r.Prefix))
}
return
}
// 补全回调发生在编辑器输入处理的那一帧,此刻 lspSync(渲染帧末才跑)
// 还没把刚敲的字符推给 gopls——它拿着旧文本解释光标位置。
// 实测:敲 fmt. 后不同步,gopls 对这个位置返回空(成员列表弹不出来);
// 先 didChange 再请求,返回完整的 fmt 成员。必须先同步。
a.lspSync(d)
at := r.Offset - len(r.Prefix)
pos := positionAt(d, r.Offset)
version := r.Version
@@ -227,9 +236,10 @@ func (a *App) complete(d *Doc, r editor.CompletionRequest) {
defer cancel()
items, err := c.Completion(ctx, d.uri, pos)
if err != nil || len(items) == 0 {
// 语言服务器还没准备好或没有建议时,退回词表补全。
// 语言服务器还没准备好或没有建议时,退回词表补全;
// 成员补全场景(点号触发)没有词可兜,直接不弹。
a.win.Update(func() {
if d.buf.Version() != version {
if d.buf.Version() != version || r.Prefix == "" {
return
}
d.st.ShowCompletions(at, wordsOf(d.buf, r.Prefix))