Skip to content

Commit e8b1bf5

Browse files
authored
Fix for bug showing preview icon for unknown files (#1768)
* Updated file type computed property so it defaults to text if file type is unknown. * Added unit tests
1 parent f683b6b commit e8b1bf5

File tree

3 files changed

+83
-20
lines changed

3 files changed

+83
-20
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,6 @@
330330
6C4104E6297C884F00F472BA /* AboutDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C4104E5297C884F00F472BA /* AboutDetailView.swift */; };
331331
6C4104E9297C970F00F472BA /* AboutDefaultView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C4104E8297C970F00F472BA /* AboutDefaultView.swift */; };
332332
6C48B5C52C0A2835001E9955 /* FileEncoding.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5C42C0A2835001E9955 /* FileEncoding.swift */; };
333-
6C48B5C92C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5C72C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift */; };
334333
6C48B5CE2C0C1BE4001E9955 /* Shell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5CD2C0C1BE4001E9955 /* Shell.swift */; };
335334
6C48B5D12C0D0519001E9955 /* ShellIntegration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C48B5D02C0D0519001E9955 /* ShellIntegration.swift */; };
336335
6C48B5D42C0D0743001E9955 /* codeedit_shell_integration_env.zsh in Resources */ = {isa = PBXBuildFile; fileRef = 6C48B5D32C0D0743001E9955 /* codeedit_shell_integration_env.zsh */; };
@@ -399,6 +398,7 @@
399398
6CFF967829BEBCF600182D6F /* MainCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CFF967729BEBCF600182D6F /* MainCommands.swift */; };
400399
6CFF967A29BEBD2400182D6F /* ViewCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CFF967929BEBD2400182D6F /* ViewCommands.swift */; };
401400
6CFF967C29BEBD5200182D6F /* WindowCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CFF967B29BEBD5200182D6F /* WindowCommands.swift */; };
401+
775566502C27FD1B001E7A4D /* CodeFileDocument+UTTypeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7755664F2C27FD1B001E7A4D /* CodeFileDocument+UTTypeTests.swift */; };
402402
77A01E1F2BB33FB500F0EA38 /* CEWorkspaceSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77A01E1E2BB33FB500F0EA38 /* CEWorkspaceSettingsView.swift */; };
403403
77A01E232BB423A800F0EA38 /* CEWorkspaceSettingsPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77A01E222BB423A800F0EA38 /* CEWorkspaceSettingsPage.swift */; };
404404
77A01E252BB4245300F0EA38 /* PageAndCEWorkspaceSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77A01E242BB4245300F0EA38 /* PageAndCEWorkspaceSettings.swift */; };
@@ -916,7 +916,6 @@
916916
6C4104E5297C884F00F472BA /* AboutDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutDetailView.swift; sourceTree = "<group>"; };
917917
6C4104E8297C970F00F472BA /* AboutDefaultView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutDefaultView.swift; sourceTree = "<group>"; };
918918
6C48B5C42C0A2835001E9955 /* FileEncoding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileEncoding.swift; sourceTree = "<group>"; };
919-
6C48B5C72C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSTextStorage+isEmpty.swift"; sourceTree = "<group>"; };
920919
6C48B5CD2C0C1BE4001E9955 /* Shell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shell.swift; sourceTree = "<group>"; };
921920
6C48B5D02C0D0519001E9955 /* ShellIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShellIntegration.swift; sourceTree = "<group>"; };
922921
6C48B5D32C0D0743001E9955 /* codeedit_shell_integration_env.zsh */ = {isa = PBXFileReference; lastKnownFileType = text; path = codeedit_shell_integration_env.zsh; sourceTree = "<group>"; };
@@ -970,6 +969,7 @@
970969
6CFF967729BEBCF600182D6F /* MainCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainCommands.swift; sourceTree = "<group>"; };
971970
6CFF967929BEBD2400182D6F /* ViewCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewCommands.swift; sourceTree = "<group>"; };
972971
6CFF967B29BEBD5200182D6F /* WindowCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowCommands.swift; sourceTree = "<group>"; };
972+
7755664F2C27FD1B001E7A4D /* CodeFileDocument+UTTypeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CodeFileDocument+UTTypeTests.swift"; sourceTree = "<group>"; };
973973
77A01E1E2BB33FB500F0EA38 /* CEWorkspaceSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CEWorkspaceSettingsView.swift; sourceTree = "<group>"; };
974974
77A01E222BB423A800F0EA38 /* CEWorkspaceSettingsPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CEWorkspaceSettingsPage.swift; sourceTree = "<group>"; };
975975
77A01E242BB4245300F0EA38 /* PageAndCEWorkspaceSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageAndCEWorkspaceSettings.swift; sourceTree = "<group>"; };
@@ -1400,6 +1400,7 @@
14001400
isa = PBXGroup;
14011401
children = (
14021402
4EE96ECC296059D200FFBEA8 /* Mocks */,
1403+
7755664F2C27FD1B001E7A4D /* CodeFileDocument+UTTypeTests.swift */,
14031404
4EE96ECA2960565E00FFBEA8 /* DocumentsUnitTests.swift */,
14041405
6195E3102B640485007261CA /* WorkspaceDocument+SearchState+IndexTests.swift */,
14051406
6195E30C2B64044F007261CA /* WorkspaceDocument+SearchState+FindTests.swift */,
@@ -2512,15 +2513,6 @@
25122513
path = TerminalEmulator;
25132514
sourceTree = "<group>";
25142515
};
2515-
6C48B5C82C0B5F7A001E9955 /* NSTextStorage */ = {
2516-
isa = PBXGroup;
2517-
children = (
2518-
6C48B5C72C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift */,
2519-
);
2520-
name = NSTextStorage;
2521-
path = CodeEdit/Utils/Extensions/NSTextStorage;
2522-
sourceTree = SOURCE_ROOT;
2523-
};
25242516
6C48B5DB2C0D664A001E9955 /* Model */ = {
25252517
isa = PBXGroup;
25262518
children = (
@@ -3877,6 +3869,7 @@
38773869
583E528C29361B39001AB554 /* CodeEditUITests.swift in Sources */,
38783870
613053652B23A49300D767E3 /* TemporaryFile.swift in Sources */,
38793871
617DB3DF2C25E13800B58BFE /* TaskNotificationHandlerTests.swift in Sources */,
3872+
775566502C27FD1B001E7A4D /* CodeFileDocument+UTTypeTests.swift in Sources */,
38803873
587B60F82934124200D5CD8F /* CEWorkspaceFileManagerTests.swift in Sources */,
38813874
6130535F2B23A31300D767E3 /* MemorySearchTests.swift in Sources */,
38823875
587B61012934170A00D5CD8F /* UnitTests_Extensions.swift in Sources */,

CodeEdit/Features/Documents/CodeFileDocument.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,11 @@ final class CodeFileDocument: NSDocument, ObservableObject {
6868
if content != nil {
6969
return .text
7070
}
71+
7172
guard let fileType, let type = UTType(fileType) else {
7273
return nil
7374
}
74-
if type.conforms(to: .text) {
75-
return .text
76-
}
77-
if type.conforms(to: .image) {
78-
return .image
79-
}
80-
if type.conforms(to: .pdf) {
81-
return .pdf
82-
}
75+
8376
return type
8477
}
8578

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// CodeFileDocument+UTTypeTests.swift
3+
// CodeEditTests
4+
//
5+
// Created by Axel Martinez on 23/6/24.
6+
//
7+
8+
import XCTest
9+
10+
@testable import CodeEdit
11+
12+
final class UTTypeTests: XCTestCase {
13+
private var document: CodeFileDocument!
14+
15+
override func setUp() {
16+
document = .init()
17+
}
18+
19+
func testTextFileByContent() {
20+
document.content = NSTextStorage(string: "Some text content")
21+
XCTAssertEqual(document.utType, .text)
22+
}
23+
24+
func testJSONFile() {
25+
document.fileType = "public.json"
26+
XCTAssertEqual(document.utType, .json)
27+
}
28+
29+
func testTextFileByExtension() {
30+
document.fileType = "public.python-script"
31+
XCTAssertEqual(document.utType, .pythonScript)
32+
}
33+
34+
func testPdfFile() {
35+
document.fileType = "com.adobe.pdf"
36+
XCTAssertEqual(document.utType, .pdf)
37+
}
38+
39+
func testImageFile() {
40+
document.fileType = "public.image"
41+
XCTAssertEqual(document.utType, .image)
42+
}
43+
44+
func testPngFile() {
45+
document.fileType = "public.png"
46+
XCTAssertEqual(document.utType, .png)
47+
}
48+
49+
func testAudioFile() {
50+
document.fileType = "public.audio"
51+
XCTAssertEqual(document.utType, .audio)
52+
}
53+
54+
func testMp3File() {
55+
document.fileType = "public.mp3"
56+
XCTAssertEqual(document.utType, .mp3)
57+
}
58+
59+
func testVideoFile() {
60+
document.fileType = "public.video"
61+
XCTAssertEqual(document.utType, .video)
62+
}
63+
64+
func testMpeg4File() {
65+
document.fileType = "public.mpeg-4"
66+
XCTAssertEqual(document.utType, .mpeg4Movie)
67+
}
68+
69+
func testUnknownFileType() {
70+
document.fileType = "unknown"
71+
XCTAssertNil(document.utType)
72+
}
73+
74+
func testEmptyFileTypeAndContent() {
75+
XCTAssertNil(document.utType)
76+
}
77+
}

0 commit comments

Comments
 (0)