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

Code before:
            // file system can change outside of the editor

            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 as NSString).deletingPathExtension
                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) }
        }

Review comment (suggestion):
if you change the order then you not need to cast to NSString:

```suggestion
                let previousName = fileUrl.deletingPathExtension().lastPathComponent
```

Code after:
            // file system can change outside of the editor

            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) }
        }