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,175 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
"github.com/egoist/mygo/ui/editor"
|
||||
|
||||
"mygoland/internal/git"
|
||||
)
|
||||
|
||||
// 差异视图:把「HEAD 里的版本」和「现在编辑器里的内容」并排比。
|
||||
//
|
||||
// 用编辑器自带的 DiffView,不自己算差异。New 侧直接引用已打开文档的缓冲区,
|
||||
// 所以未保存的改动也能看到——这比 git diff 更有用:git 只认已经落盘的。
|
||||
|
||||
// diffView 是差异视图的状态。
|
||||
type diffView struct {
|
||||
open bool
|
||||
path string
|
||||
// title 是标题栏上的说明,为空时显示「左:HEAD 右:编辑器」。
|
||||
title string
|
||||
st editor.DiffState
|
||||
}
|
||||
|
||||
// OpenDiff 打开某个文件的差异视图;不给路径就用当前文件。
|
||||
func (a *App) OpenDiff(path string) {
|
||||
if path == "" {
|
||||
d := a.doc()
|
||||
if d == nil {
|
||||
a.alert("查看差异", "先打开一个文件。")
|
||||
return
|
||||
}
|
||||
path = d.path
|
||||
}
|
||||
if a.root == "" {
|
||||
a.alert("查看差异", "当前没有打开工作区。")
|
||||
return
|
||||
}
|
||||
|
||||
// 新侧:已打开就用缓冲区(含未保存改动),否则读磁盘。
|
||||
var nw *editor.Buffer
|
||||
if d := a.find(path); d != nil {
|
||||
nw = d.buf
|
||||
} else {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
a.alert("查看差异", err.Error())
|
||||
return
|
||||
}
|
||||
text, _, ok := decodeFile(data)
|
||||
if !ok {
|
||||
a.alert("查看差异", "无法识别这个文件的编码。")
|
||||
return
|
||||
}
|
||||
nw = editor.NewBuffer(text)
|
||||
}
|
||||
|
||||
// 旧侧:HEAD 里的内容。新文件取不到,当成空文本。
|
||||
oldText, err := git.ShowFile(a.root, path)
|
||||
if err != nil {
|
||||
oldText = ""
|
||||
}
|
||||
ob := editor.NewBuffer(oldText)
|
||||
|
||||
// 两侧都按文件后缀上高亮,diff 里也有颜色。
|
||||
lang := filepath.Base(path)
|
||||
nw.SetLanguage(editor.LanguageFor(lang))
|
||||
ob.SetLanguage(editor.LanguageFor(lang))
|
||||
|
||||
font, lh, tab, _, _ := a.editorSettings()
|
||||
a.diff = diffView{
|
||||
open: true,
|
||||
path: path,
|
||||
st: editor.DiffState{
|
||||
Old: ob,
|
||||
New: nw,
|
||||
Layout: editor.DiffSplit,
|
||||
Inline: editor.DiffInlineWordAlt,
|
||||
Indicators: editor.DiffBars,
|
||||
ContextLines: 3,
|
||||
Font: font,
|
||||
LineHeight: lh,
|
||||
TabSize: tab,
|
||||
Theme: codeTheme(a.light),
|
||||
},
|
||||
}
|
||||
a.win.Update(func() {})
|
||||
}
|
||||
|
||||
// CloseDiff 关掉差异视图。
|
||||
func (a *App) CloseDiff() {
|
||||
a.diff.open = false
|
||||
a.refresh()
|
||||
}
|
||||
|
||||
// diffActive 供编辑器区判断是否要让位给差异视图。
|
||||
func (a *App) diffActive() bool { return a.diff.open }
|
||||
|
||||
// renderDiff 画差异视图:顶部一条标题栏,下面是并排对比。
|
||||
func (a *App) renderDiff(c *ui.Context) {
|
||||
t := c.Theme()
|
||||
rel := a.diff.path
|
||||
if r, err := filepath.Rel(a.root, a.diff.path); err == nil {
|
||||
rel = filepath.ToSlash(r)
|
||||
}
|
||||
// 左右两栏分别是什么,写在标题栏上:不然用户分不清哪边是历史、哪边是现在。
|
||||
left := "HEAD"
|
||||
right := "编辑器"
|
||||
if a.diff.title != "" {
|
||||
if i := strings.Index(a.diff.title, "↔"); i >= 0 {
|
||||
left = strings.TrimSpace(a.diff.title[:i])
|
||||
right = strings.TrimSpace(strings.SplitN(a.diff.title[i:], "·", 2)[0])
|
||||
right = strings.TrimSpace(strings.TrimPrefix(right, "↔"))
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
// 「差异」做成小胶囊,和文件名分开,视觉上不再是一行平铺的字。
|
||||
ui.Row(c).Padding(1, 6).Radius(3).Background(t.Accent.Alpha(0.16)).
|
||||
Children(func() {
|
||||
ui.Text(c, "差异").FontSize(10).TextColor(t.Accent)
|
||||
})
|
||||
ui.Text(c, rel).FontSize(12).Bold().TextColor(t.Text).SingleLine()
|
||||
ui.Textf(c, "%s ↔ %s", left, right).FontSize(10).
|
||||
TextColor(t.TextMuted).SingleLine()
|
||||
ui.Box(c).Grow(1)
|
||||
if ui.Button(c, "关闭").Clicked() {
|
||||
a.CloseDiff()
|
||||
}
|
||||
})
|
||||
|
||||
editor.DiffView(c, &a.diff.st).Fill()
|
||||
})
|
||||
}
|
||||
|
||||
// diffStatText 返回「+3 −1」这样的改动摘要,给状态栏用。
|
||||
//
|
||||
// 只查 refreshGit 缓存下来的表:状态栏每帧都要画,不能在这里起 git 进程。
|
||||
func (a *App) diffStatText(path string) string {
|
||||
if len(a.gst.stats) == 0 || a.root == "" {
|
||||
return ""
|
||||
}
|
||||
rel, err := filepath.Rel(a.root, path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
n, ok := a.gst.stats[filepath.ToSlash(rel)]
|
||||
if !ok || (n[0] == 0 && n[1] == 0) {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("+%d −%d", n[0], n[1])
|
||||
}
|
||||
|
||||
// currentTextOf 取一个文件「现在」的内容:打开了就用缓冲区(含未保存改动),
|
||||
// 没打开就读磁盘。
|
||||
func (a *App) currentTextOf(path string) string {
|
||||
if d := a.find(path); d != nil {
|
||||
return d.buf.String()
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
text, _, ok := decodeFile(data)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return text
|
||||
}
|
||||
Reference in new issue
Block a user