Repository: CodeEditApp/CodeEdit
File: CodeEdit/Features/Editor/Views/EditorAreaFileView.swift (Swift)

Code before:
//
//  Created by Pavel Kasila on 20.03.22.
//

import AppKit
import AVKit
import CodeEditSourceEditor
import SwiftUI

struct EditorAreaFileView: View {

    @EnvironmentObject private var editorManager: EditorManager
    @EnvironmentObject private var editor: Editor

    @Environment(\.edgeInsets)
    private var edgeInsets

    var file: CEWorkspaceFile
    var textViewCoordinators: [TextViewCoordinator] = []

    @State private var update: Bool = false

    @ViewBuilder var editorAreaFileView: some View {
        if let document = file.fileDocument {

            if let fileURL = document.fileURL, (try? String(contentsOf: fileURL)) != nil {
                CodeFileView(codeFile: document, textViewCoordinators: textViewCoordinators)
            } else {
                NonTextFileView(fileDocument: document)
                    .padding(.top, edgeInsets.top - 1.74) // Use the magic number to fine-tune its appearance.
                    .padding(.bottom, StatusBarView.height + 1.26) // Use the magic number to fine-tune its appearance.
            }

        } else {
            if update {
                Spacer()
            }
            Spacer()
            LoadingFileView(file.name)
            Spacer()
                .onAppear {
                    Task.detached {
                        let contentType = try await file.url.resourceValues(forKeys: [.contentTypeKey]).contentType
                        let codeFile = try await CodeFileDocument(
                            for: file.url,
                            withContentsOf: file.url,
                            ofType: contentType?.identifier ?? ""
                        )
                        await MainActor.run {
                            file.fileDocument = codeFile
                            CodeEditDocumentController.shared.addDocument(codeFile)

Review comment (performance):
Instead of reading the entire file into memory like this, would it work the same way to just check the document's `content` property for `isEmpty`? That would be a much more efficient operation, as this would potentially read a large file into memory quite often.

Code after:
//
//  Created by Pavel Kasila on 20.03.22.
//

import AppKit
import AVKit
import CodeEditSourceEditor
import SwiftUI

struct EditorAreaFileView: View {

    @EnvironmentObject private var editorManager: EditorManager
    @EnvironmentObject private var editor: Editor

    @Environment(\.edgeInsets)
    private var edgeInsets

    var file: CEWorkspaceFile
    var textViewCoordinators: [TextViewCoordinator] = []

    @State private var update: Bool = false

    @ViewBuilder var editorAreaFileView: some View {
        if let document = file.fileDocument {

            if let utType = document.utType, utType.conforms(to: .text) {
                CodeFileView(codeFile: document, textViewCoordinators: textViewCoordinators)
            } else {
                NonTextFileView(fileDocument: document)
                    .padding(.top, edgeInsets.top - 1.74) // Use the magic number to fine-tune its appearance.
                    .padding(.bottom, StatusBarView.height + 1.26) // Use the magic number to fine-tune its appearance.
            }

        } else {
            if update {
                Spacer()
            }
            Spacer()
            LoadingFileView(file.name)
            Spacer()
                .onAppear {
                    Task.detached {
                        let contentType = try await file.url.resourceValues(forKeys: [.contentTypeKey]).contentType
                        let codeFile = try await CodeFileDocument(
                            for: file.url,
                            withContentsOf: file.url,
                            ofType: contentType?.identifier ?? ""
                        )
                        await MainActor.run {
                            file.fileDocument = codeFile
                            CodeEditDocumentController.shared.addDocument(codeFile)