@@ -88,29 +88,73 @@ extension CEWorkspaceFileManager {
8888 )
8989 }
9090
91- /// This function deletes the item or folder from the current project
91+ /// This function deletes the item or folder from the current project by moving to Trash
92+ /// - Parameters:
93+ /// - file: The file or folder to delete
94+ /// - Authors: Paul Ebose
95+ public func trash( file: CEWorkspaceFile ) {
96+ let message : String
97+
98+ if !file. isFolder || file. isEmptyFolder { // if its a file or an empty folder, call it by its name
99+ message = file. name
100+ } else {
101+ let fileCount = fileManager. enumerator ( atPath: file. url. path) ? . allObjects. count
102+ message = " the \( ( fileCount ?? 0 ) + 1 ) selected items "
103+ }
104+
105+ let moveFileToTrashAlert = NSAlert ( )
106+ moveFileToTrashAlert. messageText = " Do you want to move \( message) to Trash? "
107+ moveFileToTrashAlert. informativeText = " This operation cannot be undone. "
108+ moveFileToTrashAlert. alertStyle = . critical
109+ moveFileToTrashAlert. addButton ( withTitle: " Move to Trash " )
110+ moveFileToTrashAlert. buttons. last? . hasDestructiveAction = true
111+ moveFileToTrashAlert. addButton ( withTitle: " Cancel " )
112+
113+ if moveFileToTrashAlert. runModal ( ) == . alertFirstButtonReturn { // "Move to Trash" button
114+ if fileManager. fileExists ( atPath: file. url. path) {
115+ do {
116+ try fileManager. trashItem ( at: file. url, resultingItemURL: nil )
117+ } catch {
118+ print ( error. localizedDescription)
119+ }
120+ }
121+ }
122+ }
123+
124+ /// This function deletes the item or folder from the current project by erasing immediately.
92125 /// - Parameters:
93126 /// - file: The file to delete
94127 /// - confirmDelete: True to present an alert to confirm the delete.
95- /// - Authors: Mattijs Eikelenboom, KaiTheRedNinja. *Moved from 7c27b1e*
128+ /// - Authors: Mattijs Eikelenboom, KaiTheRedNinja., Paul Ebose *Moved from 7c27b1e*
96129 public func delete( file: CEWorkspaceFile , confirmDelete: Bool = true ) {
97130 // This function also has to account for how the
98131 // - file system can change outside of the editor
99- let deleteConfirmation = NSAlert ( )
100- let message : String
132+ let messageText : String
133+ let informativeText : String
134+
101135 if !file. isFolder || file. isEmptyFolder { // if its a file or an empty folder, call it by its name
102- message = file. name
136+ messageText = " Are you sure you want to delete \" \( file. name) \" ? "
137+ informativeText = " This item will be deleted immediately. You can't undo this action. "
103138 } else {
104139 let childrenCount = try ? fileManager. contentsOfDirectory (
105140 at: file. url,
106141 includingPropertiesForKeys: nil
107142 ) . count
108- message = " the \( ( childrenCount ?? 0 ) + 1 ) selected items "
143+
144+ if let childrenCount {
145+ messageText = " Are you sure you want to delete the \( ( childrenCount) + 1 ) selected items? "
146+ informativeText = " \( childrenCount) items will be deleted immediately. You can't undo this action. "
147+ } else {
148+ messageText = " Are you sure you want to delete the selected items? "
149+ informativeText = " Selected items will be deleted immediately. You can't undo this action. "
150+ }
109151 }
110- deleteConfirmation. messageText = " Do you want to move \( message) to the Trash? "
111- deleteConfirmation. informativeText = " This operation cannot be undone "
152+
153+ let deleteConfirmation = NSAlert ( )
154+ deleteConfirmation. messageText = messageText
155+ deleteConfirmation. informativeText = informativeText
112156 deleteConfirmation. alertStyle = . critical
113- deleteConfirmation. addButton ( withTitle: " Move to Trash " )
157+ deleteConfirmation. addButton ( withTitle: " Delete " )
114158 deleteConfirmation. buttons. last? . hasDestructiveAction = true
115159 deleteConfirmation. addButton ( withTitle: " Cancel " )
116160 if !confirmDelete || deleteConfirmation. runModal ( ) == . alertFirstButtonReturn { // "Delete" button
0 commit comments