From d7f9d05f032228a3b026189b21923872d38c6a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Larra=C3=B1aga?= Date: Mon, 8 Jul 2024 20:00:58 -0700 Subject: [PATCH 1/5] Fix filename validation for files with only extensions (e.g., '.env', '.gitignore', '.editorconfig') --- .../CEWorkspace/Models/CEWorkspaceFile.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift b/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift index d6f0fec287..2d03120a2a 100644 --- a/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift +++ b/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift @@ -42,7 +42,17 @@ final class CEWorkspaceFile: Codable, Comparable, Hashable, Identifiable, Editor 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 { .init(rawValue: url.pathExtension) ?? .txt } + var type: FileIcon.FileType { + let fileName = url.lastPathComponent + let fileExtension = url.pathExtension + + // Verifies if the filename consists solely of the file extension, such as '.env', '.gitignore', etc. + if fileName.hasPrefix(".") && fileExtension.isEmpty { + return .init(rawValue: String(fileName.dropFirst())) ?? .txt + } else { + return .init(rawValue: fileExtension) ?? .txt + } + } /// Returns the URL of the ``CEWorkspaceFile`` let url: URL From 29329d5737e0513d0898f4b58bd57f6cda07da98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Larra=C3=B1aga?= Date: Mon, 8 Jul 2024 20:26:24 -0700 Subject: [PATCH 2/5] Support for files such as '.env.production', '.env.local' --- CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift b/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift index 2d03120a2a..34e9a9b47e 100644 --- a/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift +++ b/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift @@ -47,8 +47,8 @@ final class CEWorkspaceFile: Codable, Comparable, Hashable, Identifiable, Editor let fileExtension = url.pathExtension // Verifies if the filename consists solely of the file extension, such as '.env', '.gitignore', etc. - if fileName.hasPrefix(".") && fileExtension.isEmpty { - return .init(rawValue: String(fileName.dropFirst())) ?? .txt + if fileName.hasPrefix(".") { + return .init(rawValue: fileName.dropFirst().components(separatedBy: ".").first ?? "") ?? .txt } else { return .init(rawValue: fileExtension) ?? .txt } From 1ae92ae72ecde6e37a760e12726c8300436d2bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Larra=C3=B1aga?= Date: Mon, 8 Jul 2024 20:54:37 -0700 Subject: [PATCH 3/5] Fixes an issue with files like '.eslintrc.js'. Now, if the file starts with a ".", every component will be checked to get a valid type. --- CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift b/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift index 34e9a9b47e..2cc3a719f0 100644 --- a/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift +++ b/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift @@ -46,9 +46,11 @@ final class CEWorkspaceFile: Codable, Comparable, Hashable, Identifiable, Editor let fileName = url.lastPathComponent let fileExtension = url.pathExtension - // Verifies if the filename consists solely of the file extension, such as '.env', '.gitignore', etc. + // Verifies if the filename consists solely file extensions, such as '.env', '.gitignore', etc. if fileName.hasPrefix(".") { - return .init(rawValue: fileName.dropFirst().components(separatedBy: ".").first ?? "") ?? .txt + return fileName.dropFirst().components(separatedBy: ".") + .compactMap { FileIcon.FileType(rawValue: $0) } + .first ?? .txt } else { return .init(rawValue: fileExtension) ?? .txt } From 5ede74c19488f1ac190aa070709df15ab2d69a61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Larra=C3=B1aga?= Date: Mon, 8 Jul 2024 20:54:37 -0700 Subject: [PATCH 4/5] Fixes an issue with files like '.eslintrc.js'. Now, if the file starts with a ".", every component will be checked to get a valid type. --- CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift b/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift index 34e9a9b47e..fe3f31e259 100644 --- a/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift +++ b/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift @@ -46,9 +46,11 @@ final class CEWorkspaceFile: Codable, Comparable, Hashable, Identifiable, Editor let fileName = url.lastPathComponent let fileExtension = url.pathExtension - // Verifies if the filename consists solely of the file extension, such as '.env', '.gitignore', etc. + // Verifies if the filename consists solely of file extensions, such as '.env', '.gitignore', etc. if fileName.hasPrefix(".") { - return .init(rawValue: fileName.dropFirst().components(separatedBy: ".").first ?? "") ?? .txt + return fileName.dropFirst().components(separatedBy: ".") + .compactMap { FileIcon.FileType(rawValue: $0) } + .first ?? .txt } else { return .init(rawValue: fileExtension) ?? .txt } From 2880e68d542c601df16e5cd44d6c0f5f8bbd0f2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonardo=20Larra=C3=B1aga?= Date: Wed, 10 Jul 2024 17:38:44 -0700 Subject: [PATCH 5/5] Prioritize file extension check before handling all "." components. --- .../CEWorkspace/Models/CEWorkspaceFile.swift | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift b/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift index fe3f31e259..3596a944b2 100644 --- a/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift +++ b/CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFile.swift @@ -43,16 +43,20 @@ final class CEWorkspaceFile: Codable, Comparable, Hashable, Identifiable, Editor /// 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 + let filename = url.lastPathComponent.trimmingCharacters(in: .whitespacesAndNewlines) - // Verifies if the filename consists solely of file extensions, such as '.env', '.gitignore', etc. - if fileName.hasPrefix(".") { - return fileName.dropFirst().components(separatedBy: ".") - .compactMap { FileIcon.FileType(rawValue: $0) } - .first ?? .txt + /// First, check if there is a valid file extension. + if let type = FileIcon.FileType(rawValue: filename) { + return type } else { - return .init(rawValue: fileExtension) ?? .txt + /// 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 } }