127 lines
5.0 KiB
Swift
127 lines
5.0 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.
|
|
case editor(reference: AgentReference, code: String?)
|
|
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)
|
|
case discard(String)
|
|
case rename(String, isDirectory: Bool)
|
|
case revealInFinder(String)
|
|
case delete(String, isDirectory: Bool)
|
|
case copyTerminalSelection(TerminalKind)
|
|
case pasteIntoTerminal(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 reference, or `project/` for the root.
|
|
public static func header(for target: ContextTarget, projectName: String) -> String {
|
|
switch target {
|
|
case let .editor(reference, _): reference.description
|
|
case let .directory(path): path + "/"
|
|
case let .file(path), let .gitRow(path, _): path
|
|
case .projectRoot: projectName + "/"
|
|
case .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):
|
|
let ref = reference.description
|
|
let hasCode = !(code ?? "").isEmpty
|
|
return [
|
|
.action(.passOnReference(ref, code: hasCode ? code : nil), label: hasCode ? "Pass on selection" : "Pass on reference"),
|
|
.action(.copyReference(ref), label: "Copy reference"),
|
|
]
|
|
case .projectRoot:
|
|
return [
|
|
.action(.newFile(inDirectory: ""), label: "New file"),
|
|
.action(.revealInFinder(""), label: "Show in Finder"),
|
|
]
|
|
case let .file(path):
|
|
return fileHead(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 + "/"
|
|
return [
|
|
.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):
|
|
return (hasSelection ? [.action(.copyTerminalSelection(kind), label: "Copy")] : [])
|
|
+ [.action(.pasteIntoTerminal(kind), label: "Paste")]
|
|
case let .gitRow(path, staged):
|
|
return fileHead(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"),
|
|
]
|
|
}
|
|
}
|
|
|
|
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"),
|
|
]
|
|
}
|
|
}
|