Skip to content

Open New Files in Workspace #2043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ final class CEWorkspaceFileManager {
return nil
}

// Drill down towards the file, indexing any directories needed.
// If file is not in the `workspaceSettingsFolderURL` or subdirectories, exit.
guard url.absoluteString.starts(with: folderUrl.absoluteString),
url.pathComponents.count > folderUrl.pathComponents.count else {
// If file is not in the `folderUrl` or subdirectories, exit.
guard folderUrl.containsSubPath(url) else {
return nil
}

// Drill down towards the file, indexing any directories needed.
let pathComponents = url.pathComponents.dropFirst(folderUrl.pathComponents.count)
var currentURL = folderUrl

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ final class CodeEditDocumentController: NSDocumentController {
display displayDocument: Bool,
completionHandler: @escaping (NSDocument?, Bool, Error?) -> Void
) {
super.openDocument(withContentsOf: url, display: displayDocument) { document, documentWasAlreadyOpen, error in
guard !openFileInExistingWorkspace(url: url) else {
return
}

super.openDocument(withContentsOf: url, display: displayDocument) { document, documentWasAlreadyOpen, error in
if let document {
self.addDocument(document)
} else {
Expand All @@ -73,6 +76,28 @@ final class CodeEditDocumentController: NSDocumentController {
}
}

/// Attempt to open the file URL in an open workspace, finding the nearest workspace to open it in if possible.
/// - Parameter url: The file URL to open.
/// - Returns: True, if the document was opened in a workspace.
private func openFileInExistingWorkspace(url: URL) -> Bool {
guard !url.isFolder else { return false }
let workspaces = documents.compactMap({ $0 as? WorkspaceDocument })

// Check open workspaces for the file being opened. Sorted by shared components with the url so we
// open the nearest workspace possible.
for workspace in workspaces.sorted(by: {
($0.fileURL?.sharedComponents(url) ?? 0) > ($1.fileURL?.sharedComponents(url) ?? 0)
}) {
// createIfNotFound will still return `nil` if the files don't share a common ancestor.
if let newFile = workspace.workspaceFileManager?.getFile(url.absolutePath, createIfNotFound: true) {
workspace.editorManager?.openTab(item: newFile)
workspace.showWindows()
return true
}
}
return false
}

override func removeDocument(_ document: NSDocument) {
super.removeDocument(document)

Expand Down
35 changes: 35 additions & 0 deletions CodeEdit/Utils/Extensions/URL/URL+componentCompare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,39 @@ extension URL {
func componentCompare(_ other: URL) -> Bool {
return self.pathComponents == other.pathComponents
}

/// Determines if another URL is lower in the file system than this URL.
///
/// Examples:
/// ```
/// URL(filePath: "/Users/Bob/Desktop").containsSubPath(URL(filePath: "/Users/Bob/Desktop/file.txt")) // true
/// URL(filePath: "/Users/Bob/Desktop").containsSubPath(URL(filePath: "/Users/Bob/Desktop/")) // false
/// URL(filePath: "/Users/Bob/Desktop").containsSubPath(URL(filePath: "/Users/Bob/")) // false
/// URL(filePath: "/Users/Bob/Desktop").containsSubPath(URL(filePath: "/Users/Bob/Desktop/Folder")) // true
/// ```
///
/// - Parameter other: The URL to compare.
/// - Returns: True, if the other URL is lower in the file system.
func containsSubPath(_ other: URL) -> Bool {
other.absoluteString.starts(with: absoluteString)
&& other.pathComponents.count > pathComponents.count
}

/// Compares this url with another, counting the number of shared path components. Stops counting once a
/// different component is found.
///
/// - Note: URL treats a leading `/` as a component, so `/Users` and `/` will return `1`.
/// - Parameter other: The URL to compare against.
/// - Returns: The number of shared components.
func sharedComponents(_ other: URL) -> Int {
var count = 0
for (component, otherComponent) in zip(pathComponents, other.pathComponents) {
if component == otherComponent {
count += 1
} else {
return count
}
}
return count
}
}
34 changes: 34 additions & 0 deletions CodeEditTests/Utils/UnitTests_Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,38 @@ final class CodeEditUtilsExtensionsUnitTests: XCTestCase {
let path = #"/Hello World/ With Spaces/ And " Characters "#
XCTAssertEqual(path.escapedDirectory(), #""/Hello World/ With Spaces/ And \" Characters ""#)
}

// MARK: - URL + Contains

func testURLContainsSubPath() {
XCTAssertTrue(URL(filePath: "/Users/Bob/Desktop").containsSubPath(URL(filePath: "/Users/Bob/Desktop/file.txt")))
XCTAssertFalse(URL(filePath: "/Users/Bob/Desktop").containsSubPath(URL(filePath: "/Users/Bob/Desktop/")))
XCTAssertFalse(URL(filePath: "/Users/Bob/Desktop").containsSubPath(URL(filePath: "/Users/Bob/")))
XCTAssertTrue(URL(filePath: "/Users/Bob/Desktop").containsSubPath(URL(filePath: "/Users/Bob/Desktop/Folder")))
}

func testURLSharedComponentsCount() {
// URL Treats the leading `/` as a component, so these all appear to have + 1 but are correct.
XCTAssertEqual(
URL(filePath: "/Users/Bob/Desktop").sharedComponents(URL(filePath: "/Users/Bob/Desktop/file.txt")),
4
)
XCTAssertEqual(
URL(filePath: "/Users/Bob/Desktop").sharedComponents(URL(filePath: "/Users/Bob/Desktop/")),
4
)
XCTAssertEqual(
URL(filePath: "/Users/Bob/Desktop").sharedComponents(URL(filePath: "/Users/Bob/")),
3
)
XCTAssertEqual(
URL(filePath: "/Users/Bob/Desktop").sharedComponents(URL(filePath: "/Users/Bob/Desktop/Folder")),
4
)

XCTAssertEqual(
URL(filePath: "/Users/Bob/Desktop").sharedComponents(URL(filePath: "/Some Other/ Path ")),
1
)
}
}