Skip to content
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

feat: creates file/directory in active path #1416

Merged
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 @@ -16,6 +16,8 @@ struct ProjectNavigatorToolbarBottom: View {

@EnvironmentObject var workspace: WorkspaceDocument

@EnvironmentObject var editorManager: EditorManager

@State var filter: String = ""

var body: some View {
Expand Down Expand Up @@ -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")
Expand Down