MyGoland 首次提交:Go 轻量 IDE
- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲) - 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表) - 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记 - Git:状态着色、选择性暂存提交、pull/push、历史并排对比 - 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复 - 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈 - 设计文档与验证记录见 docs/plans/
This commit is contained in:
commit
a80c0f886a
220 files changed
+20784
No files matched your search
@@ -0,0 +1,394 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"mygoland/internal/lsp"
|
||||
"mygoland/internal/task"
|
||||
)
|
||||
|
||||
// 测试结果树:跑 `go test -json`,把事件流解析成「包 → 测试 → 子测试」的层级。
|
||||
//
|
||||
// 为什么用 -json 而不是解析文本:文本输出没有结构,失败和通过的界限只能靠
|
||||
// 前缀猜;-json 每条事件都带 package/test/action,子测试还能靠名字里的斜杠还原层级。
|
||||
|
||||
// testStatus 是一条测试的状态。
|
||||
type testStatus int
|
||||
|
||||
const (
|
||||
testNone testStatus = iota
|
||||
testRunning
|
||||
testPass
|
||||
testFail
|
||||
testSkip
|
||||
)
|
||||
|
||||
func (s testStatus) mark() string {
|
||||
switch s {
|
||||
case testRunning:
|
||||
return "⟳"
|
||||
case testPass:
|
||||
return "✓"
|
||||
case testFail:
|
||||
return "✗"
|
||||
case testSkip:
|
||||
return "○"
|
||||
}
|
||||
return "·"
|
||||
}
|
||||
|
||||
// testNode 是树里的一个节点。
|
||||
type testNode struct {
|
||||
Pkg string
|
||||
Name string // 完整名,子测试形如 TestFoo/case1
|
||||
Depth int // 缩进层级,0 是包
|
||||
Status testStatus
|
||||
Elapsed float64
|
||||
Output []string
|
||||
// File/Line 是失败输出的第一个 file:line(已还原成磁盘路径),失败时才有。
|
||||
File string
|
||||
Line int
|
||||
}
|
||||
|
||||
// testState 是测试面板的状态。
|
||||
type testState struct {
|
||||
running bool
|
||||
task *task.Task
|
||||
nodes []*testNode
|
||||
seen map[string]*testNode
|
||||
sel int
|
||||
pattern string
|
||||
}
|
||||
|
||||
// key 是节点在 map 里的标识。
|
||||
func testKey(pkg, name string) string { return pkg + "\x00" + name }
|
||||
|
||||
// RunTestTree 跑测试并填结果树;pattern 为空表示整个工作区。
|
||||
func (a *App) RunTestTree(pattern string) {
|
||||
if a.root == "" {
|
||||
a.alert("运行测试", "先打开一个工作区。")
|
||||
return
|
||||
}
|
||||
if pattern == "" {
|
||||
pattern = "./..."
|
||||
}
|
||||
a.startTest("test", "-json", pattern)
|
||||
}
|
||||
|
||||
// runOneTest 用 -run 只跑一个用例(含子测试路径)。
|
||||
// pkg 可以是 import 路径,也可以是 ./相对目录,go test 都认。
|
||||
func (a *App) runOneTest(pkg, name string) {
|
||||
if a.root == "" {
|
||||
a.alert("运行测试", "先打开一个工作区。")
|
||||
return
|
||||
}
|
||||
a.startTest("test", "-json", "-run", runFilterFor(name), pkg)
|
||||
}
|
||||
|
||||
// startTest 拉起一次 go test 并把结果树清空重填;args 是 go 的完整参数。
|
||||
func (a *App) startTest(args ...string) {
|
||||
if a.test.running {
|
||||
a.StopTask()
|
||||
}
|
||||
t, err := task.Start(a.root, "go", args...)
|
||||
if err != nil {
|
||||
a.alert("运行测试", err.Error())
|
||||
return
|
||||
}
|
||||
a.test = testState{
|
||||
running: true,
|
||||
task: t,
|
||||
seen: map[string]*testNode{},
|
||||
pattern: strings.Join(args, " "),
|
||||
}
|
||||
a.panel = true
|
||||
a.panelTab = 5
|
||||
if a.panelH < 260 {
|
||||
a.panelH = 300
|
||||
}
|
||||
a.refresh()
|
||||
|
||||
go func() {
|
||||
for line := range t.Lines {
|
||||
a.testEvent(line)
|
||||
}
|
||||
t.Wait()
|
||||
a.win.Update(func() {
|
||||
a.test.running = false
|
||||
a.test.task = nil
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
// runTestAtCursor 跑光标所在的那个测试函数。
|
||||
// 函数扫描走本地正则(scanTestFuncs),没保存的新函数也能跑。
|
||||
func (a *App) runTestAtCursor() {
|
||||
d := a.doc()
|
||||
if d == nil || a.root == "" {
|
||||
a.RunTestTree("")
|
||||
return
|
||||
}
|
||||
name, ok := testFuncAt(d.buf.String(), d.st.CursorPos().Line)
|
||||
if !ok {
|
||||
a.appendOut("光标不在测试函数里,改为运行全部测试。")
|
||||
a.RunTestTree("")
|
||||
return
|
||||
}
|
||||
a.runOneTest(packagePattern(a.root, d.path), name)
|
||||
}
|
||||
|
||||
// testEvent 处理一行 -json 事件。
|
||||
//
|
||||
// 这里的调用来自读取协程,所以要自己进 UI 线程。
|
||||
func (a *App) testEvent(line string) {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || !strings.HasPrefix(line, "{") {
|
||||
return
|
||||
}
|
||||
var ev struct {
|
||||
Action string `json:"Action"`
|
||||
Package string `json:"Package"`
|
||||
Test string `json:"Test"`
|
||||
Elapsed float64 `json:"Elapsed"`
|
||||
Output string `json:"Output"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(line), &ev); err != nil {
|
||||
return
|
||||
}
|
||||
a.win.Update(func() { a.applyTestEvent(ev.Action, ev.Package, ev.Test, ev.Elapsed, ev.Output) })
|
||||
}
|
||||
|
||||
// applyTestEvent 把事件落到树上。
|
||||
func (a *App) applyTestEvent(action, pkg, test string, elapsed float64, output string) {
|
||||
if a.test.seen == nil {
|
||||
a.test.seen = map[string]*testNode{}
|
||||
}
|
||||
|
||||
// 包级事件:没有 Test 字段,用包名当一个根节点。
|
||||
name := test
|
||||
if name == "" && pkg != "" && action != "output" {
|
||||
name = ""
|
||||
}
|
||||
if pkg == "" {
|
||||
return
|
||||
}
|
||||
|
||||
n, ok := a.test.seen[testKey(pkg, name)]
|
||||
if !ok {
|
||||
// 包节点在第一次出现时创建,位置固定在列表前面。
|
||||
if name != "" {
|
||||
if _, hasPkg := a.test.seen[testKey(pkg, "")]; !hasPkg {
|
||||
a.addTestNode(&testNode{Pkg: pkg, Name: "", Depth: 0, Status: testNone})
|
||||
}
|
||||
}
|
||||
depth := 0
|
||||
if name != "" {
|
||||
depth = 1 + strings.Count(name, "/")
|
||||
}
|
||||
n = &testNode{Pkg: pkg, Name: name, Depth: depth}
|
||||
a.addTestNode(n)
|
||||
}
|
||||
|
||||
switch action {
|
||||
case "run":
|
||||
n.Status = testRunning
|
||||
case "pass":
|
||||
n.Status = testPass
|
||||
n.Elapsed = elapsed
|
||||
case "fail":
|
||||
n.Status = testFail
|
||||
n.Elapsed = elapsed
|
||||
// 从自己的输出里找失败位置并当场还原成磁盘路径:
|
||||
// applyTestEvent 在主线程跑,resolveTestFile 里的 os.Stat / 索引
|
||||
// 扫描在这里做是安全的,且每个失败节点只做一次。
|
||||
if file, line := failureLoc(n.Output); file != "" {
|
||||
n.File = a.resolveTestFile(pkg, file)
|
||||
n.Line = line
|
||||
}
|
||||
case "skip":
|
||||
n.Status = testSkip
|
||||
n.Elapsed = elapsed
|
||||
case "output":
|
||||
if output != "" {
|
||||
n.Output = append(n.Output, strings.TrimRight(output, "\n"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// addTestNode 追加一个节点并登记。
|
||||
func (a *App) addTestNode(n *testNode) {
|
||||
a.test.nodes = append(a.test.nodes, n)
|
||||
a.test.seen[testKey(n.Pkg, n.Name)] = n
|
||||
}
|
||||
|
||||
// testCounts 统计通过/失败数量,给页签做角标。
|
||||
func (a *App) testCounts() (pass, fail, skip int) {
|
||||
for _, n := range a.test.nodes {
|
||||
if n.Name == "" {
|
||||
continue
|
||||
}
|
||||
switch n.Status {
|
||||
case testPass:
|
||||
pass++
|
||||
case testFail:
|
||||
fail++
|
||||
case testSkip:
|
||||
skip++
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// runCurrentPackageTests 只跑当前文件所在的包。
|
||||
func (a *App) runCurrentPackageTests() {
|
||||
d := a.doc()
|
||||
if d == nil || a.root == "" {
|
||||
a.RunTestTree("")
|
||||
return
|
||||
}
|
||||
a.RunTestTree(packagePattern(a.root, d.path))
|
||||
}
|
||||
|
||||
// packagePattern 把文件路径换算成 go test 的包模式(./相对路径)。
|
||||
func packagePattern(root, path string) string {
|
||||
dir := path
|
||||
if i := strings.LastIndexAny(dir, `/\`); i > 0 {
|
||||
dir = dir[:i]
|
||||
}
|
||||
rel := dir
|
||||
if strings.HasPrefix(strings.ToLower(dir), strings.ToLower(root)) {
|
||||
rel = dir[len(root):]
|
||||
}
|
||||
rel = strings.ReplaceAll(rel, `\`, "/")
|
||||
rel = strings.TrimPrefix(rel, "/")
|
||||
if rel == "" {
|
||||
return "."
|
||||
}
|
||||
return "./" + rel
|
||||
}
|
||||
|
||||
// testView 是面板里的「测试」页签。
|
||||
func (a *App) testView(c *ui.Context) {
|
||||
t := c.Theme()
|
||||
pass, fail, skip := a.testCounts()
|
||||
|
||||
ui.Column(c).FillHeight().Children(func() {
|
||||
ui.Row(c).Padding(3, 10).Gap(8).AlignItems(ui.Center).
|
||||
BorderWidth(0, 0, 1, 0).BorderColor(t.Border).Children(func() {
|
||||
ui.Textf(c, "%s", a.test.pattern).FontSize(11).TextColor(t.TextMuted).SingleLine()
|
||||
ui.Box(c).Grow(1)
|
||||
if pass+fail+skip > 0 {
|
||||
ui.Textf(c, "✓ %d ✗ %d ○ %d", pass, fail, skip).FontSize(11).TextColor(t.TextMuted)
|
||||
}
|
||||
if a.test.running {
|
||||
ui.Text(c, "运行中…").FontSize(11).TextColor(t.Accent)
|
||||
}
|
||||
if ui.Button(c, "全部测试").Clicked() {
|
||||
a.RunTestTree("")
|
||||
}
|
||||
if ui.Button(c, "当前包").Clicked() {
|
||||
a.runCurrentPackageTests()
|
||||
}
|
||||
})
|
||||
|
||||
if len(a.test.nodes) == 0 {
|
||||
ui.Column(c).Grow(1).FillHeight().Center().Children(func() {
|
||||
ui.Text(c, "还没有运行测试").FontSize(12).TextColor(t.TextMuted)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 树占满剩余高度;选中项的输出直接内联在它下面,
|
||||
// 不另开输出区——两个区域争高度会把其中一个挤没。
|
||||
ui.Scroll(c).Grow(1).Children(func() {
|
||||
ui.Column(c).Gap(1).Children(func() {
|
||||
for i, n := range a.test.nodes {
|
||||
idx := i
|
||||
row := ui.ButtonBase(c).FillWidth().Height(22).Padding(0, 6).Radius(3).
|
||||
Cursor(ui.CursorPointer)
|
||||
if idx == a.test.sel {
|
||||
row = row.Background(t.SurfaceHover)
|
||||
}
|
||||
row.Children(func() {
|
||||
ui.Row(c).FillWidth().Gap(6).AlignItems(ui.Center).Children(func() {
|
||||
ui.Box(c).Width(float32(n.Depth) * 12)
|
||||
ui.Text(c, n.Status.mark()).FontSize(11).
|
||||
TextColor(testColor(t, n.Status)).Width(14)
|
||||
label := n.Name
|
||||
if label == "" {
|
||||
label = strings.TrimPrefix(n.Pkg, "mygoland/") + "(整个包)"
|
||||
}
|
||||
ui.Text(c, label).FontSize(11).TextColor(t.Text).SingleLine()
|
||||
if n.Elapsed > 0 {
|
||||
ui.Textf(c, "%.2fs", n.Elapsed).FontSize(10).TextColor(t.TextMuted)
|
||||
}
|
||||
})
|
||||
})
|
||||
if row.Clicked() {
|
||||
a.test.sel = idx
|
||||
a.refresh()
|
||||
}
|
||||
|
||||
// 选中项:先给操作行(重跑 / 失败跳源码),再把输出末尾内联出来。
|
||||
if idx == a.test.sel {
|
||||
pad := 6 + float32(n.Depth)*12 + 22
|
||||
ui.Row(c).Padding(2, pad).Gap(6).Children(func() {
|
||||
if n.Name != "" {
|
||||
if ui.Button(c, "重跑这个用例").Clicked() {
|
||||
a.runOneTest(n.Pkg, n.Name)
|
||||
}
|
||||
}
|
||||
if n.Status == testFail && n.File != "" {
|
||||
label := filepath.Base(n.File) + ":" + strconv.Itoa(n.Line)
|
||||
if ui.Button(c, "跳到 "+label).Clicked() {
|
||||
a.openAt(n.File, lsp.Position{Line: n.Line - 1})
|
||||
}
|
||||
}
|
||||
})
|
||||
lines := n.Output
|
||||
if len(lines) > 6 {
|
||||
lines = lines[len(lines)-6:]
|
||||
}
|
||||
for _, line := range lines {
|
||||
if strings.TrimSpace(line) == "" {
|
||||
continue
|
||||
}
|
||||
ui.Text(c, line).FontSize(10).TextColor(t.TextMuted).
|
||||
Padding(0, pad).SingleLine()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// testColor 按状态给颜色。
|
||||
func testColor(t *ui.Theme, s testStatus) ui.Color {
|
||||
switch s {
|
||||
case testPass:
|
||||
return t.Success
|
||||
case testFail:
|
||||
return t.Danger
|
||||
case testSkip:
|
||||
return t.Warning
|
||||
case testRunning:
|
||||
return t.Accent
|
||||
}
|
||||
return t.TextMuted
|
||||
}
|
||||
|
||||
// testTabLabel 是页签上的文字,带失败数。
|
||||
func (a *App) testTabLabel() string {
|
||||
_, fail, _ := a.testCounts()
|
||||
if fail > 0 {
|
||||
return fmt.Sprintf("测试 %d✗", fail)
|
||||
}
|
||||
return "测试"
|
||||
}
|
||||
Reference in new issue
Block a user