Repository: CodeEditApp/CodeEdit
File: CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift (Swift)

Code before:
/// They can, however, be created standalone when necessary. Creating a standalone ``CEWorkspaceFile`` is useful if
/// loading all intermediate subdirectories (from the nearest cached parent to the file) has not been done yet and doing
/// so would be unnecessary.
///
/// An example of this is in the ``OpenQuicklyView``. This view finds a file URL via a search bar, and needs to display
/// a quick preview of the file. There's a good chance the file is deep in some subdirectory of the workspace, so
/// fetching it from the ``CEWorkspaceFileManager`` may require loading and caching multiple directories. Instead, it
/// just makes a disconnected object and uses it for the preview. Then, when opening the file in the workspace it
/// forces the file to be loaded and cached.
final class CEWorkspaceFile: Codable, Comparable, Hashable, Identifiable, EditorTabRepresentable {

    /// The id of the ``CEWorkspaceFile``.
    ///
    /// This is equal to `url.relativePath`
    var id: String { url.relativePath }

    /// Returns the file name (e.g.: `Package.swift`)
    var name: String { url.lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines) }

    /// Returns the extension of the file or an empty string if no extension is present.
    var type: FileIcon.FileType {
        let fileName = url.lastPathComponent
        let fileExtension = url.pathExtension

        // Verifies if the filename consists solely file extensions, such as '.env', '.gitignore', etc.
        if fileName.hasPrefix(".") {
            return fileName.dropFirst().components(separatedBy: ".")
                .compactMap { FileIcon.FileType(rawValue: $0) }
                .first ?? .txt
        } else {
            return .init(rawValue: fileExtension) ?? .txt
        }
    }

    /// Returns the URL of the ``CEWorkspaceFile``
    let url: URL

    /// Return the icon of the file as `Image`
    var icon: Image {
        if let customImage = NSImage.symbol(named: systemImage) {
            return Image(nsImage: customImage)
        } else {
            return Image(systemName: systemImage)
        }
    }

    /// Return the icon of the file as `NSImage`
    var nsIcon: NSImage {
        if let customImage = NSImage.symbol(named: systemImage) {
            return customImage
        } else {

Review comment (performance):
This is not very efficient in my opinion...  Instead of using `hasPrefix(".")`, we check `fileName.first == "."`. This is faster as it only checks the first character.

Code after:
/// They can, however, be created standalone when necessary. Creating a standalone ``CEWorkspaceFile`` is useful if
/// loading all intermediate subdirectories (from the nearest cached parent to the file) has not been done yet and doing
/// so would be unnecessary.
///
/// An example of this is in the ``OpenQuicklyView``. This view finds a file URL via a search bar, and needs to display
/// a quick preview of the file. There's a good chance the file is deep in some subdirectory of the workspace, so
/// fetching it from the ``CEWorkspaceFileManager`` may require loading and caching multiple directories. Instead, it
/// just makes a disconnected object and uses it for the preview. Then, when opening the file in the workspace it
/// forces the file to be loaded and cached.
final class CEWorkspaceFile: Codable, Comparable, Hashable, Identifiable, EditorTabRepresentable {

    /// The id of the ``CEWorkspaceFile``.
    ///
    /// This is equal to `url.relativePath`
    var id: String { url.relativePath }

    /// Returns the file name (e.g.: `Package.swift`)
    var name: String { url.lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines) }

    /// Returns the extension of the file or an empty string if no extension is present.
    var type: FileIcon.FileType {
        let filename = url.lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines)

        /// First, check if there is a valid file extension.
        if let type = FileIcon.FileType(rawValue: filename) {
            return type
        } else {
            /// If  there's not, verifies every extension for a valid type.
            let extensions = filename.dropFirst().components(separatedBy: ".").reversed()

            return extensions
                .compactMap { FileIcon.FileType(rawValue: $0) }
                .first
            /// Returns .txt for invalid type.
            ?? .txt
        }
    }

    /// Returns the URL of the ``CEWorkspaceFile``
    let url: URL

    /// Return the icon of the file as `Image`
    var icon: Image {
        if let customImage = NSImage.symbol(named: systemImage) {
            return Image(nsImage: customImage)
        } else {
            return Image(systemName: systemImage)
        }
    }

    /// Return the icon of the file as `NSImage`