filetree

package
v0.3.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 16, 2026 License: MIT Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package filetree 把文件变更列表拼成目录树,供侧边栏渲染。

算法移植自 Web 版(web/src/fileTree.ts),最关键的一条是:只含单个子目录的 目录链会合并成一行(internal/server/handler 显示成一行而不是三层), 否则深路径会把侧边栏横向撑爆。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FuzzyMatch

func FuzzyMatch(s, query string) bool

FuzzyMatch 做大小写不敏感的子序列匹配,和 godiff 的 Filter files 行为一致: 输入 "inmh" 能匹配 "internal/model/handler.go"。

Types

type Node

type Node struct {
	Key      string // "d:<path>" 或 "f:<path>",用作列表 key 和折叠状态
	Label    string // 显示名;合并的目录链形如 "internal/server"
	Path     string // 完整路径
	Dir      bool
	FileIdx  int // 在输入 files 切片里的下标;目录恒为 -1
	Children []*Node
}

Node 是文件树里的一个节点。

func Build

func Build(files []gitrepo.File) []*Node

Build 把文件列表拼成树。

files 的顺序决定同层内的先后。git status 的输出本身按路径排序, 所以传进来就是有序的。

func BuildAt

func BuildAt(files []gitrepo.File, base int, keyPrefix string) []*Node

BuildAt 和 Build 一样建树,但 FileIdx 从 base 开始数、节点 Key 带 keyPrefix。

文件列表分成「已暂存 / 未暂存」两段、各建一棵树时用:base 把 FileIdx 对回 扁平列表(两段共用同一份切片的拼接),keyPrefix 让两段的折叠状态互不干扰 —— 同一个路径可以同时在两段里(暂存之后又改了),Key 不带前缀就会串。

func Filter

func Filter(nodes []*Node, query string) []*Node

Filter 返回只包含匹配文件的树(保留匹配文件的所有祖先目录)。

query 为空时返回原树。匹配的是完整路径而不是单层文件名 —— 用户 输入 "srv" 时希望找到 internal/server/x.go,而不是只筛当前层的名字。

type Row

type Row struct {
	Node  *Node
	Depth int
}

Row 是树展开成一维列表后的一行,带缩进深度。

func Flatten

func Flatten(nodes []*Node, collapsed map[string]bool, flat bool) []Row

Flatten 把树展开成一维列表,跳过折叠目录里的内容。

flat 为 true 时忽略 collapsed(用于「全部展开」和筛选态 —— 筛选时 必须无视折叠状态,否则搜到的文件可能藏在收起的目录里看不见)。

func FlattenFunc added in v0.3.7

func FlattenFunc(nodes []*Node, collapsed func(key string) bool, flat bool) []Row

FlattenFunc 和 Flatten 一样摊平,但「某个目录是否收起」交给调用方判断。

两种默认值靠这个回调区分:改动列表那棵树默认展开、只记用户收起过哪些; 编辑器栏的工作区树反过来 —— 默认全收起、只认用户展开过的那几个。树本身 不必知道自己是哪一种,也就不会再有人把两份折叠状态混成一个 map。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL