import Foundation /// What was right-clicked. public enum ContextTarget: Sendable, Equatable { /// A line or selection in the editor; `code` is the selected text, if any. /// `showDiffLine` is the clicked line when it is marked and the file offers its Diff. case editor(reference: AgentReference, code: String?, showDiffLine: Int? = nil) case file(path: String) case directory(path: String) /// A git row; the row carries its own half, since one file can have two rows. case gitRow(path: String, staged: Bool) /// The empty space under the tree: the project folder itself. case projectRoot case terminal(TerminalKind, hasSelection: Bool) public var path: String { switch self { case let .editor(reference, _, _): reference.path case let .file(path), let .directory(path), let .gitRow(path, _): path case .projectRoot, .terminal: "" } } } public enum ContextAction: Sendable, Equatable { /// Opens the pass-on prompt; with code for an editor selection. case passOnReference(String, code: String?) case copyReference(String) case passOnFileName(String) case copyFileName(String) case newFile(inDirectory: String) case newFolder(inDirectory: String) case stage(String) case unstage(String) case openDiff(String) /// Opens the Diff of the open file at this line. case showDiff(String, line: Int) case discard(String) case rename(String, isDirectory: Bool) case revealInFinder(String) case delete(String, isDirectory: Bool) case copyTerminalSelection(TerminalKind) case pasteIntoTerminal(TerminalKind) /// Reads the pane's selection aloud; the text is fetched when the item runs. case readAloudTerminalSelection(TerminalKind) /// Inserts `/wait-what` with the pane's selection into the agent composer. case waitWhat(TerminalKind) /// Inserts `/bouwen ` into the agent composer for an ontwikkeling folder. case bouwen(String) /// Reads a Markdown file's prose aloud. case readAloudFile(String) case readAloudText(String) /// Sends the editor selection to Plak as it is, code and all. case plak(String) /// Sends the pane's selection to Plak; the text is fetched when the item runs. case plakTerminalSelection(TerminalKind) /// Destructive items read in amber-deep. public var isDestructive: Bool { switch self { case .discard, .delete: true default: false } } } /// A menu entry, or a separator between groups. public enum ContextMenuEntry: Sendable, Equatable { case action(ContextAction, label: String) case separator } public enum ContextActions { /// The header line of the menu: the path, or `project/` for the root; the editor, folders and terminals have none. public static func header(for target: ContextTarget, projectName: String) -> String { switch target { case let .file(path), let .gitRow(path, _): path case .projectRoot: projectName + "/" case .editor, .directory, .terminal: "" } } /// Menu entries: every "Pass on …" item has its "Copy …" twin directly below. public static func entries(for target: ContextTarget) -> [ContextMenuEntry] { switch target { case let .editor(reference, code, showDiffLine): let ref = reference.description let hasCode = !(code ?? "").isEmpty let showDiff: [ContextMenuEntry] = showDiffLine.map { [.separator, .action(.showDiff(reference.path, line: $0), label: "Show diff")] } ?? [] return [ .action(.passOnReference(ref, code: hasCode ? code : nil), label: hasCode ? "Pass on selection" : "Pass on reference"), .action(.copyReference(ref), label: "Copy reference"), ] + (hasCode ? readAloudSelection(code ?? "", path: reference.path) + plak(code ?? "") : []) + showDiff case .projectRoot: return [ .action(.newFile(inDirectory: ""), label: "New file"), .action(.revealInFinder(""), label: "Show in Finder"), ] case let .file(path): return fileHead(path) + readAloudFile(path) + [ .separator, .action(.rename(path, isDirectory: false), label: "Rename file"), .action(.revealInFinder(path), label: "Show in Finder"), .action(.delete(path, isDirectory: false), label: "Delete file"), ] case let .directory(path): let ref = path + "/" let bouwen: [ContextMenuEntry] = ontwikkelingName(path).map { [.action(.bouwen($0), label: "Bouwen"), .separator] } ?? [] return bouwen + [ .action(.passOnReference(ref, code: nil), label: "Pass on reference"), .action(.copyReference(ref), label: "Copy reference"), .separator, .action(.newFile(inDirectory: path), label: "New file"), .action(.newFolder(inDirectory: path), label: "New folder"), .separator, .action(.rename(path, isDirectory: true), label: "Rename folder"), .action(.revealInFinder(path), label: "Show in Finder"), .action(.delete(path, isDirectory: true), label: "Delete folder"), ] case let .terminal(kind, hasSelection): let readAloud: [ContextMenuEntry] = hasSelection ? [.separator, .action(.readAloudTerminalSelection(kind), label: "Read aloud selection"), .separator, .action(.plakTerminalSelection(kind), label: "Plak")] : [] let waitWhat: [ContextMenuEntry] = hasSelection && kind == .agent ? [.separator, .action(.waitWhat(kind), label: "Wait what?")] : [] return (hasSelection ? [.action(.copyTerminalSelection(kind), label: "Copy")] : []) + [.action(.pasteIntoTerminal(kind), label: "Paste")] + readAloud + waitWhat case let .gitRow(path, staged): return fileHead(path) + readAloudFile(path) + [ .separator, staged ? .action(.unstage(path), label: "Unstage changes") : .action(.stage(path), label: "Stage changes"), .action(.openDiff(path), label: "Open diff"), .action(.discard(path), label: "Discard changes"), ] } } /// The folder name of a direct child of `ontwikkelingen/`, except the `ontwikkeld` archive. private static func ontwikkelingName(_ path: String) -> String? { let parts = path.split(separator: "/", omittingEmptySubsequences: false) guard parts.count == 2, parts[0] == "ontwikkelingen", !parts[1].isEmpty, parts[1] != "ontwikkeld" else { return nil } return String(parts[1]) } private static func readAloudFile(_ path: String) -> [ContextMenuEntry] { FileType.isMarkdown(path) ? [.separator, .action(.readAloudFile(path), label: "Read aloud")] : [] } /// Selected Markdown is read as prose; a selection with no prose left, such as one code block, is read as it is. private static func readAloudSelection(_ code: String, path: String) -> [ContextMenuEntry] { let prose = FileType.isMarkdown(path) ? MarkdownSpeech.plainText(MarkdownParser.parse(code)) : "" return [.separator, .action(.readAloudText(prose.isEmpty ? code : prose), label: "Read aloud selection")] } private static func plak(_ code: String) -> [ContextMenuEntry] { [.separator, .action(.plak(code), label: "Plak")] } private static func fileHead(_ path: String) -> [ContextMenuEntry] { let name = PathUtils.basename(path) return [ .action(.passOnReference(path, code: nil), label: "Pass on reference"), .action(.copyReference(path), label: "Copy reference"), .action(.passOnFileName(name), label: "Pass on file name"), .action(.copyFileName(name), label: "Copy file name"), ] } }