Files

126 lines
4.8 KiB
Swift

import Foundation
public struct FileNode: Sendable, Equatable, Identifiable {
public enum Kind: String, Sendable, Equatable {
case directory
case file
}
public var name: String
public var kind: Kind
/// Project-relative, "/"-separated; "" is the project root.
public var path: String
/// nil for files; an empty array for an empty folder.
public var children: [FileNode]?
public var id: String { path }
public var isDirectory: Bool { kind == .directory }
public var isHidden: Bool { name.hasPrefix(".") }
public init(name: String, kind: Kind, path: String, children: [FileNode]? = nil) {
self.name = name
self.kind = kind
self.path = path
self.children = kind == .directory ? (children ?? []) : nil
}
}
/// One visible row of the Explorer, in render order.
public struct TreeRow: Sendable, Equatable {
public var path: String
public var kind: FileNode.Kind
/// 0 for a top-level entry; the Explorer indents 16px per level.
public var depth: Int
public init(path: String, kind: FileNode.Kind, depth: Int) {
self.path = path
self.kind = kind
self.depth = depth
}
}
public enum FileTree {
/// Nested tree from relative file paths: folders first, then files, each sorted by name.
///
/// `emptyDirs` adds the folders that hold no file, which `rg --files` never lists.
/// Names sort case-insensitively, not in byte order.
public static func build(rootName: String, paths: [String], emptyDirs: [String] = []) -> FileNode {
var folders: [String: Set<String>] = ["": []]
var files: [String: Set<String>] = [:]
func ensureFolder(_ parts: ArraySlice<Substring>) -> String {
var parent = ""
for part in parts {
let path = PathUtils.join(parent, String(part))
if folders[path] == nil {
folders[path] = []
folders[parent, default: []].insert(path)
}
parent = path
}
return parent
}
for path in paths {
let parts = path.split(separator: "/")
guard let name = parts.last else { continue }
let parent = ensureFolder(parts.dropLast())
files[parent, default: []].insert(PathUtils.join(parent, String(name)))
}
for dir in emptyDirs {
_ = ensureFolder(dir.split(separator: "/")[...])
}
func node(folder path: String, name: String) -> FileNode {
let subfolders = (folders[path] ?? []).map { node(folder: $0, name: PathUtils.basename($0)) }
let leaves = (files[path] ?? []).map { FileNode(name: PathUtils.basename($0), kind: .file, path: $0) }
return FileNode(name: name, kind: .directory, path: path, children: sortedByName(subfolders) + sortedByName(leaves))
}
return node(folder: "", name: rootName)
}
/// Case-insensitive, locale-aware order.
static func sortedByName(_ nodes: [FileNode]) -> [FileNode] {
nodes.sorted { $0.name.compare($1.name, locale: sortLocale) == .orderedAscending }
}
private static let sortLocale = Locale(identifier: "en_US")
/// The node at a relative path ("" is the root), or nil.
public static func find(_ path: String, in tree: FileNode) -> FileNode? {
if path.isEmpty { return tree }
var node = tree
for part in path.split(separator: "/") {
guard let next = node.children?.first(where: { $0.name == part }) else { return nil }
node = next
}
return node
}
/// A copy of the tree with the children of the node at `path` replaced; unchanged when the path is absent.
public static func replacingChildren(of path: String, with children: [FileNode], in tree: FileNode) -> FileNode {
if tree.path == path {
var copy = tree
copy.children = children
return copy
}
guard let kids = tree.children, path.hasPrefix(tree.path) else { return tree }
var copy = tree
copy.children = kids.map { replacingChildren(of: path, with: children, in: $0) }
return copy
}
/// The visible rows under the root, honouring expanded folders and the hidden-files toggle.
public static func visibleRows(_ tree: FileNode, expanded: Set<String>, showHidden: Bool) -> [TreeRow] {
var rows: [TreeRow] = []
func walk(_ node: FileNode, depth: Int) {
for child in node.children ?? [] where showHidden || !child.isHidden {
rows.append(TreeRow(path: child.path, kind: child.kind, depth: depth))
if child.isDirectory && expanded.contains(child.path) { walk(child, depth: depth + 1) }
}
}
walk(tree, depth: 0)
return rows
}
}