package app import ( "path/filepath" "sort" "strings" "github.com/egoist/mygo/ui" ) // breakpoint 是一个断点的附加信息。 type breakpoint struct { // Cond 是命中条件(DAP 的 condition),空表示无条件。 Cond string // Enabled 为假时保留但不生效,调试时不会发给适配器。 Enabled bool } // bpRef 是断点的一个引用,用于列表显示。 type bpRef struct { Path string Line int BP breakpoint } // toggleBreakpoint 在指定行加/删断点。 func (a *App) toggleBreakpoint(path string, line int) { if a.bps[path] == nil { a.bps[path] = map[int]breakpoint{} } if _, ok := a.bps[path][line]; ok { delete(a.bps[path], line) if len(a.bps[path]) == 0 { delete(a.bps, path) } } else { a.bps[path][line] = breakpoint{Enabled: true} } a.afterBreakpointChange(path) } // setBreakpointCond 设置命中条件。 func (a *App) setBreakpointCond(path string, line int, cond string) { m := a.bps[path] if m == nil { return } bp, ok := m[line] if !ok { return } bp.Cond = cond m[line] = bp a.afterBreakpointChange(path) } // setBreakpointEnabled 启用/禁用断点。 func (a *App) setBreakpointEnabled(path string, line int, on bool) { m := a.bps[path] if m == nil { return } bp, ok := m[line] if !ok { return } bp.Enabled = on m[line] = bp a.afterBreakpointChange(path) } // deleteBreakpoint 删除断点。 func (a *App) deleteBreakpoint(path string, line int) { m := a.bps[path] if m == nil { return } delete(m, line) if len(m) == 0 { delete(a.bps, path) } a.afterBreakpointChange(path) } // afterBreakpointChange 断点变动后:重画该文件的断点标记、同步给调试会话、存会话。 func (a *App) afterBreakpointChange(path string) { if d := a.find(path); d != nil { a.decorate(d) } a.pushBreakpoints(path) a.saveSession() if a.win != nil { a.win.Update(func() {}) } } // allBreakpoints 按文件、行号排序列出所有断点。 func (a *App) allBreakpoints() []bpRef { var out []bpRef for path, m := range a.bps { for line, bp := range m { out = append(out, bpRef{Path: path, Line: line, BP: bp}) } } sort.Slice(out, func(i, j int) bool { if out[i].Path != out[j].Path { return out[i].Path < out[j].Path } return out[i].Line < out[j].Line }) return out } // breakpointView 是面板里的「断点」页。 func (a *App) breakpointView(c *ui.Context) { t := c.Theme() items := a.allBreakpoints() if len(items) == 0 { ui.Text(c, "还没有断点。在行号上点一下即可添加,点「条件」可以设置命中条件。"). FontSize(11).TextColor(t.TextMuted).Padding(8, 6) return } ui.Column(c).Gap(1).Padding(4, 0).Children(func() { for _, ref := range items { a.breakpointRow(c, ref) } }) } // breakpointRow 画一行断点。 func (a *App) breakpointRow(c *ui.Context, ref bpRef) { t := c.Theme() row := ui.ButtonBase(c).FillWidth().Padding(2, 6).Gap(8).Radius(3). Justify(ui.Start).AlignItems(ui.Center).Cursor(ui.CursorPointer) if row.Hovered() { row = row.Background(t.SurfaceHover) } row.Children(func() { // 勾选框控制启用状态。 on := ref.BP.Enabled if ui.Checkbox(c, &on, "").Changed() { a.setBreakpointEnabled(ref.Path, ref.Line, on) } ui.Text(c, a.shortPath(ref.Path)).FontSize(12).TextColor(t.Text).SingleLine() ui.Textf(c, "%d", ref.Line).FontSize(11).TextColor(t.TextMuted) if ref.BP.Cond != "" { ui.Text(c, "当 "+ref.BP.Cond).FontSize(11).TextColor(t.Warning).SingleLine() } ui.Box(c).Grow(1) if ui.Button(c, "条件").Clicked() { a.openCondDialog(ref.Path, ref.Line, ref.BP.Cond) } if ui.Button(c, "删除").Clicked() { a.deleteBreakpoint(ref.Path, ref.Line) } }) // 点空白处跳到该断点。 if row.Clicked() { go a.openAtLine(ref.Path, ref.Line) } } // shortPath 把绝对路径缩成工作区相对路径。 func (a *App) shortPath(p string) string { if a.root != "" { if rel, err := filepath.Rel(a.root, p); err == nil { return filepath.ToSlash(rel) } } return p } // openCondDialog 打开条件输入框。 func (a *App) openCondDialog(path string, line int, cond string) { a.bpDlg = bpDialogState{open: true, dlg: true, path: path, line: line, cond: cond} a.win.Update(func() {}) } // bpDialogState 是条件断点弹窗的状态。 type bpDialogState struct { open bool dlg bool path string line int cond string } // breakpointCondDialog 画条件输入弹窗。 func (a *App) breakpointCondDialog(c *ui.Context) { if !a.bpDlg.open { return } t := c.Theme() ui.Modal(c, &a.bpDlg.dlg, func() { ui.Column(c).Width(460).Gap(10).Children(func() { ui.Text(c, "断点条件").FontSize(13).Bold().TextColor(t.Text) ui.Textf(c, "%s 第 %d 行", a.shortPath(a.bpDlg.path), a.bpDlg.line). FontSize(10).TextColor(t.TextMuted).SingleLine() inp := ui.TextInput(c, &a.bpDlg.cond).FillWidth().FontSize(13).AutoFocus() if inp.Submitted() { a.confirmCond() } ui.Text(c, "留空表示无条件。例如 i == 3 或 name != \"\""). FontSize(10).TextColor(t.TextMuted).SingleLine() ui.Row(c).Gap(8).Justify(ui.End).Children(func() { if ui.Button(c, "取消").Clicked() { a.bpDlg.open = false } if ui.Button(c, "确定").Clicked() { a.confirmCond() } }) }) }) if !a.bpDlg.dlg && a.bpDlg.open { a.bpDlg.dlg = true a.win.Update(func() {}) } if !a.bpDlg.open && a.bpDlg.dlg { a.bpDlg.dlg = false a.focusEditorSoon() } } // confirmCond 保存条件。 func (a *App) confirmCond() { a.setBreakpointCond(a.bpDlg.path, a.bpDlg.line, strings.TrimSpace(a.bpDlg.cond)) a.bpDlg.open = false a.win.Update(func() {}) }