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,389 @@
|
||||
package dap
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// eventLog 收集适配器事件。
|
||||
type eventLog struct {
|
||||
mu sync.Mutex
|
||||
events map[string]chan json.RawMessage
|
||||
}
|
||||
|
||||
func newEventLog() *eventLog {
|
||||
return &eventLog{events: map[string]chan json.RawMessage{}}
|
||||
}
|
||||
|
||||
func (l *eventLog) push(name string, body json.RawMessage) {
|
||||
l.mu.Lock()
|
||||
ch, ok := l.events[name]
|
||||
if !ok {
|
||||
ch = make(chan json.RawMessage, 8)
|
||||
l.events[name] = ch
|
||||
}
|
||||
l.mu.Unlock()
|
||||
select {
|
||||
case ch <- body:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (l *eventLog) wait(name string, d time.Duration) (json.RawMessage, bool) {
|
||||
l.mu.Lock()
|
||||
ch, ok := l.events[name]
|
||||
if !ok {
|
||||
ch = make(chan json.RawMessage, 8)
|
||||
l.events[name] = ch
|
||||
}
|
||||
l.mu.Unlock()
|
||||
select {
|
||||
case body := <-ch:
|
||||
return body, true
|
||||
case <-time.After(d):
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
|
||||
const prog = `package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func add(a, b int) int {
|
||||
sum := a + b
|
||||
return sum
|
||||
}
|
||||
|
||||
func main() {
|
||||
x := 1
|
||||
y := 2
|
||||
fmt.Println(add(x, y))
|
||||
}
|
||||
`
|
||||
|
||||
// goroutineProg 三个 worker 并发跑,用来验证 threads 列 goroutine。
|
||||
const goroutineProg = `package main
|
||||
|
||||
import "sync"
|
||||
|
||||
func worker(wg *sync.WaitGroup, n int) {
|
||||
defer wg.Done()
|
||||
x := n * 2
|
||||
_ = x
|
||||
}
|
||||
|
||||
func main() {
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 3; i++ {
|
||||
wg.Add(1)
|
||||
go worker(&wg, i)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
`
|
||||
|
||||
// TestThreadsGoroutines 停在并发程序的断点上时,threads 应列出
|
||||
// goroutine,且当前停住的那个在列表里——这是调试面板 goroutine 列表的数据源。
|
||||
func TestThreadsGoroutines(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("集成测试需要 dlv")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module demo\n\ngo 1.21\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
main := filepath.Join(dir, "main.go")
|
||||
if err := os.WriteFile(main, []byte(goroutineProg), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
log := newEventLog()
|
||||
c, err := Start(dir, log.push, t.Logf)
|
||||
if err != nil {
|
||||
t.Skipf("dlv 不可用: %v", err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
if err := c.Initialize(); err != nil {
|
||||
t.Fatalf("initialize: %v", err)
|
||||
}
|
||||
launched := make(chan error, 1)
|
||||
go func() { launched <- c.Launch("./", nil, false, 120*time.Second) }()
|
||||
|
||||
if _, ok := log.wait("initialized", 90*time.Second); !ok {
|
||||
t.Fatal("没有收到 initialized 事件")
|
||||
}
|
||||
// 第 7 行 `x := n * 2`:三个 worker 都会经过。
|
||||
if _, err := c.SetBreakpoints(main, []BreakpointSpec{{Line: 7}}); err != nil {
|
||||
t.Fatalf("setBreakpoints: %v", err)
|
||||
}
|
||||
if err := c.ConfigurationDone(); err != nil {
|
||||
t.Fatalf("configurationDone: %v", err)
|
||||
}
|
||||
body, ok := log.wait("stopped", 90*time.Second)
|
||||
if !ok {
|
||||
t.Fatal("没有停在断点上")
|
||||
}
|
||||
var stopped struct {
|
||||
ThreadID int `json:"threadId"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &stopped); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
threads, err := c.Threads()
|
||||
if err != nil {
|
||||
t.Fatalf("threads: %v", err)
|
||||
}
|
||||
if len(threads) == 0 {
|
||||
t.Fatal("应至少列出一个 goroutine")
|
||||
}
|
||||
ids := map[int]bool{}
|
||||
current := false
|
||||
for _, th := range threads {
|
||||
if th.ID == 0 {
|
||||
t.Fatalf("goroutine 编号不应为 0: %+v", th)
|
||||
}
|
||||
if ids[th.ID] {
|
||||
t.Fatalf("goroutine 编号重复: %d", th.ID)
|
||||
}
|
||||
ids[th.ID] = true
|
||||
if th.ID == stopped.ThreadID {
|
||||
current = true
|
||||
}
|
||||
}
|
||||
if !current {
|
||||
t.Fatalf("当前停住的 goroutine %d 不在列表里: %+v", stopped.ThreadID, threads)
|
||||
}
|
||||
<-launched
|
||||
}
|
||||
|
||||
func TestDebugSession(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("集成测试需要 dlv")
|
||||
}
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module demo\n\ngo 1.21\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
main := filepath.Join(dir, "main.go")
|
||||
if err := os.WriteFile(main, []byte(prog), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
log := newEventLog()
|
||||
c, err := Start(dir, log.push, t.Logf)
|
||||
if err != nil {
|
||||
t.Skipf("dlv 不可用: %v", err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
if err := c.Initialize(); err != nil {
|
||||
t.Fatalf("initialize: %v", err)
|
||||
}
|
||||
|
||||
launched := make(chan error, 1)
|
||||
go func() { launched <- c.Launch("./", nil, false, 120*time.Second) }()
|
||||
|
||||
// 等适配器准备好接收断点。
|
||||
if _, ok := log.wait("initialized", 90*time.Second); !ok {
|
||||
t.Fatal("没有收到 initialized 事件")
|
||||
}
|
||||
if _, err := c.SetBreakpoints(main, []BreakpointSpec{{Line: 6}}); err != nil {
|
||||
t.Fatalf("setBreakpoints: %v", err)
|
||||
}
|
||||
if err := c.ConfigurationDone(); err != nil {
|
||||
t.Fatalf("configurationDone: %v", err)
|
||||
}
|
||||
|
||||
body, ok := log.wait("stopped", 90*time.Second)
|
||||
if !ok {
|
||||
t.Fatal("没有停在断点上")
|
||||
}
|
||||
var stopped struct {
|
||||
Reason string `json:"reason"`
|
||||
ThreadID int `json:"threadId"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &stopped); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stopped.ThreadID == 0 {
|
||||
t.Fatalf("stopped 事件没有线程号: %s", body)
|
||||
}
|
||||
|
||||
frames, err := c.StackTrace(stopped.ThreadID, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("stackTrace: %v", err)
|
||||
}
|
||||
if len(frames) == 0 {
|
||||
t.Fatal("调用栈为空")
|
||||
}
|
||||
top := frames[0]
|
||||
if top.Line != 6 {
|
||||
t.Fatalf("断点行号: got %d want 6(栈顶 %s)", top.Line, top.Name)
|
||||
}
|
||||
if !strings.HasSuffix(strings.ToLower(top.Source.Path), "main.go") {
|
||||
t.Fatalf("栈顶文件不对: %s", top.Source.Path)
|
||||
}
|
||||
|
||||
// 变量里应当有 a 和 b。
|
||||
scopes, err := c.Scopes(top.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("scopes: %v", err)
|
||||
}
|
||||
names := map[string]string{}
|
||||
for _, sc := range scopes {
|
||||
if sc.Expensive || sc.VariablesReference == 0 {
|
||||
continue
|
||||
}
|
||||
vs, err := c.Variables(sc.VariablesReference)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, v := range vs {
|
||||
names[v.Name] = v.Value
|
||||
}
|
||||
}
|
||||
if _, ok := names["a"]; !ok {
|
||||
t.Fatalf("变量里没有 a:%v", names)
|
||||
}
|
||||
if names["a"] != "1" {
|
||||
t.Fatalf("a 的值: got %q want \"1\"", names["a"])
|
||||
}
|
||||
|
||||
// 表达式求值。
|
||||
res, err := c.Evaluate("a+b", top.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("evaluate: %v", err)
|
||||
}
|
||||
if !strings.HasPrefix(res, "3") {
|
||||
t.Fatalf("a+b 求值结果: %q", res)
|
||||
}
|
||||
|
||||
// 单步一次,应当移动到 return 行。
|
||||
if err := c.Next(stopped.ThreadID); err != nil {
|
||||
t.Fatalf("next: %v", err)
|
||||
}
|
||||
body, ok = log.wait("stopped", 60*time.Second)
|
||||
if !ok {
|
||||
t.Fatal("单步后没有再次停下")
|
||||
}
|
||||
if err := json.Unmarshal(body, &stopped); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
frames, err = c.StackTrace(stopped.ThreadID, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("stackTrace: %v", err)
|
||||
}
|
||||
if frames[0].Line != 7 {
|
||||
t.Fatalf("单步后行号: got %d want 7", frames[0].Line)
|
||||
}
|
||||
|
||||
// 继续执行,程序应当跑完。
|
||||
if err := c.Continue(stopped.ThreadID); err != nil {
|
||||
t.Fatalf("continue: %v", err)
|
||||
}
|
||||
if _, ok := log.wait("terminated", 60*time.Second); !ok {
|
||||
t.Fatal("程序没有结束")
|
||||
}
|
||||
if err := <-launched; err != nil {
|
||||
t.Fatalf("launch: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestConditionalBreakpoint 验证条件断点:循环里只有 i == 3 那次才停。
|
||||
func TestConditionalBreakpoint(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("集成测试需要 dlv")
|
||||
}
|
||||
prog := `package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
total := 0
|
||||
for i := 0; i < 5; i++ {
|
||||
total += i
|
||||
}
|
||||
fmt.Println(total)
|
||||
}
|
||||
`
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, "go.mod"), []byte("module demo\n\ngo 1.21\n"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
main := filepath.Join(dir, "main.go")
|
||||
if err := os.WriteFile(main, []byte(prog), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
log := newEventLog()
|
||||
c, err := Start(dir, log.push, t.Logf)
|
||||
if err != nil {
|
||||
t.Skipf("dlv 不可用: %v", err)
|
||||
}
|
||||
defer c.Close()
|
||||
|
||||
if err := c.Initialize(); err != nil {
|
||||
t.Fatalf("initialize: %v", err)
|
||||
}
|
||||
go func() { _ = c.Launch("./", nil, false, 120*time.Second) }()
|
||||
|
||||
if _, ok := log.wait("initialized", 90*time.Second); !ok {
|
||||
t.Fatal("没有收到 initialized 事件")
|
||||
}
|
||||
// 第 8 行是 total += i,条件 i == 3
|
||||
if _, err := c.SetBreakpoints(main, []BreakpointSpec{{Line: 8, Condition: "i == 3"}}); err != nil {
|
||||
t.Fatalf("setBreakpoints: %v", err)
|
||||
}
|
||||
if err := c.ConfigurationDone(); err != nil {
|
||||
t.Fatalf("configurationDone: %v", err)
|
||||
}
|
||||
|
||||
body, ok := log.wait("stopped", 90*time.Second)
|
||||
if !ok {
|
||||
t.Fatal("条件断点没有停下")
|
||||
}
|
||||
var stopped struct {
|
||||
ThreadID int `json:"threadId"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &stopped); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
frames, err := c.StackTrace(stopped.ThreadID, 10)
|
||||
if err != nil || len(frames) == 0 {
|
||||
t.Fatalf("stackTrace: %v", err)
|
||||
}
|
||||
if got := frames[0].Line; got != 8 {
|
||||
t.Fatalf("应停在第 8 行,实际 %d", got)
|
||||
}
|
||||
|
||||
// 停在 i == 3 时,变量 i 的值必须是 3
|
||||
scopes, err := c.Scopes(frames[0].ID)
|
||||
if err != nil {
|
||||
t.Fatalf("scopes: %v", err)
|
||||
}
|
||||
found := ""
|
||||
for _, sc := range scopes {
|
||||
if sc.Expensive || sc.VariablesReference == 0 {
|
||||
continue
|
||||
}
|
||||
vs, err := c.Variables(sc.VariablesReference)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, v := range vs {
|
||||
if v.Name == "i" {
|
||||
found = v.Value
|
||||
}
|
||||
}
|
||||
}
|
||||
if found != "3" {
|
||||
t.Fatalf("条件断点应在 i == 3 时停下,实际 i = %q", found)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user