Skip to content

Commit c940687

Browse files
Finished up a working version of the CEWorkspaceFileManager
1 parent e010354 commit c940687

File tree

6 files changed

+391
-5
lines changed

6 files changed

+391
-5
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
587D9B762933BF5700BF7490 /* Mocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 587D9B702933BF5700BF7490 /* Mocks.swift */; };
221221
587D9B772933BF5700BF7490 /* Live.swift in Sources */ = {isa = PBXBuildFile; fileRef = 587D9B712933BF5700BF7490 /* Live.swift */; };
222222
587D9B782933BF5700BF7490 /* Interface.swift in Sources */ = {isa = PBXBuildFile; fileRef = 587D9B722933BF5700BF7490 /* Interface.swift */; };
223+
587FB99029C1246400B519DD /* TabBarItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 587FB98F29C1246400B519DD /* TabBarItemView.swift */; };
223224
58822524292C280D00E83CDE /* StatusBarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 58822509292C280D00E83CDE /* StatusBarView.swift */; };
224225
58822525292C280D00E83CDE /* StatusBarMenuLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5882250B292C280D00E83CDE /* StatusBarMenuLabel.swift */; };
225226
58822526292C280D00E83CDE /* StatusBarBreakpointButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5882250C292C280D00E83CDE /* StatusBarBreakpointButton.swift */; };
@@ -623,6 +624,7 @@
623624
587D9B702933BF5700BF7490 /* Mocks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Mocks.swift; sourceTree = "<group>"; };
624625
587D9B712933BF5700BF7490 /* Live.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Live.swift; sourceTree = "<group>"; };
625626
587D9B722933BF5700BF7490 /* Interface.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Interface.swift; sourceTree = "<group>"; };
627+
587FB98F29C1246400B519DD /* TabBarItemView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TabBarItemView.swift; sourceTree = "<group>"; };
626628
58822509292C280D00E83CDE /* StatusBarView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusBarView.swift; sourceTree = "<group>"; };
627629
5882250B292C280D00E83CDE /* StatusBarMenuLabel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusBarMenuLabel.swift; sourceTree = "<group>"; };
628630
5882250C292C280D00E83CDE /* StatusBarBreakpointButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusBarBreakpointButton.swift; sourceTree = "<group>"; };
@@ -1882,6 +1884,7 @@
18821884
6CDA84AC284C1BA000C1CC3A /* TabBarContextMenu.swift */,
18831885
B6C6A42D29771A8D00A3D28F /* TabBarItemButtonStyle.swift */,
18841886
DE6F77862813625500D00A76 /* TabBarDivider.swift */,
1887+
587FB98F29C1246400B519DD /* TabBarItemView.swift */,
18851888
DE6405A52817734700881FDF /* TabBarNative.swift */,
18861889
DE513F51281B672D002260B9 /* TabBarAccessory.swift */,
18871890
DE513F53281DE5D0002260B9 /* TabBarXcode.swift */,
@@ -2809,6 +2812,7 @@
28092812
58F2EAF8292FB2B0004A9BDE /* AccountListItemView.swift in Sources */,
28102813
587B9E7429301D8F00AC7927 /* URL+URLParameters.swift in Sources */,
28112814
581BFB6B2926431000D251EC /* RecentProjectItem.swift in Sources */,
2815+
587FB99029C1246400B519DD /* TabBarItemView.swift in Sources */,
28122816
587B9DA429300ABD00AC7927 /* OverlayPanel.swift in Sources */,
28132817
58F2EAEE292FB2B0004A9BDE /* TextEditingPreferencesView.swift in Sources */,
28142818
58D01C95293167DC00C5B6B4 /* Bundle+Info.swift in Sources */,

CodeEdit/Features/Documents/WorkspaceDocument.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ import CodeEditKit
136136
override func read(from url: URL, ofType typeName: String) throws {
137137
try initWorkspaceState(url)
138138

139+
Swift.print("Done loading files. . .")
140+
139141
// Initialize Workspace
140142
workspaceFileManager?
141143
.getFiles

CodeEdit/Features/FileSystemClient/Models/CEWorkspaceFile.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ final class CEWorkspaceFile: Codable, Comparable, Hashable, Identifiable, TabBar
113113
}
114114

115115
func activateWatcher() -> Bool {
116-
// check that there is watcher code and that opening the file succeeded
117116
guard let watcherCode else { return false }
117+
118118
let descriptor = open(self.url.path, O_EVTONLY)
119119
guard descriptor > 0 else { return false }
120120

@@ -124,9 +124,11 @@ final class CEWorkspaceFile: Codable, Comparable, Hashable, Identifiable, TabBar
124124
eventMask: .write,
125125
queue: DispatchQueue.global()
126126
)
127+
127128
if descriptor > 2000 {
128129
print("Watcher \(descriptor) used up on \(url.path)")
129130
}
131+
130132
source.setEventHandler { watcherCode(self) }
131133
source.setCancelHandler { close(descriptor) }
132134
source.resume()

CodeEdit/Features/FileSystemClient/Models/CEWorkspaceFileManager.swift

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,51 @@ final class CEWorkspaceFileManager {
3535

3636
self.workspaceItem = CEWorkspaceFile(url: folderUrl, children: [])
3737
self.flattenedFileItems = [workspaceItem.id: workspaceItem]
38+
39+
self.setup()
40+
}
41+
42+
private func setup() {
43+
// initial load
44+
var workspaceFiles: [CEWorkspaceFile]
45+
do {
46+
workspaceFiles = try loadFiles(fromUrl: self.folderUrl)
47+
} catch {
48+
fatalError("Failed to loadFiles")
49+
}
50+
51+
// workspace fileItem
52+
let workspaceFile = CEWorkspaceFile(url: self.folderUrl, children: workspaceFiles)
53+
flattenedFileItems[workspaceFile.id] = workspaceFile
54+
workspaceFiles.forEach { item in
55+
item.parent = workspaceFile
56+
}
57+
58+
// By using `CurrentValueSubject` we can define a starting value.
59+
// The value passed during init it's going to be send as soon as the
60+
// consumer subscribes to the publisher.
61+
let subject = CurrentValueSubject<[CEWorkspaceFile], Never>(workspaceFiles)
62+
63+
self.getFiles = subject
64+
.handleEvents(receiveCancel: {
65+
for item in self.flattenedFileItems.values {
66+
item.watcher?.cancel()
67+
item.watcher = nil
68+
}
69+
})
70+
.receive(on: RunLoop.main)
71+
.eraseToAnyPublisher()
72+
73+
workspaceFile.watcherCode = { sourceFileItem in
74+
self.reloadFromWatcher(sourceFileItem: sourceFileItem)
75+
}
76+
reloadFromWatcher(sourceFileItem: workspaceFile)
3877
}
3978

4079
/// Recursive loading of files into `FileItem`s
4180
/// - Parameter url: The URL of the directory to load the items of
4281
/// - Returns: `[FileItem]` representing the contents of the directory
43-
private func loadFiles(fromURL url: URL) throws -> [CEWorkspaceFile] {
82+
private func loadFiles(fromUrl url: URL) throws -> [CEWorkspaceFile] {
4483
let directoryContents = try fileManager.contentsOfDirectory(
4584
at: url.resolvingSymlinksInPath(),
4685
includingPropertiesForKeys: nil
@@ -57,7 +96,7 @@ final class CEWorkspaceFileManager {
5796

5897
if isDir.boolValue {
5998
// Recursively fetch subdirectories and files if the path points to a directory
60-
subItems = try loadFiles(fromURL: itemURL)
99+
subItems = try loadFiles(fromUrl: itemURL)
61100
}
62101

63102
let newFileItem = CEWorkspaceFile(
@@ -183,7 +222,7 @@ final class CEWorkspaceFileManager {
183222
if fileManager.fileExists(atPath: newContent.path, isDirectory: &isDir) {
184223
var subItems: [CEWorkspaceFile]?
185224

186-
if isDir.boolValue { subItems = try loadFiles(fromURL: newContent) }
225+
if isDir.boolValue { subItems = try loadFiles(fromUrl: newContent) }
187226

188227
let newFileItem = CEWorkspaceFile(
189228
url: newContent,

CodeEdit/Features/QuickOpen/ViewModels/QuickOpenViewModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ final class QuickOpenViewModel: ObservableObject {
5656
return false
5757
}
5858
}.map { url in
59-
WorkspaceClient.FileItem(url: url, children: nil)
59+
CEWorkspaceFile(url: url, children: nil)
6060
}
6161
DispatchQueue.main.async {
6262
self.openQuicklyFiles = files

0 commit comments

Comments
 (0)