T1 移动包同步(internal/app/movepkg.go + renameentry.go)
gopls 不声明 willRename,改用 go/parser 精确定位 ImportSpec.Path,
目录移动改全仓 import 路径、单文件跨目录移动改 package 声明。
写盘失败整体恢复并回滚 rename;已打开且无改动的缓冲区用 Reset 同步。
集成测试在临时 Go 模块里移目录后直接跑 go build ./... 通过。
T2 输出/问题面板右键(panel_menu.go + output.go + popmenu.go)
新增 openPopAt 让菜单贴指针弹出;复制选中行/复制全部/清空输出。
真窗口截图确认位置正确、无裁切。
**范围缩窄**:终端从 mygo v0.7.4 起就自带原生 ContextMenu,原判断是误报。
T3 snippet 补全(completion_snippet.go + lsp/protocol.go)
snippetSupport 改 true;实测 gopls v0.23 返回 insertTextFormat=2、
textEdit.newText="Print(${1:})",接上 mygo v0.9.0 的 ParseSnippet。
解析失败降级成纯文本(支持嵌套占位/choices 剥离),结果按模板缓存。
端到端 Tester 验证接受补全后得到 fmt.Print() 且光标停在括号内。
T4 本地历史超限可见(history.go)
静默 return 改为返回 histSkipped,输出面板提示;每文件整个会话只提一次。
T5 全局搜索并发(palette.go)
NumCPU(上限 8)个 worker 并发读盘,结果按文件序合并保证顺序稳定;
命中上限停止派发;512KB 以上被跳过的文件数显示成面板底部提示。
T6 清理根目录残留;4 个 0 字节 .sock 被环境安全守卫拦住未删(已 gitignore)。
验证:gofmt 空、build/vet OK、go test -count=1 ./... 全绿(282 个测试)、
新增并发/剪贴板/回滚用例 go test -race 无数据竞争。
docs/MyGoland-待办与规划.md 已清空待办,只剩 6 项规划与完成索引。
131 lines
4.3 KiB
Go
131 lines
4.3 KiB
Go
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)
|
|
}
|
|
}
|