Files

211 lines
9.8 KiB
Swift

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 file row in the Git view; a deleted file has nothing to open.
case gitRow(path: String, isDeleted: Bool)
/// A row in the Git view's Branches panel; `current` is the checked-out branch.
case branch(name: String, remote: Bool, current: String, isDirty: 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, .branch: ""
}
}
}
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)
/// Opens a file from the Git view, which closes first.
case openFile(String)
/// Opens a file from the Git view on its Diff, which closes first.
case openDiff(String)
/// Reverts unstaged changes: modified and deleted files come back from the index, new files are removed.
case gitDiscard([String])
/// The Git view's Clean up: every unstaged change reverted to the index, every untracked file and folder removed.
case gitCleanUp
/// Opens the Diff of the open file at this line.
case showDiff(String, line: Int)
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 <name>` into the agent composer for an ontwikkeling folder.
case bouwen(String)
/// Reads a Markdown file's prose aloud.
case readAloudFile(String)
/// Runs `tail -f` on a `.log` file in the Explorer's tail block, replacing any running tail.
case tailLog(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)
/// Merges this branch into the current one.
case mergeBranch(String)
/// Destructive items read in amber-deep.
public var isDestructive: Bool {
switch self {
case .gitDiscard, .gitCleanUp, .delete: true
default: false
}
}
/// The menu's main item, set in bold.
public var isEmphasized: Bool {
switch self {
case .openFile: true
default: false
}
}
}
/// A menu entry, or a separator between groups.
public enum ContextMenuEntry: Sendable, Equatable {
case action(ContextAction, label: String)
/// Shown dimmed with its `hint` as tooltip; the keyboard skips it and a click does nothing.
case disabled(ContextAction, label: String, hint: String)
case separator
/// The keyboard can land on it.
public var isSelectable: Bool {
if case .action = self { return true }
return false
}
}
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 let .branch(name, _, _, _): name
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) + tailLog(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?")] : []
let paste: [ContextMenuEntry] = kind == .tail ? [] : [.action(.pasteIntoTerminal(kind), label: "Paste")]
return (hasSelection ? [.action(.copyTerminalSelection(kind), label: "Copy")] : []) + paste + readAloud + waitWhat
case let .gitRow(path, isDeleted):
let open: [ContextMenuEntry] = isDeleted ? [] : [
.action(.openFile(path), label: "Open in editor"),
.action(.openDiff(path), label: "Open diff"),
.separator,
]
return open + [
.action(.passOnReference(path, code: nil), label: "Pass on reference"),
.action(.copyReference(path), label: "Copy reference"),
]
case let .branch(name, remote, current, isDirty):
if !remote && name == current { return [] }
let label = "Merge into \(current)"
return [isDirty
? .disabled(.mergeBranch(name), label: label, hint: "Commit or discard your changes first")
: .action(.mergeBranch(name), label: label)]
}
}
/// 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")] : []
}
private static func tailLog(_ path: String) -> [ContextMenuEntry] {
(path as NSString).pathExtension.lowercased() == "log" ? [.separator, .action(.tailLog(path), label: "Tail log")] : []
}
/// 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"),
]
}
}