Files
MyGolandRelease/internal/app/grep.go
T
zerorigin 4ace9c9fb6 fix: 按评估优先级修四项(标签右键/UTF-8 截断/覆盖率输出/.gitignore)
1. 标签页右键菜单接回(回归修复)
   tabmenu.go 的 160 行实现全库零调用点,tabBar 只处理 Clicked。
   补 RightClicked → openPop(tabEntries(path)),右键时先设 active
   (菜单无标题,靠标签高亮传达「关闭左边」相对谁算)。
   新增 tabmenu_test.go 8 个单测。

2. 中文按字节截断导致乱码
   palette.go trimmed[:160]、rename.go s[:140] 会把多字节字符切断。
   trimTo 从 gitlog.go 提到 strutil.go(三个调用方共用),两处改用它;
   n<=0 返回空串。strutil_test.go 6 个单测(含真实 linePreview 路径)。

3. 覆盖率运行不再吞测试输出
   原来 lines 收集后 _ = lines 丢弃,测试失败只看到覆盖率数字。
   改用 ExitCode() 判失败,只回放 FAIL/--- FAIL/panic/# 包/file.go:12 行
   (上限 200),覆盖率页顶部给红底摘要 + 「查看测试输出」按钮。
   coverageFailLines/Summary 纯函数 + 5 个单测。

4. 索引与搜索感知 .gitignore
   新增 ignore.go:dir/、*.ext、/rootonly、带路径、**、! 取反、注释。
   内置跳过扩到 vendor/dist/build/out/target/.next(文件树仍用保守 skipDir)。
   indexState 加 truncated,快速打开底部提示「索引已截断至 N 个文件」。
   ignore_test.go 10 个单测 + 真实遍历集成测试。

验证:go vet ./... 干净;go test ./internal/... 7 包全绿;
e2e 实例 tools/tree 确认菜单 5 项齐全 + tools/shot 截图核对。
2026-11-05 15:43:33 +08:00

179 lines
4.3 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 (
"os"
"path/filepath"
"sort"
"strings"
"sync"
)
// 文件名索引与模糊匹配,供快速打开使用。
// indexFileCap 是文件索引的规模上限。超过就截断并置 indexState.truncated,
// 界面据此提示——静默截断会让用户以为「搜不到就是没有」。
const indexFileCap = 20000
// indexState 是工作区文件索引。
type indexState struct {
mu sync.Mutex
root string
files []string // 相对路径,正斜杠
ready bool
truncated bool // 是否因超过 indexFileCap 被截断
}
// buildIndex 在后台遍历工作区,收集可打开的文件。
//
// 跳过规则有三层:内置目录(.git/vendor/dist…)、根 .gitignore、
// 生成物后缀(.exe/.test)。vendor 必须跳过——一个 Go 项目的 vendor
// 能塞几千个包,进索引后快速打开与全局搜索全是第三方噪音。
func (a *App) buildIndex(root string) {
rules := loadIgnoreRules(root)
go func() {
var files []string
truncated := false
_ = filepath.WalkDir(root, func(p string, d os.DirEntry, err error) error {
if err != nil {
return nil
}
name := d.Name()
rel, relErr := filepath.Rel(root, p)
relSlash := ""
if relErr == nil {
relSlash = filepath.ToSlash(rel)
}
if d.IsDir() {
if p != root && (isBuiltinSkipDir(name) || rules.match(relSlash, true)) {
return filepath.SkipDir
}
return nil
}
if skipFile(name) || rules.match(relSlash, false) {
return nil
}
if relErr != nil {
return nil
}
files = append(files, relSlash)
if len(files) > indexFileCap {
truncated = true
return filepath.SkipAll
}
return nil
})
sort.Strings(files)
a.index.mu.Lock()
if a.index.root == root {
a.index.files = files
a.index.ready = true
a.index.truncated = truncated
}
a.index.mu.Unlock()
}()
}
// resetIndex 换工作区时清空索引。
func (a *App) resetIndex(root string) {
a.index.mu.Lock()
a.index.root = root
a.index.files = nil
a.index.ready = false
a.index.mu.Unlock()
}
// indexSnapshot 取当前索引的快照。
func (a *App) indexSnapshot() ([]string, bool) {
a.index.mu.Lock()
defer a.index.mu.Unlock()
return a.index.files, a.index.ready
}
// indexTruncated 报告索引是否因超过 indexFileCap 被截断。
// 只看已建好的索引:没建好时截断与否还没有意义。
func (a *App) indexTruncated() bool {
a.index.mu.Lock()
defer a.index.mu.Unlock()
return a.index.ready && a.index.truncated
}
// fuzzyScore 判断 query 是否为 path 的子序列,并给出一个分数(越大越靠前)。
//
// 规则很朴素:连续匹配、匹配到文件名部分、匹配位置靠前,都会加分。
func fuzzyScore(path, query string) (int, bool) {
if query == "" {
return 0, true
}
lp := strings.ToLower(path)
lq := strings.ToLower(query)
score := 0
pi := 0
prev := -2
for qi := 0; qi < len(lq); qi++ {
c := lq[qi]
found := -1
for ; pi < len(lp); pi++ {
if lp[pi] == c {
found = pi
break
}
}
if found < 0 {
return 0, false
}
// 连续匹配
if found == prev+1 {
score += 8
} else {
score += 2
}
// 词边界(/ _ - . 之后)加分
if found == 0 || strings.ContainsRune("/_-.\\.", rune(lp[found-1])) {
score += 6
}
prev = found
pi = found + 1
}
// 文件名里命中比目录里命中更值钱
if base := lp[strings.LastIndexByte(lp, '/')+1:]; strings.Contains(base, lq) {
score += 20
}
// 路径越短越靠前
score -= len(path) / 4
return score, true
}
// quickResults 用索引算出快速打开的候选列表。
func (a *App) quickResults(query string, limit int) []paletteItem {
files, _ := a.indexSnapshot()
type scored struct {
rel string
score int
}
var hits []scored
for _, rel := range files {
if s, ok := fuzzyScore(rel, query); ok {
hits = append(hits, scored{rel, s})
}
}
sort.Slice(hits, func(i, j int) bool {
if hits[i].score != hits[j].score {
return hits[i].score > hits[j].score
}
return hits[i].rel < hits[j].rel
})
if len(hits) > limit {
hits = hits[:limit]
}
out := make([]paletteItem, 0, len(hits))
for _, h := range hits {
out = append(out, paletteItem{
Path: filepath.Join(a.root, filepath.FromSlash(h.rel)),
Label: filepath.Base(h.rel),
Hint: filepath.Dir(h.rel),
Kind: itemFile,
})
}
return out
}