- 编辑器:多标签、语法高亮、gopls 全套(补全/诊断/跳转/重命名/代码动作/内联提示/签名大纲) - 调试:Delve DAP(断点/条件断点/变量树/表达式求值/goroutine 列表) - 测试:结果树、覆盖率行着色、失败跳源码、-run 精确运行、行号可跑标记 - Git:状态着色、选择性暂存提交、pull/push、历史并排对比 - 本地历史 + 自动快照 + 崩溃恢复、会话/光标恢复 - 全局搜索替换(预览+确认,GBK 安全)、多终端、gopls 崩溃自愈 - 设计文档与验证记录见 docs/plans/
161 lines
3.7 KiB
Go
161 lines
3.7 KiB
Go
package app
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"github.com/egoist/mygo"
|
||
)
|
||
|
||
// tabMenu 是文件标签页的右键菜单。
|
||
//
|
||
// 菜单项后面带上数量(关闭左边(3)这种),是因为「关闭全部」这类操作
|
||
// 一旦点错就不可撤销,先让人看清会影响几个文件。
|
||
func (a *App) tabEntries(path string) []menuEntry {
|
||
idx := a.docIndex(path)
|
||
if idx < 0 {
|
||
return nil
|
||
}
|
||
n := len(a.docs)
|
||
var out []menuEntry
|
||
|
||
out = append(out, menuEntry{label: "关闭", action: func() { go a.CloseDoc(path) }})
|
||
if idx > 0 {
|
||
out = append(out, menuEntry{
|
||
label: fmt.Sprintf("关闭左边(%d)", idx),
|
||
action: func() { a.closePaths(0, idx) },
|
||
})
|
||
}
|
||
if idx < n-1 {
|
||
out = append(out, menuEntry{
|
||
label: fmt.Sprintf("关闭右边(%d)", n-1-idx),
|
||
action: func() { a.closePaths(idx+1, n) },
|
||
})
|
||
}
|
||
out = append(out, menuEntry{divider: true})
|
||
if n > 1 {
|
||
out = append(out, menuEntry{
|
||
label: fmt.Sprintf("关闭其他(%d)", n-1),
|
||
action: func() { a.closeOthers(path) },
|
||
})
|
||
}
|
||
out = append(out, menuEntry{
|
||
label: fmt.Sprintf("关闭全部(%d)", n),
|
||
action: func() { a.closePaths(0, n) },
|
||
})
|
||
out = append(out, menuEntry{divider: true})
|
||
|
||
if saved := a.savedPaths(); len(saved) == 0 {
|
||
out = append(out, menuEntry{label: "关闭已保存的", disabled: true})
|
||
} else {
|
||
out = append(out, menuEntry{
|
||
label: fmt.Sprintf("关闭已保存的(%d)", len(saved)),
|
||
action: func() { a.closeSaved() },
|
||
})
|
||
}
|
||
return out
|
||
}
|
||
|
||
// docIndex 返回文件在标签栏里的位置,找不到为 -1。
|
||
func (a *App) docIndex(path string) int {
|
||
for i, d := range a.docs {
|
||
if d.path == path {
|
||
return i
|
||
}
|
||
}
|
||
return -1
|
||
}
|
||
|
||
// savedPaths 返回没有未保存改动的文件。
|
||
func (a *App) savedPaths() []string {
|
||
var out []string
|
||
for _, d := range a.docs {
|
||
if !d.buf.Modified() {
|
||
out = append(out, d.path)
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
// closeOthers 关掉除 path 以外的所有标签。
|
||
func (a *App) closeOthers(path string) {
|
||
var paths []string
|
||
for _, d := range a.docs {
|
||
if d.path != path {
|
||
paths = append(paths, d.path)
|
||
}
|
||
}
|
||
a.closePathsOf(paths)
|
||
}
|
||
|
||
// closeSaved 只关已保存的标签,天然不会丢改动,直接关。
|
||
func (a *App) closeSaved() {
|
||
a.closePathsOf(a.savedPaths())
|
||
}
|
||
|
||
// closePaths 关闭 [from, to) 区间的标签。
|
||
func (a *App) closePaths(from, to int) {
|
||
if from < 0 {
|
||
from = 0
|
||
}
|
||
if to > len(a.docs) {
|
||
to = len(a.docs)
|
||
}
|
||
if from >= to {
|
||
return
|
||
}
|
||
paths := make([]string, 0, to-from)
|
||
for _, d := range a.docs[from:to] {
|
||
paths = append(paths, d.path)
|
||
}
|
||
a.closePathsOf(paths)
|
||
}
|
||
|
||
// closePathsOf 是批量关闭的执行端。
|
||
//
|
||
// 未保存的文件只统一问一次,而不是逐个弹框——一次关五个标签弹五次确认
|
||
// 没人受得了。选择「全部保存」时用 writeDoc 同步落盘,不走 saveDoc 的
|
||
// 异步格式化,否则关窗口和写盘会撞在一起。
|
||
func (a *App) closePathsOf(paths []string) {
|
||
if len(paths) == 0 || a.win == nil {
|
||
return
|
||
}
|
||
|
||
var dirty []*Doc
|
||
for _, p := range paths {
|
||
if d := a.find(p); d != nil && d.buf.Modified() {
|
||
dirty = append(dirty, d)
|
||
}
|
||
}
|
||
|
||
if len(dirty) > 0 {
|
||
res, err := mygo.Dialog.Message(mygo.MessageOptions{
|
||
Parent: a.win,
|
||
Type: mygo.MessageWarning,
|
||
Title: "未保存的更改",
|
||
Message: fmt.Sprintf("要关闭的 %d 个文件里有 %d 个未保存。", len(paths), len(dirty)),
|
||
Buttons: []string{"全部保存", "不保存", "取消"},
|
||
DefaultButton: 0,
|
||
CancelButton: 2,
|
||
})
|
||
if err != nil {
|
||
return
|
||
}
|
||
switch res.Button {
|
||
case 0:
|
||
for _, d := range dirty {
|
||
a.writeDoc(d)
|
||
}
|
||
case 1:
|
||
// 丢弃改动。
|
||
default:
|
||
return
|
||
}
|
||
}
|
||
|
||
a.win.Update(func() {
|
||
for _, p := range paths {
|
||
a.removeDoc(p)
|
||
}
|
||
})
|
||
}
|