Repository: CodeEditApp/CodeEdit
File: CodeEditModules/Modules/WorkspaceClient/src/Model/FileItem.swift (Swift)

Code before:

            let deleteConfirmation = NSAlert()
            let message = "\(self.fileName)\(self.isFolder ? " and its children" :"")"
            deleteConfirmation.messageText = "Are you sure you want to move \(message) to the Trash?"
            deleteConfirmation.alertStyle = .critical
            deleteConfirmation.addButton(withTitle: "Delete")
            deleteConfirmation.buttons.last?.hasDestructiveAction = true
            deleteConfirmation.addButton(withTitle: "Cancel")
            if deleteConfirmation.runModal() == .alertFirstButtonReturn { // "Delete" button
                if FileItem.fileManger.fileExists(atPath: self.url.path) {
                    do {
                        try FileItem.fileManger.removeItem(at: self.url)
                    } catch {
                        fatalError(error.localizedDescription)
                    }
                }
            }
        }

        /// This function duplicates the item or folder
        public func duplicate() {
            // if a file/folder with the same name exists, add "copy" to the end
            var fileUrl = self.url
            while FileItem.fileManger.fileExists(atPath: fileUrl.path) {
                let previousName = fileUrl.lastPathComponent
                let previousNameWithoutExtension = (previousName as NSString).deletingPathExtension
                let filextension = fileUrl.pathExtension
                let fileName = filextension.isEmpty ?
                previousName : previousNameWithoutExtension
                fileUrl = fileUrl.deletingLastPathComponent()
                fileUrl.appendPathComponent("\(fileName)_copy.\(filextension)")
            }

            if FileItem.fileManger.fileExists(atPath: self.url.path) {
                do {
                    try FileItem.fileManger.copyItem(at: self.url, to: fileUrl)
                } catch {
                    print("Error at \(self.url.path) to \(fileUrl.path)")
                    fatalError(error.localizedDescription)
                }
            }
        }

        /// This function moves the item or folder if possible
        public func move(to newLocation: URL) {
            guard !FileItem.fileManger.fileExists(atPath: newLocation.path) else { return }
            do {
                try FileItem.fileManger.moveItem(at: self.url, to: newLocation)
            } catch { fatalError(error.localizedDescription) }
        }
    }

Review comment (suggestion):
`deletingPathExtension()` (note: an method) is also available at `URL` so i would avoid the additional casting to ~NSString

Code after:

            let deleteConfirmation = NSAlert()
            let message = "\(self.fileName)\(self.isFolder ? " and its children" :"")"
            deleteConfirmation.messageText = "Are you sure you want to move \(message) to the Trash?"
            deleteConfirmation.alertStyle = .critical
            deleteConfirmation.addButton(withTitle: "Delete")
            deleteConfirmation.buttons.last?.hasDestructiveAction = true
            deleteConfirmation.addButton(withTitle: "Cancel")
            if deleteConfirmation.runModal() == .alertFirstButtonReturn { // "Delete" button
                if FileItem.fileManger.fileExists(atPath: self.url.path) {
                    do {
                        try FileItem.fileManger.removeItem(at: self.url)
                    } catch {
                        fatalError(error.localizedDescription)
                    }
                }
            }
        }

        /// This function duplicates the item or folder
        public func duplicate() {
            // if a file/folder with the same name exists, add "copy" to the end
            var fileUrl = self.url
            while FileItem.fileManger.fileExists(atPath: fileUrl.path) {
                let previousName = fileUrl.deletingPathExtension().lastPathComponent
                let filextension = fileUrl.pathExtension
                let duplicateName = "\(previousName)-copy"

                fileUrl = fileUrl.deletingLastPathComponent()
                fileUrl.appendPathComponent(duplicateName)
                fileUrl.appendPathExtension(filextension)
            }

            if FileItem.fileManger.fileExists(atPath: self.url.path) {
                do {
                    try FileItem.fileManger.copyItem(at: self.url, to: fileUrl)
                } catch {
                    print("Error at \(self.url.path) to \(fileUrl.path)")
                    fatalError(error.localizedDescription)
                }
            }
        }

        /// This function moves the item or folder if possible
        public func move(to newLocation: URL) {
            guard !FileItem.fileManger.fileExists(atPath: newLocation.path) else { return }
            do {
                try FileItem.fileManger.moveItem(at: self.url, to: newLocation)
            } catch { fatalError(error.localizedDescription) }
        }
    }