- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲) - 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表) - 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记 - Git:状态着色、选择性暂存提交、pull/push、历史并排对比 - 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复 - 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈 - 设计文档与验证记录见 docs/plans/
334 lines
8.3 KiB
Go
334 lines
8.3 KiB
Go
package app
|
|
|
|
import (
|
|
"crypto/sha1"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/egoist/mygo/ui/editor"
|
|
)
|
|
|
|
// sessionState 是关掉再打开时要恢复的界面状态。
|
|
//
|
|
// 存在用户配置目录而不是工作区里:这些是「这台机器上的使用习惯」,
|
|
// 不该跟着仓库一起提交。
|
|
type sessionState struct {
|
|
Root string `json:"root"`
|
|
Tabs []string `json:"tabs"`
|
|
Active string `json:"active"`
|
|
Breakpoints map[string][]bpJSON `json:"breakpoints,omitempty"`
|
|
Panel bool `json:"panel"`
|
|
PanelTab int `json:"panelTab"`
|
|
PanelH float32 `json:"panelH"`
|
|
Sidebar bool `json:"sidebar"`
|
|
SidebarW float32 `json:"sidebarW"`
|
|
PanelKind int `json:"panelKind"`
|
|
Light bool `json:"light"`
|
|
ShellIdx int `json:"shellIdx"`
|
|
// Terms 存每个终端会话用的外壳编号,TermIdx 是当前显示的那个。
|
|
Terms []int `json:"terms,omitempty"`
|
|
TermIdx int `json:"termIdx"`
|
|
// Cursors 记每个文件上次的光标与滚动位置,键是绝对路径。
|
|
Cursors map[string]cursorJSON `json:"cursors,omitempty"`
|
|
}
|
|
|
|
// cursorJSON 是一个文件的位置记忆。
|
|
type cursorJSON struct {
|
|
Line int `json:"line"` // 0 起
|
|
Column int `json:"column"` // 0 起
|
|
ScrollY float64 `json:"scrollY"`
|
|
}
|
|
|
|
// bpJSON 是断点在会话文件里的形态。
|
|
//
|
|
// 老版本写的是纯行号数组,UnmarshalJSON 兼容一下,免得升级后断点全丢。
|
|
type bpJSON struct {
|
|
Line int `json:"line"`
|
|
Cond string `json:"cond,omitempty"`
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
|
|
func (b *bpJSON) UnmarshalJSON(data []byte) error {
|
|
var n int
|
|
if err := json.Unmarshal(data, &n); err == nil {
|
|
b.Line, b.Enabled = n, true
|
|
return nil
|
|
}
|
|
type plain bpJSON
|
|
var v plain
|
|
if err := json.Unmarshal(data, &v); err != nil {
|
|
return err
|
|
}
|
|
*b = bpJSON(v)
|
|
return nil
|
|
}
|
|
|
|
// sessionDir 返回存会话文件的目录。
|
|
func sessionDir() string {
|
|
base, err := os.UserConfigDir()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return filepath.Join(base, "mygoland")
|
|
}
|
|
|
|
// sessionPath 按工作区路径散列出一个文件名,不同项目互不干扰。
|
|
func sessionPath(root string) string {
|
|
dir := sessionDir()
|
|
if dir == "" || root == "" {
|
|
return ""
|
|
}
|
|
sum := sha1.Sum([]byte(strings.ToLower(filepath.Clean(root))))
|
|
return filepath.Join(dir, fmt.Sprintf("session-%x.json", sum[:6]))
|
|
}
|
|
|
|
// lastPath 记录上次打开的工作区,启动时没带参数就用它。
|
|
func lastPath() string {
|
|
dir := sessionDir()
|
|
if dir == "" {
|
|
return ""
|
|
}
|
|
return filepath.Join(dir, "last.json")
|
|
}
|
|
|
|
// saveSession 把当前界面状态写盘。只在主线程调用。
|
|
func (a *App) saveSession() {
|
|
if a.restoring || a.root == "" {
|
|
return
|
|
}
|
|
p := sessionPath(a.root)
|
|
if p == "" {
|
|
return
|
|
}
|
|
bps := map[string][]bpJSON{}
|
|
for path, m := range a.bps {
|
|
if len(m) == 0 {
|
|
continue
|
|
}
|
|
lines := make([]bpJSON, 0, len(m))
|
|
for line, bp := range m {
|
|
lines = append(lines, bpJSON{Line: line, Cond: bp.Cond, Enabled: bp.Enabled})
|
|
}
|
|
sort.Slice(lines, func(i, j int) bool { return lines[i].Line < lines[j].Line })
|
|
bps[path] = lines
|
|
}
|
|
|
|
st := sessionState{
|
|
Root: a.root,
|
|
Active: a.active,
|
|
Breakpoints: bps,
|
|
Panel: a.panel,
|
|
PanelTab: a.panelTab,
|
|
PanelH: a.panelH,
|
|
Sidebar: a.sidebarOn,
|
|
SidebarW: a.sidebar,
|
|
PanelKind: a.panelKind,
|
|
Light: a.light,
|
|
ShellIdx: a.shellIdx,
|
|
Terms: a.termShells(),
|
|
TermIdx: a.termIdx,
|
|
}
|
|
st.Cursors = map[string]cursorJSON{}
|
|
for _, d := range a.docs {
|
|
st.Tabs = append(st.Tabs, d.path)
|
|
if d.st == nil {
|
|
continue
|
|
}
|
|
// 大文件是只读截断的,位置意义不大,跳过。
|
|
if d.truncated {
|
|
continue
|
|
}
|
|
pos := d.st.CursorPos()
|
|
st.Cursors[d.path] = cursorJSON{
|
|
Line: pos.Line,
|
|
Column: pos.Col,
|
|
ScrollY: d.st.ScrollY(),
|
|
}
|
|
}
|
|
b, err := json.MarshalIndent(st, "", " ")
|
|
if err != nil {
|
|
logf("保存会话失败: %v", err)
|
|
return
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
logf("保存会话失败: %v", err)
|
|
return
|
|
}
|
|
if err := os.WriteFile(p, b, 0o644); err != nil {
|
|
logf("保存会话失败: %v", err)
|
|
return
|
|
}
|
|
// 顺手记下最后的工作区,下次没带参数时直接打开它。
|
|
last := fmt.Sprintf("{\"root\":%q}", a.root)
|
|
_ = os.WriteFile(lastPath(), []byte(last), 0o644)
|
|
}
|
|
|
|
// loadSession 读回某个工作区的会话状态。
|
|
func loadSession(root string) (sessionState, bool) {
|
|
var st sessionState
|
|
p := sessionPath(root)
|
|
if p == "" {
|
|
return st, false
|
|
}
|
|
b, err := os.ReadFile(p)
|
|
if err != nil {
|
|
return st, false
|
|
}
|
|
if err := json.Unmarshal(b, &st); err != nil {
|
|
logf("读取会话失败: %v", err)
|
|
return st, false
|
|
}
|
|
return st, true
|
|
}
|
|
|
|
// lastWorkspace 返回上次打开的工作区路径,目录不存在时返回空。
|
|
func lastWorkspace() string {
|
|
b, err := os.ReadFile(lastPath())
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
var v struct {
|
|
Root string `json:"root"`
|
|
}
|
|
if err := json.Unmarshal(b, &v); err != nil || v.Root == "" {
|
|
return ""
|
|
}
|
|
if st, err := os.Stat(v.Root); err != nil || !st.IsDir() {
|
|
return ""
|
|
}
|
|
return v.Root
|
|
}
|
|
|
|
// restoreSession 恢复某个工作区的界面状态。
|
|
func (a *App) restoreSession(root string) {
|
|
st, ok := loadSession(root)
|
|
if !ok {
|
|
return
|
|
}
|
|
a.applySession(st)
|
|
}
|
|
|
|
// applySession 把会话状态套到当前界面上。
|
|
//
|
|
// 期间把 restoring 置真:恢复标签页会触发 loadDoc,那里面会存会话,
|
|
// 不挡住就会用「恢复了一半」的状态覆盖掉原文件。
|
|
func (a *App) applySession(st sessionState) {
|
|
a.restoring = true
|
|
defer func() { a.restoring = false }()
|
|
|
|
if st.Light != a.light {
|
|
a.SetLight(st.Light)
|
|
}
|
|
if st.SidebarW >= sidebarMin {
|
|
a.sidebar = st.SidebarW
|
|
}
|
|
a.sidebarOn = st.Sidebar
|
|
a.panelKind = st.PanelKind
|
|
a.panel = st.Panel
|
|
a.panelTab = st.PanelTab
|
|
if st.PanelH >= 80 {
|
|
a.panelH = st.PanelH
|
|
}
|
|
if st.ShellIdx >= 0 && st.ShellIdx < 3 {
|
|
a.shellIdx = st.ShellIdx
|
|
}
|
|
// 终端会话只恢复「有几个、各是什么外壳」,进程等切到终端页再启动——
|
|
// 开机就把几个 shell 全拉起来没有必要。
|
|
for _, sh := range st.Terms {
|
|
if sh < 0 || sh > 2 {
|
|
sh = 0
|
|
}
|
|
a.terms = append(a.terms, &termSession{shell: sh, state: "启动中…"})
|
|
}
|
|
if len(a.terms) > 0 {
|
|
a.termIdx = st.TermIdx
|
|
if a.termIdx < 0 || a.termIdx >= len(a.terms) {
|
|
a.termIdx = 0
|
|
}
|
|
}
|
|
for path, lines := range st.Breakpoints {
|
|
if len(lines) == 0 {
|
|
continue
|
|
}
|
|
m := map[int]breakpoint{}
|
|
for _, ln := range lines {
|
|
m[ln.Line] = breakpoint{Cond: ln.Cond, Enabled: ln.Enabled}
|
|
}
|
|
a.bps[path] = m
|
|
}
|
|
for _, p := range st.Tabs {
|
|
if _, err := os.Stat(p); err != nil {
|
|
continue // 文件可能已经被删了
|
|
}
|
|
d, err := a.loadDoc(p)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
if c, ok := st.Cursors[p]; ok {
|
|
// 位置要等编辑器排过版再套,否则滚动距离算不准。
|
|
a.pendingCursor = append(a.pendingCursor, cursorRestore{doc: d, cur: c})
|
|
}
|
|
}
|
|
if st.Active != "" {
|
|
if d := a.find(st.Active); d != nil {
|
|
a.active = d.path
|
|
}
|
|
}
|
|
a.syncTitle()
|
|
}
|
|
|
|
// cursorRestore 是一条待应用的「回到原位」请求。
|
|
type cursorRestore struct {
|
|
doc *Doc
|
|
cur cursorJSON
|
|
}
|
|
|
|
// applyPendingCursor 把上次的光标与滚动位置套回编辑器。
|
|
//
|
|
// 必须等编辑器排过版再做:滚动距离依赖内容高度,一打开就设会被算成 0。
|
|
// 只处理一次,之后用户的滚动就归用户了。
|
|
func (a *App) applyPendingCursor() {
|
|
if len(a.pendingCursor) == 0 {
|
|
return
|
|
}
|
|
batch := a.pendingCursor
|
|
a.pendingCursor = nil
|
|
for _, it := range batch {
|
|
if it.doc == nil || it.doc.st == nil || !a.hasDoc(it.doc) {
|
|
continue
|
|
}
|
|
line := it.cur.Line
|
|
if line < 0 {
|
|
line = 0
|
|
}
|
|
if line >= it.doc.buf.LineCount() {
|
|
line = it.doc.buf.LineCount() - 1
|
|
}
|
|
col := it.cur.Column
|
|
lineLen := len(it.doc.buf.Line(line))
|
|
if col > lineLen {
|
|
col = lineLen
|
|
}
|
|
// GoTo 会把光标行滚到中间,所以先定位、再把滚动条拉回原位。
|
|
it.doc.st.GoTo(editor.Pos{Line: line, Col: col})
|
|
if it.cur.ScrollY > 0 {
|
|
it.doc.st.SetScrollY(it.cur.ScrollY)
|
|
}
|
|
}
|
|
a.refresh()
|
|
}
|
|
|
|
// hasDoc 判断文档还在打开列表里。
|
|
func (a *App) hasDoc(target *Doc) bool {
|
|
for _, d := range a.docs {
|
|
if d == target {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|