diff --git a/CodeEdit.xcodeproj/project.pbxproj b/CodeEdit.xcodeproj/project.pbxproj index b359c60e3..ae3931dd6 100644 --- a/CodeEdit.xcodeproj/project.pbxproj +++ b/CodeEdit.xcodeproj/project.pbxproj @@ -330,7 +330,6 @@ 6C4104E6297C884F00F472BA /* AboutDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C4104E5297C884F00F472BA /* AboutDetailView.swift */; }; 6C4104E9297C970F00F472BA /* AboutDefaultView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C4104E8297C970F00F472BA /* AboutDefaultView.swift */; }; 6C48B5C52C0A2835001E9955 /* FileEncoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5C42C0A2835001E9955 /* FileEncoding.swift */; }; - 6C48B5C92C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5C72C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift */; }; 6C48B5CE2C0C1BE4001E9955 /* Shell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5CD2C0C1BE4001E9955 /* Shell.swift */; }; 6C48B5D12C0D0519001E9955 /* ShellIntegration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5D02C0D0519001E9955 /* ShellIntegration.swift */; }; 6C48B5D42C0D0743001E9955 /* codeedit_shell_integration_env.zsh in Resources */ = {isa = PBXBuildFile; fileRef = 6C48B5D32C0D0743001E9955 /* codeedit_shell_integration_env.zsh */; }; @@ -399,6 +398,7 @@ 6CFF967829BEBCF600182D6F /* MainCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CFF967729BEBCF600182D6F /* MainCommands.swift */; }; 6CFF967A29BEBD2400182D6F /* ViewCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CFF967929BEBD2400182D6F /* ViewCommands.swift */; }; 6CFF967C29BEBD5200182D6F /* WindowCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CFF967B29BEBD5200182D6F /* WindowCommands.swift */; }; + 775566502C27FD1B001E7A4D /* CodeFileDocument+UTTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7755664F2C27FD1B001E7A4D /* CodeFileDocument+UTTypeTests.swift */; }; 77A01E1F2BB33FB500F0EA38 /* CEWorkspaceSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77A01E1E2BB33FB500F0EA38 /* CEWorkspaceSettingsView.swift */; }; 77A01E232BB423A800F0EA38 /* CEWorkspaceSettingsPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77A01E222BB423A800F0EA38 /* CEWorkspaceSettingsPage.swift */; }; 77A01E252BB4245300F0EA38 /* PageAndCEWorkspaceSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77A01E242BB4245300F0EA38 /* PageAndCEWorkspaceSettings.swift */; }; @@ -916,7 +916,6 @@ 6C4104E5297C884F00F472BA /* AboutDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutDetailView.swift; sourceTree = ""; }; 6C4104E8297C970F00F472BA /* AboutDefaultView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutDefaultView.swift; sourceTree = ""; }; 6C48B5C42C0A2835001E9955 /* FileEncoding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileEncoding.swift; sourceTree = ""; }; - 6C48B5C72C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSTextStorage+isEmpty.swift"; sourceTree = ""; }; 6C48B5CD2C0C1BE4001E9955 /* Shell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shell.swift; sourceTree = ""; }; 6C48B5D02C0D0519001E9955 /* ShellIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShellIntegration.swift; sourceTree = ""; }; 6C48B5D32C0D0743001E9955 /* codeedit_shell_integration_env.zsh */ = {isa = PBXFileReference; lastKnownFileType = text; path = codeedit_shell_integration_env.zsh; sourceTree = ""; }; @@ -970,6 +969,7 @@ 6CFF967729BEBCF600182D6F /* MainCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainCommands.swift; sourceTree = ""; }; 6CFF967929BEBD2400182D6F /* ViewCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewCommands.swift; sourceTree = ""; }; 6CFF967B29BEBD5200182D6F /* WindowCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowCommands.swift; sourceTree = ""; }; + 7755664F2C27FD1B001E7A4D /* CodeFileDocument+UTTypeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CodeFileDocument+UTTypeTests.swift"; sourceTree = ""; }; 77A01E1E2BB33FB500F0EA38 /* CEWorkspaceSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CEWorkspaceSettingsView.swift; sourceTree = ""; }; 77A01E222BB423A800F0EA38 /* CEWorkspaceSettingsPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CEWorkspaceSettingsPage.swift; sourceTree = ""; }; 77A01E242BB4245300F0EA38 /* PageAndCEWorkspaceSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageAndCEWorkspaceSettings.swift; sourceTree = ""; }; @@ -1400,6 +1400,7 @@ isa = PBXGroup; children = ( 4EE96ECC296059D200FFBEA8 /* Mocks */, + 7755664F2C27FD1B001E7A4D /* CodeFileDocument+UTTypeTests.swift */, 4EE96ECA2960565E00FFBEA8 /* DocumentsUnitTests.swift */, 6195E3102B640485007261CA /* WorkspaceDocument+SearchState+IndexTests.swift */, 6195E30C2B64044F007261CA /* WorkspaceDocument+SearchState+FindTests.swift */, @@ -2512,15 +2513,6 @@ path = TerminalEmulator; sourceTree = ""; }; - 6C48B5C82C0B5F7A001E9955 /* NSTextStorage */ = { - isa = PBXGroup; - children = ( - 6C48B5C72C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift */, - ); - name = NSTextStorage; - path = CodeEdit/Utils/Extensions/NSTextStorage; - sourceTree = SOURCE_ROOT; - }; 6C48B5DB2C0D664A001E9955 /* Model */ = { isa = PBXGroup; children = ( @@ -3877,6 +3869,7 @@ 583E528C29361B39001AB554 /* CodeEditUITests.swift in Sources */, 613053652B23A49300D767E3 /* TemporaryFile.swift in Sources */, 617DB3DF2C25E13800B58BFE /* TaskNotificationHandlerTests.swift in Sources */, + 775566502C27FD1B001E7A4D /* CodeFileDocument+UTTypeTests.swift in Sources */, 587B60F82934124200D5CD8F /* CEWorkspaceFileManagerTests.swift in Sources */, 6130535F2B23A31300D767E3 /* MemorySearchTests.swift in Sources */, 587B61012934170A00D5CD8F /* UnitTests_Extensions.swift in Sources */, diff --git a/CodeEdit/Features/Documents/CodeFileDocument.swift b/CodeEdit/Features/Documents/CodeFileDocument.swift index 79cbd176b..22e50451e 100644 --- a/CodeEdit/Features/Documents/CodeFileDocument.swift +++ b/CodeEdit/Features/Documents/CodeFileDocument.swift @@ -68,18 +68,11 @@ final class CodeFileDocument: NSDocument, ObservableObject { if content != nil { return .text } + guard let fileType, let type = UTType(fileType) else { return nil } - if type.conforms(to: .text) { - return .text - } - if type.conforms(to: .image) { - return .image - } - if type.conforms(to: .pdf) { - return .pdf - } + return type } diff --git a/CodeEditTests/Features/Documents/CodeFileDocument+UTTypeTests.swift b/CodeEditTests/Features/Documents/CodeFileDocument+UTTypeTests.swift new file mode 100644 index 000000000..cd0149457 --- /dev/null +++ b/CodeEditTests/Features/Documents/CodeFileDocument+UTTypeTests.swift @@ -0,0 +1,77 @@ +// +// CodeFileDocument+UTTypeTests.swift +// CodeEditTests +// +// Created by Axel Martinez on 23/6/24. +// + +import XCTest + +@testable import CodeEdit + +final class UTTypeTests: XCTestCase { + private var document: CodeFileDocument! + + override func setUp() { + document = .init() + } + + func testTextFileByContent() { + document.content = NSTextStorage(string: "Some text content") + XCTAssertEqual(document.utType, .text) + } + + func testJSONFile() { + document.fileType = "public.json" + XCTAssertEqual(document.utType, .json) + } + + func testTextFileByExtension() { + document.fileType = "public.python-script" + XCTAssertEqual(document.utType, .pythonScript) + } + + func testPdfFile() { + document.fileType = "com.adobe.pdf" + XCTAssertEqual(document.utType, .pdf) + } + + func testImageFile() { + document.fileType = "public.image" + XCTAssertEqual(document.utType, .image) + } + + func testPngFile() { + document.fileType = "public.png" + XCTAssertEqual(document.utType, .png) + } + + func testAudioFile() { + document.fileType = "public.audio" + XCTAssertEqual(document.utType, .audio) + } + + func testMp3File() { + document.fileType = "public.mp3" + XCTAssertEqual(document.utType, .mp3) + } + + func testVideoFile() { + document.fileType = "public.video" + XCTAssertEqual(document.utType, .video) + } + + func testMpeg4File() { + document.fileType = "public.mpeg-4" + XCTAssertEqual(document.utType, .mpeg4Movie) + } + + func testUnknownFileType() { + document.fileType = "unknown" + XCTAssertNil(document.utType) + } + + func testEmptyFileTypeAndContent() { + XCTAssertNil(document.utType) + } +}