- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲) - 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表) - 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记 - Git:状态着色、选择性暂存提交、pull/push、历史并排对比 - 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复 - 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈 - 设计文档与验证记录见 docs/plans/
166 lines
3.8 KiB
Go
166 lines
3.8 KiB
Go
package app
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
)
|
|
|
|
// recentItem 是最近打开过的一个工作区。
|
|
type recentItem struct {
|
|
Path string `json:"path"`
|
|
At int64 `json:"at"`
|
|
}
|
|
|
|
// recentLimit 是保留条数。
|
|
const recentLimit = 10
|
|
|
|
// recentPath 返回最近项目文件的路径。
|
|
func recentPath() string {
|
|
dir := sessionDir()
|
|
if dir == "" {
|
|
return ""
|
|
}
|
|
return filepath.Join(dir, "recent.json")
|
|
}
|
|
|
|
// loadRecent 读最近项目,顺手滤掉已经不存在的目录。
|
|
func loadRecent() []recentItem {
|
|
p := recentPath()
|
|
if p == "" {
|
|
return nil
|
|
}
|
|
b, err := os.ReadFile(p)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
var items []recentItem
|
|
if err := json.Unmarshal(b, &items); err != nil {
|
|
logf("读取最近项目失败: %v", err)
|
|
return nil
|
|
}
|
|
out := make([]recentItem, 0, len(items))
|
|
for _, it := range items {
|
|
if it.Path == "" {
|
|
continue
|
|
}
|
|
if st, err := os.Stat(it.Path); err != nil || !st.IsDir() {
|
|
continue
|
|
}
|
|
out = append(out, it)
|
|
}
|
|
sort.Slice(out, func(i, j int) bool { return out[i].At > out[j].At })
|
|
if len(out) > recentLimit {
|
|
out = out[:recentLimit]
|
|
}
|
|
return out
|
|
}
|
|
|
|
// saveRecent 写最近项目。
|
|
func saveRecent(items []recentItem) {
|
|
p := recentPath()
|
|
if p == "" {
|
|
return
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
return
|
|
}
|
|
b, err := json.MarshalIndent(items, "", " ")
|
|
if err != nil {
|
|
return
|
|
}
|
|
if err := os.WriteFile(p, b, 0o644); err != nil {
|
|
logf("保存最近项目失败: %v", err)
|
|
}
|
|
}
|
|
|
|
// addRecent 把一个工作区插到列表最前面(已有的去重)。
|
|
func addRecent(path string) {
|
|
if path == "" {
|
|
return
|
|
}
|
|
clean := filepath.Clean(path)
|
|
items := loadRecent()
|
|
out := make([]recentItem, 0, len(items)+1)
|
|
// 用纳秒:同一秒内连开两个项目也要能排出先后。
|
|
out = append(out, recentItem{Path: clean, At: time.Now().UnixNano()})
|
|
for _, it := range items {
|
|
if sameDir(it.Path, clean) {
|
|
continue
|
|
}
|
|
out = append(out, it)
|
|
}
|
|
if len(out) > recentLimit {
|
|
out = out[:recentLimit]
|
|
}
|
|
saveRecent(out)
|
|
}
|
|
|
|
// sameDir 比较两个路径是否同一目录(Windows 下忽略大小写)。
|
|
func sameDir(a, b string) bool {
|
|
return strings.EqualFold(filepath.Clean(a), filepath.Clean(b))
|
|
}
|
|
|
|
// OpenRecent 打开「最近项目」列表。
|
|
func (a *App) OpenRecent() {
|
|
items := loadRecent()
|
|
if len(items) == 0 {
|
|
a.alert("最近项目", "还没有打开过任何项目")
|
|
return
|
|
}
|
|
list := make([]paletteItem, 0, len(items))
|
|
for i, it := range items {
|
|
list = append(list, paletteItem{
|
|
Kind: itemFile,
|
|
Path: it.Path,
|
|
Label: filepath.Base(it.Path),
|
|
Hint: it.Path,
|
|
Idx: i,
|
|
})
|
|
}
|
|
a.recent = items
|
|
a.pal = paletteState{open: true, recent: true, refsAll: list, items: list}
|
|
a.win.Update(func() {})
|
|
}
|
|
|
|
// recentView 在欢迎页列出最近打开的项目。
|
|
func (a *App) recentView(c *ui.Context) {
|
|
t := c.Theme()
|
|
items := loadRecent()
|
|
if len(items) == 0 {
|
|
return
|
|
}
|
|
ui.Box(c).Height(10)
|
|
ui.Text(c, "最近打开").FontSize(11).TextColor(t.TextMuted)
|
|
ui.Column(c).Gap(2).Width(360).Children(func() {
|
|
for _, it := range items {
|
|
item := it
|
|
row := ui.ButtonBase(c).FillWidth().Padding(4, 8).Gap(8).Radius(4).
|
|
Justify(ui.Start).AlignItems(ui.Center).Cursor(ui.CursorPointer)
|
|
if row.Hovered() {
|
|
row = row.Background(t.SurfaceHover)
|
|
}
|
|
row.Children(func() {
|
|
ui.Text(c, filepath.Base(item.Path)).FontSize(12).TextColor(t.Text).SingleLine()
|
|
ui.Text(c, item.Path).FontSize(10).TextColor(t.TextMuted).SingleLine().Grow(1)
|
|
})
|
|
if row.Clicked() {
|
|
go a.switchWorkspace(item.Path)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// switchWorkspace 切到指定工作区(用 goroutine 打开对话框外的场景)。
|
|
func (a *App) switchWorkspace(dir string) {
|
|
a.setRoot(dir)
|
|
if a.win != nil {
|
|
a.win.Update(func() {})
|
|
}
|
|
}
|