forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkspacePool.swift
More file actions
242 lines (208 loc) · 7.49 KB
/
Copy pathWorkspacePool.swift
File metadata and controls
242 lines (208 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import Dependencies
import Foundation
import XcodeInspector
public struct WorkspacePoolDependencyKey: DependencyKey {
public static var liveValue: WorkspacePool = .init()
}
public extension DependencyValues {
var workspacePool: WorkspacePool {
get { self[WorkspacePoolDependencyKey.self] }
set { self[WorkspacePoolDependencyKey.self] = newValue }
}
}
@globalActor public enum WorkspaceActor {
public actor TheActor {}
public static let shared = TheActor()
}
public class WorkspacePool {
public enum Error: Swift.Error, LocalizedError {
case invalidWorkspaceURL(URL)
public var errorDescription: String? {
switch self {
case let .invalidWorkspaceURL(url):
return "Invalid workspace URL: \(url)"
}
}
}
public internal(set) var workspaces: [URL: Workspace] = [:]
var plugins = [ObjectIdentifier: (Workspace) -> WorkspacePlugin]()
var filespacePlugins = [ObjectIdentifier: (Filespace) -> FilespacePlugin]()
public init(
workspaces: [URL: Workspace] = [:],
plugins: [ObjectIdentifier: (Workspace) -> WorkspacePlugin] = [:]
) {
self.workspaces = workspaces
self.plugins = plugins
}
public func registerPlugin<Plugin: WorkspacePlugin>(_ plugin: @escaping (Workspace) -> Plugin) {
let id = ObjectIdentifier(Plugin.self)
let erasedPlugin: (Workspace) -> WorkspacePlugin = { plugin($0) }
plugins[id] = erasedPlugin
for workspace in workspaces.values {
addPlugin(erasedPlugin, id: id, to: workspace)
}
}
public func unregisterPlugin<Plugin: WorkspacePlugin>(_: Plugin.Type) {
let id = ObjectIdentifier(Plugin.self)
plugins[id] = nil
for workspace in workspaces.values {
removePlugin(id: id, from: workspace)
}
}
public func registerPlugin<Plugin: FilespacePlugin>(_ plugin: @escaping (Filespace) -> Plugin) {
let id = ObjectIdentifier(Plugin.self)
let erasedPlugin: (Filespace) -> FilespacePlugin = { plugin($0) }
filespacePlugins[id] = erasedPlugin
for workspace in workspaces.values {
for filespace in workspace.filespaces {
addPlugin(erasedPlugin, id: id, to: filespace.value)
}
}
}
public func unregisterPlugin<Plugin: FilespacePlugin>(_: Plugin.Type) {
let id = ObjectIdentifier(Plugin.self)
filespacePlugins[id] = nil
for workspace in workspaces.values {
for filespace in workspace.filespaces {
removePlugin(id: id, from: filespace.value)
}
}
}
public func fetchFilespaceIfExisted(fileURL: URL) -> Filespace? {
for workspace in workspaces.values {
if let filespace = workspace.filespaces[fileURL] {
return filespace
}
}
return nil
}
@WorkspaceActor
public func fetchOrCreateWorkspace(workspaceURL: URL) async throws -> Workspace {
guard workspaceURL != URL(fileURLWithPath: "/") else {
throw Error.invalidWorkspaceURL(workspaceURL)
}
if let existed = workspaces[workspaceURL] {
return existed
}
let new = createNewWorkspace(workspaceURL: workspaceURL)
workspaces[workspaceURL] = new
return new
}
@WorkspaceActor
public func fetchOrCreateWorkspaceAndFilespace(
fileURL: URL,
checkIfFileExists: Bool = true
) async throws
-> (workspace: Workspace, filespace: Filespace)
{
// If we can get the workspace URL directly.
if let currentWorkspaceURL = await XcodeInspector.shared.safe.realtimeActiveWorkspaceURL {
if let existed = workspaces[currentWorkspaceURL] {
// Reuse the existed workspace.
let filespace = try createNewFilespace(
in: existed,
fileURL: fileURL,
checkIfFileExists: checkIfFileExists
)
return (existed, filespace)
}
let new = createNewWorkspace(workspaceURL: currentWorkspaceURL)
workspaces[currentWorkspaceURL] = new
let filespace = try createNewFilespace(
in: new,
fileURL: fileURL,
checkIfFileExists: checkIfFileExists
)
return (new, filespace)
}
// If not, we try to reuse a filespace if found.
//
// Sometimes, we can't get the project root path from Xcode window, for example, when the
// quick open window in displayed.
for workspace in workspaces.values {
if let filespace = workspace.filespaces[fileURL] {
return (workspace, filespace)
}
}
// If we can't find the workspace URL, we will try to guess it.
// Most of the time we won't enter this branch, just incase.
if let workspaceURL = WorkspaceXcodeWindowInspector.extractProjectURL(
workspaceURL: nil,
documentURL: fileURL
) {
let workspace = {
if let existed = workspaces[workspaceURL] {
return existed
}
// Reuse existed workspace if possible
for (_, workspace) in workspaces {
if fileURL.path.hasPrefix(workspace.projectRootURL.path) {
return workspace
}
}
return createNewWorkspace(workspaceURL: workspaceURL)
}()
let filespace = try createNewFilespace(
in: workspace,
fileURL: fileURL,
checkIfFileExists: checkIfFileExists
)
workspaces[workspaceURL] = workspace
workspace.refreshUpdateTime()
return (workspace, filespace)
}
throw Workspace.CantFindWorkspaceError()
}
@WorkspaceActor
public func removeWorkspace(url: URL) {
workspaces[url] = nil
}
}
extension WorkspacePool {
func addPlugin(
_ plugin: (Workspace) -> WorkspacePlugin,
id: ObjectIdentifier,
to workspace: Workspace
) {
if workspace.plugins[id] != nil { return }
workspace.plugins[id] = plugin(workspace)
}
func removePlugin(id: ObjectIdentifier, from workspace: Workspace) {
workspace.plugins[id] = nil
}
func createNewWorkspace(workspaceURL: URL) -> Workspace {
let new = Workspace(workspaceURL: workspaceURL)
for (id, plugin) in plugins {
addPlugin(plugin, id: id, to: new)
}
return new
}
}
extension WorkspacePool {
func addPlugin(
_ plugin: (Filespace) -> FilespacePlugin,
id: ObjectIdentifier,
to filespace: Filespace
) {
if filespace.plugins[id] != nil { return }
filespace.plugins[id] = plugin(filespace)
}
func removePlugin(id: ObjectIdentifier, from filespace: Filespace) {
filespace.plugins[id] = nil
}
@WorkspaceActor
func createNewFilespace(
in workspace: Workspace,
fileURL: URL,
checkIfFileExists: Bool
) throws -> Filespace {
let filespace = try workspace.createFilespaceIfNeeded(
fileURL: fileURL,
checkIfFileExists: checkIfFileExists
)
for (id, plugin) in filespacePlugins {
addPlugin(plugin, id: id, to: filespace)
}
return filespace
}
}