Skip to content

Commit 16c76b4

Browse files
committed
Update
1 parent 7288b61 commit 16c76b4

3 files changed

Lines changed: 38 additions & 18 deletions

File tree

Tool/Sources/Environment/Environment.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public enum Environment {
4141
return false
4242
}
4343
}
44-
45-
public static var fetchCurrentProjectRootURLFromXcode: () async throws -> URL? = {
44+
45+
public static var fetchCurrentWorkspaceURLFromXcode: () async throws -> URL? = {
4646
if let xcode = ActiveApplicationMonitor.shared.activeXcode
4747
?? ActiveApplicationMonitor.shared.latestXcode
4848
{
@@ -53,11 +53,6 @@ public enum Environment {
5353
let path = child.description
5454
let trimmedNewLine = path.trimmingCharacters(in: .newlines)
5555
var url = URL(fileURLWithPath: trimmedNewLine)
56-
while !FileManager.default.fileIsDirectory(atPath: url.path) ||
57-
!url.pathExtension.isEmpty
58-
{
59-
url = url.deletingLastPathComponent()
60-
}
6156
return url
6257
}
6358
}
@@ -66,6 +61,19 @@ public enum Environment {
6661
return nil
6762
}
6863

64+
public static var fetchCurrentProjectRootURLFromXcode: () async throws -> URL? = {
65+
if var url = try await fetchCurrentWorkspaceURLFromXcode() {
66+
while !FileManager.default.fileIsDirectory(atPath: url.path) ||
67+
!url.pathExtension.isEmpty
68+
{
69+
url = url.deletingLastPathComponent()
70+
}
71+
return url
72+
}
73+
74+
return nil
75+
}
76+
6977
public static var guessProjectRootURLForFile: (_ fileURL: URL) async throws -> URL = {
7078
fileURL in
7179
var currentURL = fileURL
@@ -250,7 +258,7 @@ func runAppleScript(_ appleScript: String) async throws -> String {
250258
}
251259
}
252260

253-
extension FileManager {
261+
public extension FileManager {
254262
func fileIsDirectory(atPath path: String) -> Bool {
255263
var isDirectory: ObjCBool = false
256264
let exists = fileExists(atPath: path, isDirectory: &isDirectory)

Tool/Sources/Workspace/Workspace.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class WorkspacePropertyValues {
3030
open class WorkspacePlugin {
3131
public private(set) weak var workspace: Workspace?
3232
public var projectRootURL: URL { workspace?.projectRootURL ?? URL(fileURLWithPath: "/") }
33+
public var workspaceURL: URL { workspace?.workspaceURL ?? projectRootURL }
3334
public var filespaces: [URL: Filespace] { workspace?.filespaces ?? [:] }
3435

3536
public init(workspace: Workspace) {
@@ -57,6 +58,7 @@ public final class Workspace {
5758

5859
var additionalProperties = WorkspacePropertyValues()
5960
public internal(set) var plugins = [ObjectIdentifier: WorkspacePlugin]()
61+
public let workspaceURL: URL
6062
public let projectRootURL: URL
6163
public let openedFileRecoverableStorage: OpenedFileRecoverableStorage
6264
public private(set) var lastSuggestionUpdateTime = Environment.now()
@@ -84,8 +86,18 @@ public final class Workspace {
8486
plugins[ObjectIdentifier(type)] as? P
8587
}
8688

87-
init(projectRootURL: URL) {
88-
self.projectRootURL = projectRootURL
89+
init(workspaceURL: URL) {
90+
self.workspaceURL = workspaceURL
91+
self.projectRootURL = {
92+
var url = workspaceURL
93+
while !FileManager.default.fileIsDirectory(atPath: url.path) ||
94+
!url.pathExtension.isEmpty
95+
{
96+
url = url.deletingLastPathComponent()
97+
}
98+
return url
99+
100+
}()
89101
openedFileRecoverableStorage = .init(projectRootURL: projectRootURL)
90102
let openedFiles = openedFileRecoverableStorage.openedFiles
91103
Task { @WorkspaceActor in
@@ -134,7 +146,7 @@ public final class Workspace {
134146
public func closeFilespace(fileURL: URL) {
135147
filespaces[fileURL] = nil
136148
}
137-
149+
138150
@WorkspaceActor
139151
public func didUpdateFilespace(fileURL: URL, content: String) {
140152
guard let filespace = filespaces[fileURL] else { return }

Tool/Sources/Workspace/WorkspacePool.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ public class WorkspacePool {
5656
}
5757

5858
// If we know which project is opened.
59-
if let currentProjectURL = try await Environment.fetchCurrentProjectRootURLFromXcode() {
60-
if let existed = workspaces[currentProjectURL] {
59+
if let currentWorkspaceURL = try await Environment.fetchCurrentWorkspaceURLFromXcode() {
60+
if let existed = workspaces[currentWorkspaceURL] {
6161
let filespace = existed.createFilespaceIfNeeded(fileURL: fileURL)
6262
return (existed, filespace)
6363
}
6464

65-
let new = createNewWorkspace(projectRootURL: currentProjectURL)
66-
workspaces[currentProjectURL] = new
65+
let new = createNewWorkspace(workspaceURL: currentWorkspaceURL)
66+
workspaces[currentWorkspaceURL] = new
6767
let filespace = new.createFilespaceIfNeeded(fileURL: fileURL)
6868
return (new, filespace)
6969
}
@@ -93,7 +93,7 @@ public class WorkspacePool {
9393
return workspace
9494
}
9595
}
96-
return createNewWorkspace(projectRootURL: workspaceURL)
96+
return createNewWorkspace(workspaceURL: workspaceURL)
9797
}()
9898

9999
let filespace = workspace.createFilespaceIfNeeded(fileURL: fileURL)
@@ -121,8 +121,8 @@ extension WorkspacePool {
121121
workspace.plugins[id] = nil
122122
}
123123

124-
func createNewWorkspace(projectRootURL: URL) -> Workspace {
125-
let new = Workspace(projectRootURL: projectRootURL)
124+
func createNewWorkspace(workspaceURL: URL) -> Workspace {
125+
let new = Workspace(workspaceURL: workspaceURL)
126126
for (id, plugin) in plugins {
127127
addPlugin(plugin, id: id, to: new)
128128
}

0 commit comments

Comments
 (0)