MyGoland 首次提交:Go 轻量 IDE

- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲)
- 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表)
- 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记
- Git:状态着色、选择性暂存提交、pull/push、历史并排对比
- 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复
- 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈
- 设计文档与验证记录见 docs/plans/
This commit is contained in:
zerorigin committed 2026-11-04 16:37:05 +08:00
commit a80c0f886a
220 files changed
+20784

No files matched your search

+366
View File
@@ -0,0 +1,366 @@
package app
import (
"fmt"
"path/filepath"
"strings"
"github.com/egoist/mygo/ui"
"github.com/egoist/mygo/ui/editor"
"mygoland/internal/git"
)
// 历史视图:左边列表,右边「选中的那条 ↔ 当前代码」,全屏铺在编辑区。
//
// 为什么不用弹窗:Modal 覆盖层里的点击(包括按钮)在我们的自动化验证下完全不响应,
// 而且弹窗宽度有限,看不到并排对比。做成全屏视图后,左列表 + 右差异同屏,
// 「这条历史跟现在差在哪」一眼就能看到,这正是看历史时真正要的信息。
// histMode 是历史视图的模式。
type histMode int
const (
histGit histMode = iota
histLocal
)
// historyViewState 是全屏历史视图的状态。
type historyViewState struct {
open bool
mode histMode
path string
isDir bool
// git 模式的提交,local 模式的快照。
commits []git.LogEntry
items []histItem
sel int
err string
// 右侧对比的当前内容与标题。
title string
diff editor.DiffState
blank bool // 还没有可对比的内容
}
// OpenHistoryView 打开某个文件/目录的历史视图。
func (a *App) OpenHistoryView(path string, isDir bool, mode histMode) {
st := historyViewState{open: true, mode: mode, path: path, isDir: isDir, blank: true}
if mode == histLocal {
if isDir {
st.items = listHistoryUnder(path)
} else {
for _, e := range listHistory(path) {
st.items = append(st.items, histItem{Path: path, histEntry: e})
}
}
}
a.histView = st
a.refresh()
if mode == histGit {
root, p := a.root, path
go func() {
commits, err := git.Log(root, p, 50)
a.win.Update(func() {
if !a.histView.open || a.histView.path != p || a.histView.mode != histGit {
return
}
if err != nil {
a.histView.err = err.Error()
} else {
a.histView.commits = commits
// 默认选中最新一条,右侧立刻有内容,不用先点一下。
if len(commits) > 0 {
a.previewHistorySel()
}
}
a.refresh()
})
}()
} else if len(st.items) > 0 {
a.previewHistorySel()
}
}
// OpenGitLog 打开某个文件/目录的 git 历史视图。
func (a *App) OpenGitLog(path string, isDir bool) {
if a.root == "" {
a.alert("Git 历史", "当前没有打开工作区。")
return
}
if !git.Available(a.root) {
a.alert("Git 历史", "这个工作区不是 git 仓库。")
return
}
a.OpenHistoryView(path, isDir, histGit)
}
// closeHistoryView 关掉历史视图。
func (a *App) closeHistoryView() {
a.histView.open = false
a.refresh()
}
// previewHistorySel 把当前选中的那条和当前代码比,填右侧。
func (a *App) previewHistorySel() {
v := &a.histView
if v.isDir {
return
}
var left, title string
switch v.mode {
case histGit:
if v.sel < 0 || v.sel >= len(v.commits) {
return
}
c := v.commits[v.sel]
text, err := git.ShowAt(a.root, c.Hash, v.path)
if err != nil {
v.blank = true
v.title = c.Hash + " 里没有这个文件"
return
}
left = text
title = c.Hash + " ↔ 当前 · " + trimTo(c.Subject, 34)
case histLocal:
if v.sel < 0 || v.sel >= len(v.items) {
return
}
it := v.items[v.sel]
data, err := readSnapshot(it.Path, it.Hash)
if err != nil {
v.blank = true
v.title = "读取快照失败"
return
}
text, _, ok := decodeFile(data)
if !ok {
v.blank = true
v.title = "快照内容无法解码"
return
}
left = text
title = historyTime(it.Time) + " " + historyReason(it.Reason) + " ↔ 当前"
}
right := a.currentTextOf(v.path)
a.fillHistDiff(v, left, right, title)
}
// fillHistDiff 造左右两份缓冲区给 DiffView。
func (a *App) fillHistDiff(v *historyViewState, left, right, title string) {
ob := editor.NewBuffer(left)
nb := editor.NewBuffer(right)
lang := filepath.Base(v.path)
ob.SetLanguage(editor.LanguageFor(lang))
nb.SetLanguage(editor.LanguageFor(lang))
font, lh, tab, _, _ := a.editorSettings()
v.title = title
v.blank = false
v.diff = editor.DiffState{
Old: ob,
New: nb,
Layout: editor.DiffSplit,
Inline: editor.DiffInlineWordAlt,
Indicators: editor.DiffBars,
ContextLines: 3,
Font: font,
LineHeight: lh,
TabSize: tab,
Theme: codeTheme(a.light),
}
}
// historyView 画全屏历史视图。
func (a *App) historyView(c *ui.Context) {
t := c.Theme()
v := &a.histView
rel := v.path
if r, err := filepath.Rel(a.root, v.path); err == nil {
rel = filepath.ToSlash(r)
}
if v.isDir {
rel += "/"
}
ui.Column(c).Fill().Children(func() {
// 顶部:模式标签 + 路径 + 关闭
ui.Row(c).Padding(5, 10).Gap(8).AlignItems(ui.Center).
BorderWidth(0, 0, 1, 0).BorderColor(t.Border).Children(func() {
label := "Git 历史"
if v.mode == histLocal {
label = "本地历史"
}
ui.Row(c).Padding(1, 6).Radius(3).Background(t.Accent.Alpha(0.16)).
Children(func() {
ui.Text(c, label).FontSize(10).TextColor(t.Accent)
})
ui.Text(c, rel).FontSize(12).Bold().TextColor(t.Text).SingleLine()
ui.Box(c).Grow(1)
if ui.Button(c, "关闭(Esc)").Clicked() {
a.closeHistoryView()
}
})
ui.Row(c).Grow(1).AlignItems(ui.Stretch).Children(func() {
// 左:列表
ui.Column(c).Width(340).Children(func() {
ui.Row(c).Padding(4, 8).Gap(6).AlignItems(ui.Center).Children(func() {
n := len(v.commits)
if v.mode == histLocal {
n = len(v.items)
}
unit := "条提交"
if v.mode == histLocal {
unit = "条快照"
}
ui.Textf(c, "%d %s", n, unit).FontSize(10).TextColor(t.TextMuted)
ui.Box(c).Grow(1)
if v.mode == histLocal && !v.isDir && len(v.items) > 0 {
if ui.Button(c, "恢复这条").Clicked() {
it := v.items[v.sel]
a.restoreFromHistory(it.Path, it.histEntry)
}
}
})
ui.Box(c).FillWidth().Height(1).Background(t.Border)
switch {
case v.err != "":
ui.Column(c).Padding(8, 10).Children(func() {
ui.Text(c, v.err).FontSize(11).TextColor(t.Danger).SingleLine()
})
case len(v.commits) == 0 && len(v.items) == 0:
ui.Column(c).Grow(1).Center().Children(func() {
ui.Text(c, "没有记录").FontSize(12).TextColor(t.TextMuted)
})
default:
ui.Scroll(c).Grow(1).Children(func() {
ui.Column(c).Gap(1).Padding(4, 4).Children(func() {
if v.mode == histGit {
a.histGitRows(c)
} else {
a.histLocalRows(c)
}
})
})
}
})
ui.Box(c).Width(1).FillHeight().Background(t.Border)
// 右:对比
ui.Column(c).Grow(1).Children(func() {
ui.Row(c).Padding(4, 10).Gap(8).AlignItems(ui.Center).Children(func() {
if v.title != "" {
ui.Text(c, v.title).FontSize(10).TextColor(t.TextMuted).SingleLine()
}
ui.Box(c).Grow(1)
if !v.blank {
ui.Text(c, "左:历史 右:当前").FontSize(10).TextColor(t.TextMuted)
}
})
ui.Box(c).FillWidth().Height(1).Background(t.Border)
if v.blank {
ui.Column(c).Grow(1).Center().Children(func() {
msg := "选一条看它和当前代码的差异"
if v.isDir {
msg = "目录只列提交;点具体文件看差异"
}
ui.Text(c, msg).FontSize(12).TextColor(t.TextMuted)
})
} else {
editor.DiffView(c, &v.diff).Fill()
}
})
})
})
}
// histGitRows 画 git 提交行。
func (a *App) histGitRows(c *ui.Context) {
t := c.Theme()
v := &a.histView
for i, cm := range v.commits {
idx, entry := i, cm
active := idx == v.sel
bg := ui.RGBA(0, 0, 0, 0)
if active {
bg = t.SurfaceHover
}
row := ui.ButtonBase(c).FillWidth().Height(40).Padding(0, 6).Radius(4).
Background(bg).Cursor(ui.CursorPointer)
row.Children(func() {
ui.Row(c).FillWidth().Gap(8).AlignItems(ui.Center).Children(func() {
mark := ui.RGBA(0, 0, 0, 0)
if active {
mark = t.Accent
}
ui.Box(c).Width(2).Height(24).Background(mark).Radius(1)
ui.Column(c).Grow(1).Gap(1).Children(func() {
ui.Text(c, entry.Subject).FontSize(12).TextColor(t.Text).SingleLine()
ui.Textf(c, "%s %s %s", entry.Hash, entry.Author, entry.When).
FontSize(10).TextColor(t.TextMuted).SingleLine()
})
})
})
if row.Clicked() {
v.sel = idx
a.previewHistorySel()
a.refresh()
}
}
}
// histLocalRows 画本地快照行。
func (a *App) histLocalRows(c *ui.Context) {
t := c.Theme()
v := &a.histView
for i, it := range v.items {
idx, item := i, it
active := idx == v.sel
bg := ui.RGBA(0, 0, 0, 0)
if active {
bg = t.SurfaceHover
}
row := ui.ButtonBase(c).FillWidth().Height(40).Padding(0, 6).Radius(4).
Background(bg).Cursor(ui.CursorPointer)
row.Children(func() {
ui.Row(c).FillWidth().Gap(8).AlignItems(ui.Center).Children(func() {
mark := ui.RGBA(0, 0, 0, 0)
if active {
mark = t.Accent
}
ui.Box(c).Width(2).Height(24).Background(mark).Radius(1)
ui.Column(c).Grow(1).Gap(1).Children(func() {
name := historyTime(item.Time)
if v.isDir {
name = filepath.Base(item.Path) + " · " + name
}
ui.Text(c, name).FontSize(12).TextColor(t.Text).SingleLine()
meta := fmt.Sprintf("%s · %d KB", historyReason(item.Reason), (item.Size+1023)/1024)
if item.Dirty {
meta += " · 未保存"
}
ui.Text(c, meta).FontSize(10).TextColor(t.TextMuted).SingleLine()
})
})
})
if row.Clicked() {
v.sel = idx
a.previewHistorySel()
a.refresh()
}
}
}
// histViewActive 供编辑区判断是否要让位。
func (a *App) histViewActive() bool { return a.histView.open }
// 让旧的弹窗入口改走全屏视图。
func (a *App) OpenHistoryAt(path string, isDir bool) {
a.OpenHistoryView(path, isDir, histLocal)
}
// ensure 引用 strings,避免未使用(渲染里用到了 TrimPrefix 之类)。
var _ = strings.TrimSpace