Skip to content

Commit 105cfd4

Browse files
authored
Push newly created file into the workspace (#696) (#1057)
* Push newly created file into the workspace (#696) * Change DispatchQueue call to avoid two context switches (#696)
1 parent feddd82 commit 105cfd4

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

CodeEdit/Features/NavigatorSidebar/NavigatorSidebarToolbarBottom.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ struct NavigatorSidebarToolbarBottom: View {
3333
Button("Add File") {
3434
guard let folderURL = workspace.workspaceClient?.folderURL() else { return }
3535
guard let root = try? workspace.workspaceClient?.getFileItem(folderURL.path) else { return }
36-
root.addFile(fileName: "untitled") // TODO: use currently selected file instead of root
36+
let newFile = root.addFile(fileName: "untitled") // TODO: use currently selected file instead of root
37+
38+
DispatchQueue.main.async {
39+
guard let newFileItem = try? workspace.workspaceClient?.getFileItem(newFile) else {
40+
return
41+
}
42+
workspace.openTab(item: newFileItem)
43+
}
44+
3745
}
3846
Button("Add Folder") {
3947
guard let folderURL = workspace.workspaceClient?.folderURL() else { return }

CodeEdit/Utils/WorkspaceClient/Model/FileItem.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ extension WorkspaceClient {
214214

215215
/// This function allows creating files in the selected folder or project main directory
216216
/// - Parameter fileName: The name of the new file
217-
func addFile(fileName: String) {
217+
func addFile(fileName: String) -> String {
218218
// check if folder, if it is create file under self
219219
var fileUrl = (self.isFolder ?
220220
self.url.appendingPathComponent(fileName) :
@@ -233,6 +233,8 @@ extension WorkspaceClient {
233233
contents: nil,
234234
attributes: [FileAttributeKey.creationDate: Date()]
235235
)
236+
237+
return fileUrl.path
236238
}
237239

238240
/// This function deletes the item or folder from the current project

0 commit comments

Comments
 (0)