T1 移动包同步(internal/app/movepkg.go + renameentry.go)
gopls 不声明 willRename,改用 go/parser 精确定位 ImportSpec.Path,
目录移动改全仓 import 路径、单文件跨目录移动改 package 声明。
写盘失败整体恢复并回滚 rename;已打开且无改动的缓冲区用 Reset 同步。
集成测试在临时 Go 模块里移目录后直接跑 go build ./... 通过。
T2 输出/问题面板右键(panel_menu.go + output.go + popmenu.go)
新增 openPopAt 让菜单贴指针弹出;复制选中行/复制全部/清空输出。
真窗口截图确认位置正确、无裁切。
**范围缩窄**:终端从 mygo v0.7.4 起就自带原生 ContextMenu,原判断是误报。
T3 snippet 补全(completion_snippet.go + lsp/protocol.go)
snippetSupport 改 true;实测 gopls v0.23 返回 insertTextFormat=2、
textEdit.newText="Print(${1:})",接上 mygo v0.9.0 的 ParseSnippet。
解析失败降级成纯文本(支持嵌套占位/choices 剥离),结果按模板缓存。
端到端 Tester 验证接受补全后得到 fmt.Print() 且光标停在括号内。
T4 本地历史超限可见(history.go)
静默 return 改为返回 histSkipped,输出面板提示;每文件整个会话只提一次。
T5 全局搜索并发(palette.go)
NumCPU(上限 8)个 worker 并发读盘,结果按文件序合并保证顺序稳定;
命中上限停止派发;512KB 以上被跳过的文件数显示成面板底部提示。
T6 清理根目录残留;4 个 0 字节 .sock 被环境安全守卫拦住未删(已 gitignore)。
验证:gofmt 空、build/vet OK、go test -count=1 ./... 全绿(282 个测试)、
新增并发/剪贴板/回滚用例 go test -race 无数据竞争。
docs/MyGoland-待办与规划.md 已清空待办,只剩 6 项规划与完成索引。
207 lines
5.1 KiB
Go
207 lines
5.1 KiB
Go
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
|
||
}
|
||
// Go 包的同步要读旧路径上的文件,必须在 rename 之前算好。
|
||
edits := a.planGoMoveEdits(old, target)
|
||
|
||
var renameErr error
|
||
if strings.EqualFold(target, old) {
|
||
// 只改大小写:Windows 上需要先改名到临时名字再改回来。
|
||
tmp := old + ".renaming"
|
||
if err := os.Rename(old, tmp); err != nil {
|
||
renameErr = err
|
||
} else if err := os.Rename(tmp, target); err != nil {
|
||
renameErr = err
|
||
_ = os.Rename(tmp, old)
|
||
}
|
||
} else {
|
||
if _, err := os.Stat(target); err == nil {
|
||
a.alert("重命名失败", "目标已存在:"+filepath.Base(target))
|
||
return
|
||
}
|
||
renameErr = os.Rename(old, target)
|
||
}
|
||
if renameErr != nil {
|
||
a.alert("重命名失败", renameErr.Error())
|
||
return
|
||
}
|
||
|
||
if err := a.applyGoMoveEdits(edits); err != nil {
|
||
// 内容同步失败就把移动也退回去,别留下一个编译不过的仓库。
|
||
_ = os.Rename(target, old)
|
||
a.alert("移动失败", "已回滚文件移动;同步 import 时出错:"+err.Error())
|
||
return
|
||
}
|
||
|
||
a.afterRename(old, target) // 先把内存里的路径搬到新位置
|
||
a.syncGoMoveBuffers(edits)
|
||
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
|
||
}
|