Skip to content

Fix for bug showing preview icon for unknown files #1768

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions CodeEdit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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 */; };
Expand Down Expand Up @@ -916,7 +916,6 @@
6C4104E5297C884F00F472BA /* AboutDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutDetailView.swift; sourceTree = "<group>"; };
6C4104E8297C970F00F472BA /* AboutDefaultView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutDefaultView.swift; sourceTree = "<group>"; };
6C48B5C42C0A2835001E9955 /* FileEncoding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileEncoding.swift; sourceTree = "<group>"; };
6C48B5C72C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSTextStorage+isEmpty.swift"; sourceTree = "<group>"; };
6C48B5CD2C0C1BE4001E9955 /* Shell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shell.swift; sourceTree = "<group>"; };
6C48B5D02C0D0519001E9955 /* ShellIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShellIntegration.swift; sourceTree = "<group>"; };
6C48B5D32C0D0743001E9955 /* codeedit_shell_integration_env.zsh */ = {isa = PBXFileReference; lastKnownFileType = text; path = codeedit_shell_integration_env.zsh; sourceTree = "<group>"; };
Expand Down Expand Up @@ -970,6 +969,7 @@
6CFF967729BEBCF600182D6F /* MainCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainCommands.swift; sourceTree = "<group>"; };
6CFF967929BEBD2400182D6F /* ViewCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewCommands.swift; sourceTree = "<group>"; };
6CFF967B29BEBD5200182D6F /* WindowCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowCommands.swift; sourceTree = "<group>"; };
7755664F2C27FD1B001E7A4D /* CodeFileDocument+UTTypeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CodeFileDocument+UTTypeTests.swift"; sourceTree = "<group>"; };
77A01E1E2BB33FB500F0EA38 /* CEWorkspaceSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CEWorkspaceSettingsView.swift; sourceTree = "<group>"; };
77A01E222BB423A800F0EA38 /* CEWorkspaceSettingsPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CEWorkspaceSettingsPage.swift; sourceTree = "<group>"; };
77A01E242BB4245300F0EA38 /* PageAndCEWorkspaceSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageAndCEWorkspaceSettings.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -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 */,
Expand Down Expand Up @@ -2512,15 +2513,6 @@
path = TerminalEmulator;
sourceTree = "<group>";
};
6C48B5C82C0B5F7A001E9955 /* NSTextStorage */ = {
isa = PBXGroup;
children = (
6C48B5C72C0B5F7A001E9955 /* NSTextStorage+isEmpty.swift */,
);
name = NSTextStorage;
path = CodeEdit/Utils/Extensions/NSTextStorage;
sourceTree = SOURCE_ROOT;
};
6C48B5DB2C0D664A001E9955 /* Model */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -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 */,
Expand Down
11 changes: 2 additions & 9 deletions CodeEdit/Features/Documents/CodeFileDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}