diff --git a/CodeEdit/Features/NavigatorArea/ProjectNavigator/ProjectNavigatorToolbarBottom.swift b/CodeEdit/Features/NavigatorArea/ProjectNavigator/ProjectNavigatorToolbarBottom.swift index 52cc88a72..cc13f54f5 100644 --- a/CodeEdit/Features/NavigatorArea/ProjectNavigator/ProjectNavigatorToolbarBottom.swift +++ b/CodeEdit/Features/NavigatorArea/ProjectNavigator/ProjectNavigatorToolbarBottom.swift @@ -16,6 +16,8 @@ struct ProjectNavigatorToolbarBottom: View { @EnvironmentObject var workspace: WorkspaceDocument + @EnvironmentObject var editorManager: EditorManager + @State var filter: String = "" var body: some View { @@ -50,21 +52,43 @@ struct ProjectNavigatorToolbarBottom: View { } } + /// Retrieves the active tab URL from the underlying editor instance, if theres no + /// active tab, fallbacks to the workspace's root directory + private func activeTabURL() -> URL { + if let selectedTab = editorManager.activeEditor.selectedTab { + if selectedTab.isFolder { + return selectedTab.url + } + + // If the current active tab belongs to a file, pop the filename from + // the path URL to retrieve the folder URL + let activeTabFileURL = selectedTab.url + + if URLComponents(url: activeTabFileURL, resolvingAgainstBaseURL: false) != nil { + var pathComponents = activeTabFileURL.pathComponents + pathComponents.removeLast() + + let fileURL = NSURL.fileURL(withPathComponents: pathComponents)! as URL + return fileURL + } + } + + return workspace.workspaceFileManager.unsafelyUnwrapped.folderUrl + } + private var addNewFileButton: some View { Menu { Button("Add File") { - guard let folderURL = workspace.workspaceFileManager?.folderUrl, - let root = try? workspace.workspaceFileManager?.getFile(folderURL.path) else { return } + let filePathURL = activeTabURL() + guard let workspace = workspace.workspaceFileManager?.getFile(filePathURL.path) else { return } - // TODO: use currently selected file instead of root - root.addFile(fileName: "untitled") + workspace.addFile(fileName: "untitled") } Button("Add Folder") { - guard let folderURL = workspace.workspaceFileManager?.folderUrl, - let root = try? workspace.workspaceFileManager?.getFile(folderURL.path) else { return } + let filePathURL = activeTabURL() + guard let workspace = workspace.workspaceFileManager?.getFile(filePathURL.path) else { return } - // TODO: use currently selected file instead of root - root.addFolder(folderName: "untitled") + workspace.addFolder(folderName: "untitled") } } label: { Image(systemName: "plus")