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

164 lines
3.7 KiB
Go

package app
import (
"context"
"os"
"path/filepath"
"time"
"github.com/egoist/mygo"
)
// watchDebounce 是磁盘事件去抖时长:一次保存常常连发多个事件。
const watchDebounce = 300 * time.Millisecond
// docWatch 记录一个文件的监听器与最近一次已知的磁盘状态。
type docWatch struct {
w *mygo.FileWatcher
ctx context.Context
cancel context.CancelFunc
at time.Time
size int64
}
// stamp 记录文件当前的时间戳与大小,用于识别「自己的保存」。
func (dw *docWatch) stamp(path string) {
st, err := os.Stat(path)
if err != nil {
return
}
dw.at, dw.size = st.ModTime(), st.Size()
}
// unchanged 判断磁盘状态是否与记录一致。
func (dw *docWatch) unchanged(st os.FileInfo) bool {
if dw == nil {
return false
}
return dw.at.Equal(st.ModTime()) && dw.size == st.Size()
}
// startWatch 开始监听文件在编辑器外的改动。
func (a *App) startWatch(d *Doc) {
if d.watch != nil || a.win == nil || d.path == "" {
return
}
ctx, cancel := context.WithCancel(context.Background())
w, err := mygo.WatchFiles(ctx, d.path, mygo.FileWatchOptions{Debounce: watchDebounce})
if err != nil {
cancel()
logf("监听 %s 失败: %v", d.path, err)
return
}
dw := &docWatch{w: w, ctx: ctx, cancel: cancel}
dw.stamp(d.path)
d.watch = dw
go func() {
for {
if _, err := w.Next(ctx); err != nil {
return
}
a.win.Update(func() { a.onDiskChange(d) })
}
}()
}
// stopWatch 结束监听。
func (d *Doc) stopWatch() {
if d.watch == nil {
return
}
d.watch.cancel()
_ = d.watch.w.Close()
d.watch = nil
}
// onDiskChange 处理磁盘上的改动,须在主线程调用。
func (a *App) onDiskChange(d *Doc) {
if a.find(d.path) != d {
d.stopWatch()
return
}
st, err := os.Stat(d.path)
if err != nil {
if os.IsNotExist(err) {
a.appendOut("文件已从磁盘删除:" + filepath.Base(d.path))
d.watch = nil
}
return
}
if d.watch != nil && d.watch.unchanged(st) {
return
}
data, err := os.ReadFile(d.path)
if err != nil {
return
}
text := string(data)
if text == d.buf.String() {
if d.watch != nil {
d.watch.at, d.watch.size = st.ModTime(), st.Size()
}
return
}
if d.buf.Modified() {
a.askConflict(d, text)
return
}
a.reloadDoc(d, text)
if d.watch != nil {
d.watch.at, d.watch.size = st.ModTime(), st.Size()
}
logf("reload %s after external change", d.path)
a.appendOut("文件已在编辑器外更改,已重新加载:" + filepath.Base(d.path))
}
// reloadDoc 用磁盘内容替换缓冲区,旧诊断一并清掉。
func (a *App) reloadDoc(d *Doc, text string) {
d.buf.Reset(text)
if d.st != nil {
d.st.SetDiagnostics(d.buf.Version(), nil)
d.problems = nil
}
}
// askConflict 在缓冲区有改动时询问如何处理磁盘上的新内容。
func (a *App) askConflict(d *Doc, text string) {
path := d.path
go func() {
res, err := mygo.Dialog.Message(mygo.MessageOptions{
Parent: a.win,
Type: mygo.MessageWarning,
Title: "文件已在编辑器外更改",
Message: filepath.Base(path) + " 在编辑器外被修改,而你有未保存的改动。",
Detail: "重新加载会丢弃你在编辑器里的修改。",
Buttons: []string{"重新加载", "保留我的改动"},
DefaultButton: 1,
CancelButton: 1,
})
if err != nil {
return
}
if res.Button != 0 {
return
}
a.win.Update(func() {
if a.find(path) != d {
return
}
a.reloadDoc(d, text)
if d.watch != nil {
d.watch.stamp(path)
}
})
}()
}
// notifySaved 在写盘后刷新已知的磁盘状态,避免把自己的保存当成外部改动。
func (d *Doc) notifySaved() {
if d.watch != nil {
d.watch.stamp(d.path)
}
}