Sources/OrgApp/FileTree.swift

1e2bc5fb0b3e80e3b2d877c37d07f3ec7f183be2
orgstar/Sources/OrgApp/FileTree.swift

80 lines · 3643 bytes

 1import Foundation
 2import OrgCore
 3import OrgIndex
 4
 5/// One file the workspace knows about.
 6public struct FileEntry: Sendable, Hashable, Identifiable {
 7    public let path: String
 8    public let root: String
 9    public let kind: FileKind
10
11    public var id: String { path }
12    public var name: String { (path as NSString).lastPathComponent }
13    /// Path below the root, for display and agenda globs.
14    public var relativePath: String {
15        path.hasPrefix(root + "/") ? String(path.dropFirst(root.count + 1)) : path
16    }
17}
18
19/// A folder or file in the sidebar.
20public struct FileTreeNode: Sendable, Hashable, Identifiable {
21    public let name: String
22    /// The file's or folder's absolute path.
23    public let path: String
24    public let file: FileEntry?
25    /// Nil for files, so a SwiftUI `OutlineGroup` shows no disclosure triangle.
26    public let children: [FileTreeNode]?
27    /// An iCloud file on its way; it opens once it's indexed.
28    public var isDownloading = false
29
30    public var id: String { path }
31
32    /// Folders first, then files, each by name. Conflict copies are listed with their files.
33    public static func build(root: String, files: [FileEntry], downloading: [String] = []) -> FileTreeNode {
34        final class Folder {
35            var folders: [String: Folder] = [:]
36            var files: [FileEntry] = []
37            var downloading: [String] = []
38        }
39        let top = Folder()
40        for path in downloading where path.hasPrefix(root + "/") {
41            var folder = top
42            for component in path.dropFirst(root.count + 1).split(separator: "/").dropLast() {
43                let name = String(component)
44                if folder.folders[name] == nil { folder.folders[name] = Folder() }
45                folder = folder.folders[name]!
46            }
47            folder.downloading.append(path)
48        }
49        for file in files where file.root == root {
50            var folder = top
51            for component in file.relativePath.split(separator: "/").dropLast() {
52                let name = String(component)
53                if folder.folders[name] == nil { folder.folders[name] = Folder() }
54                folder = folder.folders[name]!
55            }
56            folder.files.append(file)
57        }
58        func node(_ folder: Folder, name: String, path: String) -> FileTreeNode {
59            let folders = folder.folders.keys.sorted { $0.localizedStandardCompare($1) == .orderedAscending }.map {
60                node(folder.folders[$0]!, name: $0, path: path + "/" + $0)
61            }
62            let files = (folder.files.map {
63                FileTreeNode(name: $0.name, path: $0.path, file: $0, children: nil)
64            } + folder.downloading.map {
65                FileTreeNode(name: ($0 as NSString).lastPathComponent, path: $0, file: nil, children: nil, isDownloading: true)
66            }).sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending }
67            return FileTreeNode(name: name, path: path, file: nil, children: folders + files)
68        }
69        return node(top, name: (root as NSString).lastPathComponent, path: root)
70    }
71}
72
73extension FuzzyMatch {
74    /// Entries ranked by `score` against their relative path, best first.
75    public static func rank(_ query: String, _ files: [FileEntry], limit: Int = 50) -> [FileEntry] {
76        let scored: [(file: FileEntry, score: Int)] = files.compactMap { file in score(query, file.relativePath).map { (file, $0) } }
77        let ranked = scored.sorted { a, b in a.score != b.score ? a.score > b.score : a.file.relativePath < b.file.relativePath }
78        return ranked.prefix(limit).map(\.file)
79    }
80}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form