Files
MyGolandRelease/internal/app/fileops.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

268 lines
6.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package app
import (
"fmt"
"os"
"path/filepath"
"sort"
"github.com/egoist/mygo"
)
// selected 判断路径是否被选中。
func (a *App) selected(path string) bool { return a.sel != nil && a.sel[path] }
// selectOnly 只选中这一项(普通点击)。
func (a *App) selectOnly(path string) {
a.sel = map[string]bool{path: true}
a.selAnchor = path
}
// toggleSelect 切换某一项的选中状态(Ctrl 点击)。
func (a *App) toggleSelect(path string) {
if a.sel == nil {
a.sel = map[string]bool{}
}
if a.sel[path] {
delete(a.sel, path)
} else {
a.sel[path] = true
}
a.selAnchor = path
}
// selectRange 选中从基准项到当前项之间的全部(Shift 点击)。
// 区间按「树里当前可见的顺序」算,折叠着的子树不参与。
func (a *App) selectRange(path string) {
all := a.visiblePaths()
from, to := -1, -1
for i, p := range all {
if samePath(p, a.selAnchor) {
from = i
}
if samePath(p, path) {
to = i
}
}
if from < 0 || to < 0 {
a.selectOnly(path)
return
}
if from > to {
from, to = to, from
}
a.sel = make(map[string]bool, to-from+1)
for _, p := range all[from : to+1] {
a.sel[p] = true
}
}
// visiblePaths 按当前展开状态深度优先列出树上可见的项。
func (a *App) visiblePaths() []string {
var out []string
var walk func(dir string)
walk = func(dir string) {
for _, e := range a.entries(dir) {
p := filepath.Join(dir, e.Name())
if e.IsDir() {
if skipDir(e.Name()) {
continue
}
out = append(out, p)
if *a.openFlag(p) {
walk(p)
}
continue
}
if skipFile(e.Name()) {
continue
}
out = append(out, p)
}
}
if a.root != "" {
walk(a.root)
}
return out
}
// targetsFor 返回一次操作要作用的路径:右键的那一行如果在选中集合里,
// 就作用于整个集合,否则只作用于它自己。
func (a *App) targetsFor(path string) []string {
if a.sel[path] && len(a.sel) > 0 {
out := make([]string, 0, len(a.sel))
for p := range a.sel {
out = append(out, p)
}
sort.Strings(out)
return out
}
return []string{path}
}
// treeMenu 是文件树上的一行的右键菜单。
func (a *App) treeEntries(path string, isDir bool) []menuEntry {
dir := path
if !isDir {
dir = filepath.Dir(path)
}
targets := a.targetsFor(path)
var out []menuEntry
out = append(out,
menuEntry{label: "新建文件", action: func() { a.beginNew(dir, false) }},
menuEntry{label: "新建文件夹", action: func() { a.beginNew(dir, true) }},
menuEntry{divider: true},
)
if !isDir && len(targets) == 1 {
out = append(out, menuEntry{label: "打开", action: func() { go a.OpenFile(path) }})
}
// 多选时不做重命名:一次改一个更安全。
if len(targets) == 1 {
out = append(out, menuEntry{label: "重命名…", action: func() { a.BeginRename(path, isDir) }})
}
// 历史入口放在这里:右键一个文件看「它改过什么」是最自然的时机。
if !isDir || len(targets) == 1 {
out = append(out, menuEntry{divider: true})
out = append(out, menuEntry{
label: "Git 历史…",
action: func() { a.OpenGitLog(path, isDir) },
})
out = append(out, menuEntry{
label: "本地历史…",
action: func() { a.OpenHistoryAt(path, isDir) },
})
}
out = append(out, menuEntry{
label: "在资源管理器中打开",
action: func() { mygo.Shell.ShowItemInFolder(path) },
})
out = append(out, menuEntry{divider: true})
del := "删除"
if len(targets) > 1 {
del = fmt.Sprintf("删除这 %d 项", len(targets))
}
out = append(out, menuEntry{label: del, action: func() { a.deletePaths(targets) }})
out = append(out, menuEntry{divider: true})
out = append(out, menuEntry{label: "刷新文件树", action: func() { a.Refresh() }})
return out
}
// deletePaths 删除文件或目录。删目录用 RemoveAll,会连内容一起删掉,
// 所以先明确问一次,并列出数量。
func (a *App) deletePaths(paths []string) {
if len(paths) == 0 || a.win == nil {
return
}
what := "项目"
if len(paths) == 1 {
what = filepath.Base(paths[0])
}
msg := fmt.Sprintf("确定删除 %s?\n此操作不可撤销。", what)
if len(paths) > 1 {
msg = fmt.Sprintf("确定删除选中的 %d 个项目?\n其中目录会连内容一起删除,此操作不可撤销。", len(paths))
}
res, err := mygo.Dialog.Message(mygo.MessageOptions{
Parent: a.win,
Type: mygo.MessageWarning,
Title: "删除",
Message: msg,
Buttons: []string{"删除", "取消"},
CancelButton: 1,
})
if err != nil || res.Button != 0 {
return
}
for _, p := range paths {
if err := os.RemoveAll(p); err != nil {
a.appendOut(err.Error())
a.panel = true
}
}
// 被删掉的文件如果开着,标签也要跟着关,否则编辑器指向一个不存在的路径。
a.win.Update(func() {
var closed []string
for _, d := range a.docs {
if underAny(d.path, paths) {
closed = append(closed, d.path)
}
}
for _, p := range closed {
a.removeDoc(p)
}
})
a.sel = nil
a.selAnchor = ""
a.Refresh()
a.win.Update(func() {})
}
// underAny 判断 path 是否等于或位于 paths 里某一项之下。
func underAny(path string, paths []string) bool {
clean := filepath.Clean(path)
for _, p := range paths {
base := filepath.Clean(p)
if samePath(clean, base) {
return true
}
if len(clean) > len(base) && samePath(clean[:len(base)], base) {
return true
}
}
return false
}
// addEntries 是侧栏「+」按钮的菜单:在当前选中目录下新建。
func (a *App) addEntries() []menuEntry {
dir := a.root
if len(a.sel) == 1 {
for p := range a.sel {
if st, err := os.Stat(p); err == nil {
if st.IsDir() {
dir = p
} else {
dir = filepath.Dir(p)
}
}
}
}
return []menuEntry{
{label: "新建文件", action: func() { a.beginNew(dir, false) }},
{label: "新建文件夹", action: func() { a.beginNew(dir, true) }},
}
}
// ExpandAll 展开文件树里所有目录,深度上限防止超大仓库卡死。
func (a *App) ExpandAll() {
if a.root == "" {
return
}
if a.win == nil {
a.expandDir(a.root, 0)
return
}
a.win.Update(func() { a.expandDir(a.root, 0) })
}
// expandDir 递归把目录标记为展开。
func (a *App) expandDir(dir string, depth int) {
if depth > 10 {
return
}
for _, e := range a.entries(dir) {
if !e.IsDir() || skipDir(e.Name()) {
continue
}
p := filepath.Join(dir, e.Name())
*a.openFlag(p) = true
a.expandDir(p, depth+1)
}
}