package app import ( "path/filepath" "strings" ) // tabLabel 返回标签上显示的名字。 // // 没有重名就只显示文件名;有重名时显示「能区分它的最短后缀」: // a/main.go 与 b/main.go 显示成 a/main.go / b/main.go, // 若还撞(internal/x/main.go 与 cmd/x/main.go)就再多带一层。 // // 为什么不统一显示相对工作区的全路径:标签栏只有一行,全路径会把 // 后面的标签挤到看不见。只在冲突时加长,是 VS Code 的做法。 func (a *App) tabLabel(d *Doc) string { base := filepath.Base(d.path) same := 0 for _, o := range a.docs { if strings.EqualFold(filepath.Base(o.path), base) { same++ } } if same <= 1 { return base } return a.shortestUniqueSuffix(d.path) } // shortestUniqueSuffix 从路径尾部往上取段,直到与其它同名文件不再冲突。 func (a *App) shortestUniqueSuffix(path string) string { self := strings.Split(filepath.ToSlash(path), "/") base := filepath.Base(path) // 只和「同名」的那些比,别的标签无关。 var others [][]string for _, o := range a.docs { if strings.EqualFold(filepath.Base(o.path), base) && o.path != path { others = append(others, strings.Split(filepath.ToSlash(o.path), "/")) } } for depth := 2; depth <= len(self); depth++ { cand := strings.Join(self[len(self)-depth:], "/") uniq := true for _, op := range others { if len(op) < depth { continue } if strings.EqualFold(strings.Join(op[len(op)-depth:], "/"), cand) { uniq = false break } } if uniq { return cand } } // 全路径都一样(理论上不会有),原样返回。 return strings.Join(self, "/") }