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 项规划与完成索引。
727 lines
20 KiB
Go
727 lines
20 KiB
Go
package app
|
||
|
||
import (
|
||
"fmt"
|
||
"os"
|
||
"path/filepath"
|
||
"runtime"
|
||
"strings"
|
||
"sync"
|
||
"sync/atomic"
|
||
"time"
|
||
"unicode/utf8"
|
||
|
||
"github.com/egoist/mygo/ui"
|
||
"github.com/egoist/mygo/ui/editor"
|
||
)
|
||
|
||
// itemKind 区分候选类型:文件(快速打开)与命中行(全局搜索)。
|
||
type itemKind int
|
||
|
||
const (
|
||
itemFile itemKind = iota
|
||
itemLine
|
||
)
|
||
|
||
// paletteItem 是命令面板里的一行。
|
||
type paletteItem struct {
|
||
Kind itemKind
|
||
Path string
|
||
Line int // 1 起,仅 itemLine 使用
|
||
Label string
|
||
Hint string
|
||
Text string
|
||
// Idx 是它在调用方数组里的下标(代码动作用)。
|
||
Idx int
|
||
}
|
||
|
||
// paletteState 是命令面板(快速打开 / 全局搜索)的状态。
|
||
type paletteState struct {
|
||
open bool
|
||
search bool
|
||
// refs 是「查找引用」模式:候选已经算好,输入只做本地过滤。
|
||
refs bool
|
||
refsAll []paletteItem
|
||
// action 是「代码动作」模式:候选固定,回车执行 a.actions[idx]。
|
||
action bool
|
||
// recent 是「最近项目」模式:回车切换工作区。
|
||
recent bool
|
||
// symbol 是「转到符号」模式:查询打给 gopls 的 workspace/symbol。
|
||
symbol bool
|
||
// 全局搜索的匹配选项。
|
||
useRegex bool
|
||
matchCase bool
|
||
wholeWord bool
|
||
query string
|
||
lastQ string
|
||
sel int
|
||
items []paletteItem
|
||
gen int
|
||
waiting bool
|
||
// searchHint 是这轮全局搜索跳过了哪些文件,底部一行小字提示。
|
||
searchHint string
|
||
|
||
// 全局搜索的替换:replaceTo 是「替换为」,replacing 表示正在显示
|
||
// 预览(此时候选列表被替换成文件清单 + 确认按钮),planning 表示
|
||
// 正在后台算预览。plan 由 buildReplacePlan 填。
|
||
replaceTo string
|
||
replacing bool
|
||
planning bool
|
||
plan replacePlan
|
||
}
|
||
|
||
// paletteLimit 是结果条数上限。
|
||
const paletteLimit = 200
|
||
|
||
// OpenQuick 打开「快速打开」面板。
|
||
func (a *App) OpenQuick() {
|
||
if a.root == "" {
|
||
return
|
||
}
|
||
a.pal = paletteState{open: true}
|
||
if files, ready := a.indexSnapshot(); !ready || len(files) == 0 {
|
||
a.resetIndex(a.root)
|
||
a.buildIndex(a.root)
|
||
}
|
||
a.pal.items = a.quickResults("", 60)
|
||
a.win.Update(func() {})
|
||
}
|
||
|
||
// OpenSearch 打开「全局搜索」面板。
|
||
func (a *App) OpenSearch() {
|
||
if a.root == "" {
|
||
return
|
||
}
|
||
a.pal = paletteState{open: true, search: true}
|
||
a.win.Update(func() {})
|
||
}
|
||
|
||
// refreshPalette 在查询变化后重算候选。
|
||
func (a *App) refreshPalette() {
|
||
a.pal.lastQ = a.pal.query
|
||
a.pal.sel = 0
|
||
// 查询或匹配选项一变,替换预览就过期了:退回搜索态。
|
||
a.pal.replacing = false
|
||
a.pal.plan = replacePlan{}
|
||
a.pal.planning = false
|
||
if a.pal.recent {
|
||
q := strings.ToLower(strings.TrimSpace(a.pal.query))
|
||
if q == "" {
|
||
a.pal.items = a.pal.refsAll
|
||
return
|
||
}
|
||
var out []paletteItem
|
||
for _, it := range a.pal.refsAll {
|
||
if strings.Contains(strings.ToLower(it.Label), q) || strings.Contains(strings.ToLower(it.Hint), q) {
|
||
out = append(out, it)
|
||
}
|
||
}
|
||
a.pal.items = out
|
||
return
|
||
}
|
||
if a.pal.symbol {
|
||
// 结果由 gopls 给,这里只发起查询;回来时自己填 items。
|
||
a.scheduleSymbols(symbolHint(a.pal.query))
|
||
return
|
||
}
|
||
if a.pal.action {
|
||
// 动作列表通常只有几条,按标题前缀过滤就够。
|
||
q := strings.ToLower(strings.TrimSpace(a.pal.query))
|
||
if q == "" {
|
||
a.pal.items = a.pal.refsAll
|
||
return
|
||
}
|
||
var out []paletteItem
|
||
for _, it := range a.pal.refsAll {
|
||
if strings.Contains(strings.ToLower(it.Label), q) {
|
||
out = append(out, it)
|
||
}
|
||
}
|
||
a.pal.items = out
|
||
return
|
||
}
|
||
if a.pal.refs {
|
||
q := strings.ToLower(strings.TrimSpace(a.pal.query))
|
||
if q == "" {
|
||
a.pal.items = a.pal.refsAll
|
||
return
|
||
}
|
||
var out []paletteItem
|
||
for _, it := range a.pal.refsAll {
|
||
if strings.Contains(strings.ToLower(it.Label), q) || strings.Contains(strings.ToLower(it.Text), q) {
|
||
out = append(out, it)
|
||
}
|
||
}
|
||
a.pal.items = out
|
||
return
|
||
}
|
||
if !a.pal.search {
|
||
a.pal.items = a.quickResults(a.pal.query, 60)
|
||
return
|
||
}
|
||
if strings.TrimSpace(a.pal.query) == "" {
|
||
a.pal.items = nil
|
||
a.pal.waiting = false
|
||
a.pal.searchHint = ""
|
||
return
|
||
}
|
||
a.pal.gen++
|
||
gen := a.pal.gen
|
||
a.pal.waiting = true
|
||
a.pal.searchHint = "" // 新查询开始,别挂着上一轮的跳过提示
|
||
q := a.pal.query
|
||
opt := searchOpts{useRegex: a.pal.useRegex, matchCase: a.pal.matchCase, wholeWord: a.pal.wholeWord}
|
||
go func() {
|
||
// 轻微去抖:连续输入时只跑最后一次。
|
||
time.Sleep(220 * time.Millisecond)
|
||
items, stats := a.searchInWorkspace(q, paletteLimit, opt)
|
||
a.win.Update(func() {
|
||
if !a.pal.open || gen != a.pal.gen {
|
||
return
|
||
}
|
||
a.pal.items = items
|
||
a.pal.searchHint = stats.hint()
|
||
a.pal.waiting = false
|
||
})
|
||
}()
|
||
}
|
||
|
||
// searchOpts 是全局搜索的匹配选项。
|
||
type searchOpts struct {
|
||
useRegex bool
|
||
matchCase bool
|
||
wholeWord bool
|
||
}
|
||
|
||
// buildReplaceRe 按选项装配正则;buildMatcher 与替换共用(见 replace.go)。
|
||
|
||
// buildMatcher 按选项造一个行匹配函数。
|
||
//
|
||
// 正则和整词都走 regexp:整词就是在转义后的模式外面套 \b,
|
||
// 比手写边界判断可靠(中文没有词边界,套 \b 会退化成普通包含,正好符合预期)。
|
||
func buildMatcher(query string, o searchOpts) (func(string) bool, error) {
|
||
re, err := buildReplaceRe(query, o)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return re.MatchString, nil
|
||
}
|
||
|
||
// workspaceFiles 返回工作区文件清单(仓库相对路径,正斜杠)。
|
||
// 索引没建好就现场走一遍,避免搜索/替换面板空着。
|
||
func (a *App) workspaceFiles() []string {
|
||
root := a.root
|
||
if root == "" {
|
||
return nil
|
||
}
|
||
if files, ready := a.indexSnapshot(); ready {
|
||
return files
|
||
}
|
||
rules := loadIgnoreRules(root)
|
||
var files []string
|
||
_ = filepath.WalkDir(root, func(p string, d os.DirEntry, err error) error {
|
||
if err != nil {
|
||
return nil
|
||
}
|
||
rel, relErr := filepath.Rel(root, p)
|
||
relSlash := ""
|
||
if relErr == nil {
|
||
relSlash = filepath.ToSlash(rel)
|
||
}
|
||
if d.IsDir() {
|
||
if p != root && (isBuiltinSkipDir(d.Name()) || rules.match(relSlash, true)) {
|
||
return filepath.SkipDir
|
||
}
|
||
return nil
|
||
}
|
||
if skipFile(d.Name()) || rules.match(relSlash, false) {
|
||
return nil
|
||
}
|
||
if relErr == nil {
|
||
files = append(files, relSlash)
|
||
}
|
||
if len(files) > indexFileCap {
|
||
return filepath.SkipAll
|
||
}
|
||
return nil
|
||
})
|
||
return files
|
||
}
|
||
|
||
// searchStats 是这一轮搜索里被静默跳过的文件数。
|
||
// 以前超过 512KB 或读不出来的文件直接 continue,用户根本不知道自己
|
||
// 漏搜了东西;现在交给调用方显示成面板里的一行提示。
|
||
type searchStats struct {
|
||
tooLarge int
|
||
unreadable int
|
||
}
|
||
|
||
func (s searchStats) hint() string {
|
||
var parts []string
|
||
if s.tooLarge > 0 {
|
||
parts = append(parts, fmt.Sprintf("%d 个文件超过 512 KB", s.tooLarge))
|
||
}
|
||
if s.unreadable > 0 {
|
||
parts = append(parts, fmt.Sprintf("%d 个文件读不出或非 UTF-8", s.unreadable))
|
||
}
|
||
if len(parts) == 0 {
|
||
return ""
|
||
}
|
||
return "全局搜索已跳过:" + strings.Join(parts, "、")
|
||
}
|
||
|
||
// searchInWorkspace 在工作区里按行搜索,返回命中的行与跳过统计。
|
||
//
|
||
// 读盘是 I/O 密集,按 runtime.NumCPU() 并发(上限 8);每个 worker 只写
|
||
// 自己那份结果槽,最后按文件顺序合并,所以结果顺序和串行版完全一致。
|
||
// 命中上限后停止派发新文件,避免为了 200 条结果把上万个文件全读一遍。
|
||
func (a *App) searchInWorkspace(query string, limit int, o searchOpts) ([]paletteItem, searchStats) {
|
||
root := a.root
|
||
if root == "" || query == "" {
|
||
return nil, searchStats{}
|
||
}
|
||
files := a.workspaceFiles()
|
||
|
||
match, err := buildMatcher(query, o)
|
||
if err != nil {
|
||
return nil, searchStats{}
|
||
}
|
||
if len(files) == 0 {
|
||
return nil, searchStats{}
|
||
}
|
||
|
||
type fileHits struct {
|
||
items []paletteItem
|
||
tooLarge, unreadable int
|
||
}
|
||
per := make([]fileHits, len(files))
|
||
workers := runtime.NumCPU()
|
||
if workers > 8 {
|
||
workers = 8
|
||
}
|
||
if workers > len(files) {
|
||
workers = len(files)
|
||
}
|
||
|
||
var total atomic.Int64
|
||
var stop atomic.Bool
|
||
jobs := make(chan int, workers)
|
||
var wg sync.WaitGroup
|
||
for w := 0; w < workers; w++ {
|
||
wg.Add(1)
|
||
go func() {
|
||
defer wg.Done()
|
||
for i := range jobs {
|
||
if stop.Load() {
|
||
continue
|
||
}
|
||
rel := files[i]
|
||
abs := filepath.Join(root, filepath.FromSlash(rel))
|
||
info, err := os.Stat(abs)
|
||
if err != nil || info.Size() > 512*1024 {
|
||
per[i].tooLarge = 1
|
||
continue
|
||
}
|
||
data, err := os.ReadFile(abs)
|
||
if err != nil || !utf8.Valid(data) {
|
||
per[i].unreadable = 1
|
||
continue
|
||
}
|
||
var hits []paletteItem
|
||
for ln, line := range strings.Split(string(data), "\n") {
|
||
if !match(line) {
|
||
continue
|
||
}
|
||
// 按字符截断,别切坏中文(见 strutil.go 的注释)。
|
||
hits = append(hits, paletteItem{
|
||
Kind: itemLine,
|
||
Path: abs,
|
||
Line: ln + 1,
|
||
Label: rel,
|
||
Text: trimTo(strings.TrimSpace(line), 160),
|
||
})
|
||
}
|
||
per[i].items = hits
|
||
if len(hits) > 0 && total.Add(int64(len(hits))) >= int64(limit) {
|
||
stop.Store(true)
|
||
}
|
||
}
|
||
}()
|
||
}
|
||
for i := range files {
|
||
if stop.Load() {
|
||
break
|
||
}
|
||
jobs <- i
|
||
}
|
||
close(jobs)
|
||
wg.Wait()
|
||
|
||
var out []paletteItem
|
||
var stats searchStats
|
||
for i := range per {
|
||
stats.tooLarge += per[i].tooLarge
|
||
stats.unreadable += per[i].unreadable
|
||
for _, it := range per[i].items {
|
||
out = append(out, it)
|
||
if len(out) >= limit {
|
||
return out, stats
|
||
}
|
||
}
|
||
}
|
||
return out, stats
|
||
}
|
||
|
||
// moveSel 上下移动选中项。
|
||
func (a *App) moveSel(d int) {
|
||
if len(a.pal.items) == 0 {
|
||
return
|
||
}
|
||
a.pal.sel += d
|
||
if a.pal.sel < 0 {
|
||
a.pal.sel = len(a.pal.items) - 1
|
||
}
|
||
if a.pal.sel >= len(a.pal.items) {
|
||
a.pal.sel = 0
|
||
}
|
||
a.win.Update(func() {})
|
||
}
|
||
|
||
// activatePalette 打开当前选中的候选。
|
||
func (a *App) activatePalette() {
|
||
if a.pal.sel < 0 || a.pal.sel >= len(a.pal.items) {
|
||
return
|
||
}
|
||
it := a.pal.items[a.pal.sel]
|
||
a.pal.open = false
|
||
a.focusEditorSoon()
|
||
if a.pal.action {
|
||
a.applyAction(it.Idx)
|
||
return
|
||
}
|
||
if a.pal.recent {
|
||
// 最近项目:换工作区。
|
||
a.switchWorkspace(it.Path)
|
||
return
|
||
}
|
||
path, line := it.Path, it.Line
|
||
a.win.Update(func() {})
|
||
go a.openAtLine(path, line)
|
||
}
|
||
|
||
// openAt 打开文件并跳到指定行(line 为 1 起,0 表示只打开)。
|
||
func (a *App) openAtLine(path string, line int) {
|
||
d, err := a.loadDoc(path)
|
||
if err != nil {
|
||
a.alert("打开失败", err.Error())
|
||
return
|
||
}
|
||
if line <= 0 {
|
||
return
|
||
}
|
||
a.win.Update(func() {
|
||
if line-1 < d.buf.LineCount() {
|
||
d.st.GoTo(editor.Pos{Line: line - 1, Col: 0})
|
||
d.st.ScrollTo(line - 1)
|
||
}
|
||
a.active = d.path
|
||
a.syncTitle()
|
||
})
|
||
}
|
||
|
||
// paletteView 画命令面板。挂在根级(Modal 是覆盖层)。
|
||
func (a *App) paletteView(c *ui.Context) {
|
||
if !a.pal.open {
|
||
return
|
||
}
|
||
t := c.Theme()
|
||
|
||
ui.Modal(c, &a.pal.open, func() {
|
||
ui.Column(c).Width(620).Gap(6).Children(func() {
|
||
// 回车与上下键挂在输入框自己身上:焦点就在它这里,
|
||
// 元素级 OnShortcut 会先于「可点元素吃回车」的逻辑拿到按键。
|
||
// 回车用输入框自己的 Submitted:它在输入框内部就把 Enter 接住了,
|
||
// 外层的 Shortcut / OnShortcut 都收不到(Enter 会先变成输入框的一次点击)。
|
||
inp := ui.TextInput(c, &a.pal.query).FillWidth().FontSize(14).AutoFocus()
|
||
// 搜索模式下才显示匹配选项:快速打开用不上正则。
|
||
if a.pal.search {
|
||
ui.Row(c).FillWidth().Gap(6).Padding(0, 2).Children(func() {
|
||
a.searchToggle(c, "Aa", "区分大小写", &a.pal.matchCase)
|
||
a.searchToggle(c, ".*", "正则", &a.pal.useRegex)
|
||
a.searchToggle(c, "ab", "整词", &a.pal.wholeWord)
|
||
})
|
||
}
|
||
// 替换入口也在搜索模式下;预览时整块内容换成确认视图。
|
||
if a.pal.search && !a.pal.replacing {
|
||
a.replaceRow(c)
|
||
}
|
||
// 先把候选刷成当前查询的结果,再处理回车:否则这一帧回车会作用在
|
||
// 上一帧的候选上(输入 greet 却打开了 go.mod)。
|
||
if a.pal.query != a.pal.lastQ {
|
||
a.refreshPalette()
|
||
}
|
||
// 上下键用 Ctrl+N / Ctrl+P:方向键在输入框里是移动光标,抢不过来。
|
||
// 先处理移动再处理回车:同一帧里两个键一起来时,
|
||
// 回车应该作用在移动之后的选中项上。
|
||
if c.Shortcut(ui.Ctrl, ui.KeyN) {
|
||
a.moveSel(1)
|
||
}
|
||
if c.Shortcut(ui.Ctrl, ui.KeyP) {
|
||
a.moveSel(-1)
|
||
}
|
||
if inp.Submitted() {
|
||
a.activatePalette()
|
||
}
|
||
|
||
if a.pal.replacing {
|
||
a.replacePreview(c)
|
||
} else if len(a.pal.items) == 0 {
|
||
msg := "输入关键字开始搜索"
|
||
if a.pal.symbol {
|
||
msg = "输入关键字搜索符号"
|
||
if a.pal.waiting {
|
||
msg = "搜索中…"
|
||
}
|
||
}
|
||
if a.pal.refs {
|
||
msg = "没有找到引用"
|
||
}
|
||
if a.pal.search {
|
||
if a.pal.waiting {
|
||
msg = "搜索中…"
|
||
} else if strings.TrimSpace(a.pal.query) != "" {
|
||
msg = "没有匹配"
|
||
}
|
||
} else if a.pal.query != "" {
|
||
msg = "没有匹配的文件"
|
||
}
|
||
ui.Text(c, msg).FontSize(12).TextColor(t.TextMuted).Padding(6, 2)
|
||
} else {
|
||
ui.Column(c).Gap(1).Children(func() {
|
||
for i, it := range a.pal.items {
|
||
a.paletteRow(c, i, it)
|
||
}
|
||
})
|
||
}
|
||
|
||
ui.Row(c).Gap(10).Padding(4, 2).Children(func() {
|
||
// 方向键在输入框里是移动光标,抢不过来,所以用 Ctrl+N / Ctrl+P。
|
||
ui.Text(c, "Ctrl+N/P 选择").FontSize(10).TextColor(t.TextMuted)
|
||
ui.Text(c, "Enter 打开").FontSize(10).TextColor(t.TextMuted)
|
||
ui.Text(c, "Esc 关闭").FontSize(10).TextColor(t.TextMuted)
|
||
ui.Box(c).Grow(1)
|
||
// 索引被上限截断时说一声:否则「搜不到」会被当成「不存在」。
|
||
if !a.pal.search && a.indexTruncated() {
|
||
ui.Textf(c, "索引已截断至 %d 个文件", indexFileCap).
|
||
FontSize(10).TextColor(t.Warning)
|
||
}
|
||
// 被静默跳过的文件要让用户知道,否则「搜不到」会被当成「不存在」。
|
||
if a.pal.search && a.pal.searchHint != "" {
|
||
ui.Text(c, a.pal.searchHint).FontSize(10).TextColor(t.Warning)
|
||
}
|
||
if n := len(a.pal.items); n > 0 {
|
||
ui.Textf(c, "%d 项", n).FontSize(10).TextColor(t.TextMuted)
|
||
}
|
||
})
|
||
})
|
||
})
|
||
}
|
||
|
||
// replaceRow 是搜索面板里的「替换为 ___ [预览替换]」一行。
|
||
func (a *App) replaceRow(c *ui.Context) {
|
||
t := c.Theme()
|
||
ui.Row(c).FillWidth().Gap(6).Padding(0, 2).AlignItems(ui.Center).Children(func() {
|
||
ui.Text(c, "替换为").FontSize(11).TextColor(t.TextMuted)
|
||
ui.TextInput(c, &a.pal.replaceTo).FillWidth().FontSize(12)
|
||
label := "预览替换"
|
||
if a.pal.planning {
|
||
label = "计算中…"
|
||
}
|
||
if ui.Button(c, label).Clicked() && !a.pal.planning {
|
||
a.startReplacePreview()
|
||
}
|
||
})
|
||
}
|
||
|
||
// searchOpts 收集面板当前的匹配选项。
|
||
func (a *App) searchOpts() searchOpts {
|
||
return searchOpts{useRegex: a.pal.useRegex, matchCase: a.pal.matchCase, wholeWord: a.pal.wholeWord}
|
||
}
|
||
|
||
// startReplacePreview 算替换预览:主线程收缓冲区快照 → 后台扫盘 → 回来切预览。
|
||
func (a *App) startReplacePreview() {
|
||
if strings.TrimSpace(a.pal.query) == "" || a.pal.planning {
|
||
return
|
||
}
|
||
if _, err := buildReplaceRe(a.pal.query, a.searchOpts()); err != nil {
|
||
a.alert("替换", "匹配规则有误: "+err.Error())
|
||
return
|
||
}
|
||
a.pal.planning = true
|
||
a.refresh()
|
||
// 打开文件的缓冲区文本只能在主线程读,先在这里收快照;
|
||
// 替换时以确认那一刻的缓冲区为准,这里只是预览。
|
||
openTexts := map[string]string{}
|
||
for _, d := range a.docs {
|
||
openTexts[d.path] = d.buf.String()
|
||
}
|
||
query, to, opt := a.pal.query, a.pal.replaceTo, a.searchOpts()
|
||
go func() {
|
||
plan, err := a.buildReplacePlan(openTexts, query, to, opt)
|
||
a.win.Update(func() {
|
||
a.pal.planning = false
|
||
if err != nil {
|
||
a.alert("替换", "匹配规则有误: "+err.Error())
|
||
return
|
||
}
|
||
if plan.Total == 0 {
|
||
a.alert("替换", "没有可替换的匹配")
|
||
return
|
||
}
|
||
a.pal.plan = plan
|
||
a.pal.replacing = true
|
||
})
|
||
}()
|
||
}
|
||
|
||
// replacePreview 画预览:文件清单 + 处数 + 确认/取消。
|
||
func (a *App) replacePreview(c *ui.Context) {
|
||
t := c.Theme()
|
||
p := a.pal.plan
|
||
ui.Textf(c, "将替换 %d 处 / %d 个文件", p.Total, len(p.Files)).
|
||
FontSize(12).TextColor(t.Text)
|
||
ui.Text(c, "打开中的文件按缓冲区改(保持未保存),其余按原编码直接写盘。").
|
||
FontSize(10).TextColor(t.TextMuted).SingleLine()
|
||
ui.Scroll(c).FillWidth().Height(240).Children(func() {
|
||
ui.Column(c).Gap(1).Children(func() {
|
||
for _, f := range p.Files {
|
||
ui.Textf(c, "%d 处 %s", f.Count, f.Rel).
|
||
FontSize(11).TextColor(t.Text).SingleLine().Padding(1, 6)
|
||
}
|
||
if p.Truncated {
|
||
ui.Textf(c, "…文件数超过 %d,未列出的也会一起替换", replacePlanCap).
|
||
FontSize(10).TextColor(t.Warning).Padding(1, 6)
|
||
}
|
||
})
|
||
})
|
||
ui.Row(c).Gap(8).Justify(ui.End).Padding(2, 0).Children(func() {
|
||
if ui.Button(c, "取消").Clicked() {
|
||
a.pal.replacing = false
|
||
a.pal.plan = replacePlan{}
|
||
a.refresh()
|
||
}
|
||
if ui.Button(c, "确认替换").Clicked() {
|
||
a.confirmReplace()
|
||
}
|
||
})
|
||
}
|
||
|
||
// confirmReplace 执行替换并汇报到输出面板。
|
||
func (a *App) confirmReplace() {
|
||
if !a.pal.replacing || a.pal.plan.Total == 0 {
|
||
return
|
||
}
|
||
query, to, opt := a.pal.query, a.pal.replaceTo, a.searchOpts()
|
||
files := a.pal.plan.Files
|
||
a.pal.open = false
|
||
a.pal.replacing = false
|
||
a.pal.plan = replacePlan{}
|
||
a.focusEditorSoon()
|
||
|
||
hits, nfiles, failed := a.applyReplacePlan(query, to, opt, files)
|
||
kind := OutSuccess
|
||
text := fmt.Sprintf("已替换 %d 处(%d 个文件):%s → %s", hits, nfiles, query, to)
|
||
if len(failed) > 0 {
|
||
kind = OutWarn
|
||
text += fmt.Sprintf(",%d 个文件失败", len(failed))
|
||
}
|
||
a.out = append(a.out, OutLine{Text: text, Kind: kind})
|
||
for _, f := range failed {
|
||
a.out = append(a.out, OutLine{Text: " " + f, Kind: OutError})
|
||
}
|
||
a.panel = true
|
||
a.panelTab = 0
|
||
a.refreshGit()
|
||
a.refresh()
|
||
}
|
||
|
||
// paletteRow 画一行候选。
|
||
func (a *App) paletteRow(c *ui.Context, i int, it paletteItem) {
|
||
t := c.Theme()
|
||
selected := i == a.pal.sel
|
||
|
||
row := ui.ButtonBase(c).FillWidth().Padding(3, 6).Gap(8).Radius(3).
|
||
Justify(ui.Start).AlignItems(ui.Center).Cursor(ui.CursorPointer)
|
||
bg := ui.RGBA(0, 0, 0, 0)
|
||
switch {
|
||
case selected:
|
||
bg = t.Accent.Alpha(0.24)
|
||
case row.Hovered():
|
||
bg = t.SurfaceHover
|
||
}
|
||
row = row.Background(bg)
|
||
|
||
row.Children(func() {
|
||
if it.Kind == itemLine {
|
||
ui.Textf(c, "%d", it.Line).FontSize(11).TextColor(t.TextMuted).Width(38)
|
||
ui.Column(c).Grow(1).Gap(1).Children(func() {
|
||
ui.Text(c, it.Label).FontSize(11).TextColor(t.TextMuted).SingleLine()
|
||
ui.Text(c, it.Text).FontSize(12).TextColor(t.Text).SingleLine()
|
||
})
|
||
} else {
|
||
ui.Column(c).Grow(1).Gap(1).Children(func() {
|
||
ui.Text(c, it.Label).FontSize(12).TextColor(t.Text).SingleLine()
|
||
if it.Hint != "" && it.Hint != "." {
|
||
ui.Text(c, it.Hint).FontSize(10).TextColor(t.TextMuted).SingleLine()
|
||
}
|
||
})
|
||
}
|
||
})
|
||
|
||
if row.Clicked() {
|
||
a.pal.sel = i
|
||
a.activatePalette()
|
||
}
|
||
}
|
||
|
||
// searchToggle 是搜索面板里的一个开关小标签。
|
||
//
|
||
// 不用 Checkbox:面板里要的是紧凑的一行,勾选框太大。
|
||
func (a *App) searchToggle(c *ui.Context, label, tip string, on *bool) {
|
||
t := c.Theme()
|
||
bg := ui.RGBA(0, 0, 0, 0)
|
||
fg := t.TextMuted
|
||
bd := t.Border
|
||
if *on {
|
||
bg = t.SurfaceHover
|
||
fg = t.Text
|
||
bd = t.Accent
|
||
}
|
||
b := ui.ButtonBase(c).Padding(1, 6).Radius(4).Background(bg).
|
||
BorderWidth(1, 1, 1, 1).BorderColor(bd).Cursor(ui.CursorPointer)
|
||
b.Children(func() {
|
||
ui.Text(c, label).FontSize(10).TextColor(fg)
|
||
})
|
||
if b.Clicked() {
|
||
*on = !*on
|
||
// 选项变了要按新规则重搜一次。
|
||
a.refreshPalette()
|
||
}
|
||
_ = tip
|
||
}
|
||
|
||
// asciiWordEnds 判断首尾是否都是 ASCII 词字符(字母/数字/下划线)。
|
||
func asciiWordEnds(s string) bool {
|
||
r := []rune(s)
|
||
if len(r) == 0 {
|
||
return false
|
||
}
|
||
isWord := func(c rune) bool {
|
||
return c < 128 && (c == '_' ||
|
||
(c >= '0' && c <= '9') ||
|
||
(c >= 'a' && c <= 'z') ||
|
||
(c >= 'A' && c <= 'Z'))
|
||
}
|
||
return isWord(r[0]) && isWord(r[len(r)-1])
|
||
}
|