import Foundation import HelderCore /// How the editor can show a file at all. public enum EditorFileKind: Equatable, Sendable { case text case image /// Known binary formats: a placeholder, never a text buffer. case binary private static let binaryExtensions: Set = [ "pdf", "zip", "gz", "tgz", "tar", "bz2", "xz", "7z", "rar", "jar", "war", "class", "woff", "woff2", "ttf", "otf", "eot", "mp3", "mp4", "m4a", "mov", "avi", "wav", "ogg", "webm", "exe", "dll", "so", "dylib", "a", "o", "bin", "dat", "psd", "sqlite", "sqlite3", "db", "phar", "heic", "tiff", "tif", "icns", "DS_Store", ] public static func of(_ path: String) -> EditorFileKind { if FileType.isImage(path) { return .image } if PathUtils.basename(path) == ".DS_Store" { return .binary } return binaryExtensions.contains(FileType.ext(path)) ? .binary : .text } } /// The file-level change, taken from the whole-file diff rather than one git row. public enum FileChangeStatus: String, Equatable, Sendable { case added = "Added" case modified = "Modified" case deleted = "Deleted" public init(_ diff: Diff) { self = diff.isDeletedFile ? .deleted : diff.isAddedFile ? .added : .modified } } /// A one-sentence empty state in place of the code. public enum EditorPlaceholder: Equatable, Sendable { /// Actual of a file the change deleted. case noUpdated case cannotDisplay public var title: String { switch self { case .noUpdated: "No updated version" case .cannotDisplay: "This file cannot be displayed" } } public var note: String? { switch self { case .noUpdated: "This file was deleted in the change." case .cannotDisplay: nil } } } /// A button on the bar above the code. public enum EditorBarButton: Equatable, Sendable { /// Toggles the rendered Markdown. case preview /// Opens the side-by-side Diff overlay. case diff } /// Everything the column needs to decide what to draw for one file. public struct EditorPresentation: Equatable, Sendable { public var path: String public var kind: EditorFileKind public var hasDiff: Bool public var isMarkdown: Bool /// The requested mode resolved against what the file supports. public var mode: ViewMode /// What the pane under the Diff overlay shows: Diff falls back to Actual. public var paneMode: ViewMode public var status: FileChangeStatus? public var placeholder: EditorPlaceholder? /// A mode carried over from another state (or an unchanged file asked for a diff view) /// falls back to a view the file has, so nothing renders blank. `isBinary` is what the read /// found, for a binary file whose extension looks like text. public init(path: String, requested: ViewMode?, diff: Diff?, defaultMode: ViewMode = .actual, isBinary: Bool = false) { self.path = path let byName = EditorFileKind.of(path) kind = isBinary && byName == .text ? .binary : byName hasDiff = kind == .text && diff != nil isMarkdown = FileType.isMarkdown(path) let wanted = requested ?? (hasDiff ? defaultMode : .code) if wanted == .preview && isMarkdown { mode = .preview } else if hasDiff { mode = wanted == .diff ? .diff : .actual } else { mode = .code } paneMode = mode == .diff ? .actual : mode status = diff.map(FileChangeStatus.init) if kind == .binary { placeholder = .cannotDisplay } else if paneMode == .actual && status == .deleted { placeholder = .noUpdated } else { placeholder = nil } } /// Preview only for Markdown, Diff only for a file with changes. public var barButtons: [EditorBarButton] { var buttons: [EditorBarButton] = [] if kind == .text && isMarkdown { buttons.append(.preview) } if hasDiff { buttons.append(.diff) } return buttons } /// Actual and Code edit the live buffer; Preview is a read-only view. public var isEditable: Bool { kind == .text && placeholder == nil && (paneMode == .code || paneMode == .actual) } /// A marked line offers Show diff, except on a new file where every line is marked. public var offersShowDiff: Bool { paneMode == .actual && hasDiff && status != .added } }