package app import ( "strings" "testing" "github.com/egoist/mygo/ui" "github.com/egoist/mygo/ui/editor" "mygoland/internal/lsp" ) // TestParseGoplsSnippet 固化实测前提:gopls 给的 ${1:} 空默认值模板, // mygo v0.9.0 的 ParseSnippet 能解析,插入文本是 Print()。 func TestParseGoplsSnippet(t *testing.T) { s := parseCompletionSnippet("Print(${1:})") if s == nil { t.Fatal("mygo 应该能解析 gopls 的 ${1:} 模板") } if got := s.Text(); got != "Print()" { t.Fatalf("插入文本 = %q,想要 Print()", got) } // 同一模板走缓存,结果必须是同一个对象。 if s2 := parseCompletionSnippet("Print(${1:})"); s2 != s { t.Fatal("缓存没生效") } } func TestPlainSnippetText(t *testing.T) { cases := []struct{ in, want string }{ {"Print(${1:})", "Print()"}, {"Add(${1:a}, ${2:b})$0", "Add(a, b)"}, {"cost is \\$5$0", "cost is $5"}, {"plain text", "plain text"}, } for _, c := range cases { if got := plainSnippetText(c.in); got != c.want { t.Errorf("plainSnippetText(%q) = %q, want %q", c.in, got, c.want) } } if got := plainSnippetText("Print(${1:})"); strings.Contains(got, "${") { t.Fatalf("降级文本不该残留占位语法:%q", got) } } // TestApplyCompletionSnippet 只有 insertTextFormat==2 才接模板; // 解析不了的要降级成纯文本,不能把 ${...} 原样插进代码。 func TestApplyCompletionSnippet(t *testing.T) { // 纯文本补全不碰 Snippet。 plain := editor.Completion{Label: "Print", Insert: "Print"} applyCompletionSnippet(&plain, lsp.CompletionItem{InsertTextFormat: 1}, "Print") if plain.Snippet != nil || plain.Insert != "Print" { t.Fatalf("纯文本补全被改了:%+v", plain) } // snippet 补全:模板交给框架。 snip := editor.Completion{Label: "Print", Insert: "Print(${1:})", Kind: editor.CompletionFunction} applyCompletionSnippet(&snip, lsp.CompletionItem{InsertTextFormat: 2}, "Print(${1:})") if snip.Snippet == nil { t.Fatal("应把模板交给 Completion.Snippet") } if snip.Kind != editor.CompletionFunction { t.Fatal("Kind 只决定图标,不该被 snippet 改掉") } // 解析失败:降级成纯文本,且缓存住失败结果。 bad := "${1|one,two|} + ${2:${3:nested}}" fallback := editor.Completion{Label: "x", Insert: bad} applyCompletionSnippet(&fallback, lsp.CompletionItem{InsertTextFormat: 2}, bad) if fallback.Snippet != nil { t.Fatal("不支持的模板不该当成 snippet") } if strings.Contains(fallback.Insert, "${") { t.Fatalf("降级文本不该残留占位语法:%q", fallback.Insert) } } // TestToEditorCompletionGopls 按 gopls 的真实返回走完整映射: // insertText 为空、内容在 textEdit.newText、format=2。 func TestToEditorCompletionGopls(t *testing.T) { it := lsp.CompletionItem{ Label: "Print", Kind: 3, TextEdit: &lsp.TextEdit{NewText: "Print(${1:})"}, InsertTextFormat: 2, } got := toEditorCompletion(it) if got.Snippet == nil { t.Fatalf("gopls 的模板补全没接上:%+v", got) } if got.Kind != completionKind(3) { t.Fatal("映射把补全类型改了") } if got.Label != "Print" { t.Fatalf("label 不对:%q", got.Label) } } // TestSnippetCompletionEndToEnd 走完「LSP 项 → 编辑器补全 → 接受 → 光标」。 // 框架自己的 snippet_test.go 覆盖模板状态机,这里只负责证明我们填的 // Completion.Snippet 确实被框架应用了,而不是设了个没人读的字段。 func TestSnippetCompletionEndToEnd(t *testing.T) { src := "package main\n\nfunc main() {\n\tfmt.Pr\n}\n" d := newActionDoc(t, src) at := strings.Index(src, "Pr") end := at + len("Pr") d.st.Select(end, end) d.st.ShowCompletions(at, []editor.Completion{ toEditorCompletion(lsp.CompletionItem{ Label: "Print", TextEdit: &lsp.TextEdit{NewText: "Print(${1:})"}, InsertTextFormat: 2, }), }) tt := ui.NewTester(func(c *ui.Context) { editor.View(c, d.st).Fill().AutoFocus() }, 640, 400) tt.Frame() tt.Key(0, ui.KeyTab) // 接受第一项补全 tt.Frame() got := d.buf.String() if !strings.Contains(got, "fmt.Print()") { t.Fatalf("接受补全后文本不对:%q", got) } // 光标应停在括号里(第一个占位符),而不是行尾。 if cur := d.st.Cursor(); cur != strings.Index(got, "Print()")+len("Print(") { t.Fatalf("光标 = %d,应停在括号内;文本 %q", cur, got) } }