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,190 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/egoist/mygo/ui"
|
||||
|
||||
"mygoland/internal/lsp"
|
||||
)
|
||||
|
||||
// renameEntryState 是「重命名」弹窗的状态。
|
||||
type renameEntryState struct {
|
||||
open bool
|
||||
dlg bool
|
||||
path string
|
||||
isDir bool
|
||||
name string
|
||||
}
|
||||
|
||||
// BeginRename 打开重命名弹窗,输入框预填当前名字。
|
||||
//
|
||||
// 输入里可以带目录(如 sub/util.go),这样顺带能搬文件——
|
||||
// 不单独做「移动」,一个输入框覆盖两种操作。
|
||||
func (a *App) BeginRename(path string, isDir bool) {
|
||||
if path == "" {
|
||||
return
|
||||
}
|
||||
a.renEntry = renameEntryState{
|
||||
open: true,
|
||||
dlg: true,
|
||||
path: path,
|
||||
isDir: isDir,
|
||||
name: filepath.Base(path),
|
||||
}
|
||||
a.win.Update(func() {})
|
||||
}
|
||||
|
||||
// renameEntryDialog 画重命名弹窗。
|
||||
func (a *App) renameEntryDialog(c *ui.Context) {
|
||||
if !a.renEntry.open {
|
||||
return
|
||||
}
|
||||
t := c.Theme()
|
||||
|
||||
ui.Modal(c, &a.renEntry.dlg, func() {
|
||||
ui.Column(c).Width(440).Gap(10).Children(func() {
|
||||
title := "重命名文件"
|
||||
if a.renEntry.isDir {
|
||||
title = "重命名文件夹"
|
||||
}
|
||||
ui.Text(c, title).FontSize(13).Bold().TextColor(t.Text)
|
||||
ui.Text(c, filepath.Dir(a.renEntry.path)).FontSize(10).TextColor(t.TextMuted).SingleLine()
|
||||
|
||||
inp := ui.TextInput(c, &a.renEntry.name).FillWidth().FontSize(13).AutoFocus()
|
||||
if inp.Submitted() {
|
||||
a.confirmRenameEntry()
|
||||
}
|
||||
ui.Text(c, "可以带目录,如 sub/name.go,等于同时移动").FontSize(10).TextColor(t.TextMuted).SingleLine()
|
||||
|
||||
ui.Row(c).Gap(8).Justify(ui.End).Children(func() {
|
||||
if ui.Button(c, "取消").Clicked() {
|
||||
a.renEntry.open = false
|
||||
}
|
||||
if ui.Button(c, "确定").Clicked() {
|
||||
a.confirmRenameEntry()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// 遮罩/ Esc 只改副本,弹窗照常重建;只有按钮能真正关掉。
|
||||
if !a.renEntry.dlg && a.renEntry.open {
|
||||
a.renEntry.dlg = true
|
||||
a.win.Update(func() {})
|
||||
}
|
||||
if !a.renEntry.open && a.renEntry.dlg {
|
||||
a.renEntry.dlg = false
|
||||
a.focusEditorSoon()
|
||||
}
|
||||
}
|
||||
|
||||
// confirmRenameEntry 落盘改名,并同步已打开的文件。
|
||||
func (a *App) confirmRenameEntry() {
|
||||
old := a.renEntry.path
|
||||
name := strings.TrimSpace(a.renEntry.name)
|
||||
if name == "" {
|
||||
a.renEntry.open = false
|
||||
return
|
||||
}
|
||||
target := filepath.FromSlash(name)
|
||||
if !filepath.IsAbs(target) {
|
||||
target = filepath.Join(filepath.Dir(old), target)
|
||||
}
|
||||
target = filepath.Clean(target)
|
||||
|
||||
// 关掉弹窗再干活,避免错误提示被弹窗盖住。
|
||||
a.renEntry.open = false
|
||||
if target == old {
|
||||
a.win.Update(func() {})
|
||||
return
|
||||
}
|
||||
if strings.EqualFold(target, old) {
|
||||
// 只改大小写:Windows 上需要先改名到临时名字再改回来。
|
||||
tmp := old + ".renaming"
|
||||
if err := os.Rename(old, tmp); err == nil {
|
||||
_ = os.Rename(tmp, target)
|
||||
}
|
||||
} else {
|
||||
if _, err := os.Stat(target); err == nil {
|
||||
a.alert("重命名失败", "目标已存在:"+filepath.Base(target))
|
||||
return
|
||||
}
|
||||
if err := os.Rename(old, target); err != nil {
|
||||
a.alert("重命名失败", err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
a.afterRename(old, target)
|
||||
a.win.Update(func() {})
|
||||
}
|
||||
|
||||
// afterRename 把内存里跟旧路径有关的东西都搬到新路径。
|
||||
func (a *App) afterRename(old, target string) {
|
||||
remap := func(p string) (string, bool) { return remapPath(p, old, target) }
|
||||
|
||||
for _, d := range a.docs {
|
||||
np, ok := remap(d.path)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
// 先和语言服务器断开旧 URI,再按新路径挂回去。
|
||||
d.stopWatch()
|
||||
if a.lsp != nil && d.opened {
|
||||
_ = a.lsp.DidClose(d.uri)
|
||||
d.opened = false
|
||||
}
|
||||
d.path = np
|
||||
d.uri = lsp.PathToURI(np)
|
||||
a.lspOpen(d)
|
||||
a.startWatch(d)
|
||||
if a.active == old || strings.HasPrefix(a.active, old+string(filepath.Separator)) {
|
||||
a.active = np
|
||||
}
|
||||
}
|
||||
|
||||
// 展开状态与目录缓存都按路径存的,改名后这些键就失效了。
|
||||
next := map[string]*bool{}
|
||||
for k, v := range a.open {
|
||||
if nk, ok := remap(k); ok {
|
||||
next[nk] = v
|
||||
} else {
|
||||
next[k] = v
|
||||
}
|
||||
}
|
||||
a.open = next
|
||||
a.dirs = map[string][]os.DirEntry{}
|
||||
if a.sel != nil {
|
||||
sel := map[string]bool{}
|
||||
for k := range a.sel {
|
||||
nk, _ := remap(k)
|
||||
sel[nk] = true
|
||||
}
|
||||
a.sel = sel
|
||||
}
|
||||
|
||||
a.syncTitle()
|
||||
a.saveSession()
|
||||
a.refreshGit()
|
||||
}
|
||||
|
||||
// remapPath 把落在 old 里的路径换到 target 下。
|
||||
//
|
||||
// 只按「目录分隔符」判断前缀:a.go.bak 不属于 a.go,
|
||||
// 只看字符串前缀会把这类同名前缀的邻居误伤。
|
||||
func remapPath(p, old, target string) (string, bool) {
|
||||
if p == "" || old == "" {
|
||||
return p, false
|
||||
}
|
||||
if p == old {
|
||||
return target, true
|
||||
}
|
||||
prefix := old + string(filepath.Separator)
|
||||
if strings.HasPrefix(p, prefix) {
|
||||
return filepath.Join(target, strings.TrimPrefix(p, prefix)), true
|
||||
}
|
||||
return p, false
|
||||
}
|
||||
Reference in new issue
Block a user