adds Clean Copy Support
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
import HelderServices
|
||||
import Observation
|
||||
|
||||
/// The clean-copy popup: text the agent wrote to `.clean-copy`, editable before it is copied.
|
||||
@Observable
|
||||
@MainActor
|
||||
public final class CleanCopyStore {
|
||||
public static let fileName = CleanCopy.fileName
|
||||
|
||||
/// The popup's text, with the user's edits.
|
||||
public var text = ""
|
||||
/// The text as it arrived, so an edit can be told apart.
|
||||
public private(set) var received = ""
|
||||
/// The newest text waiting for the popup, while another overlay is open.
|
||||
public private(set) var pending: String?
|
||||
|
||||
/// The file's last contents Helder saw; nil before the first read or while the file is missing.
|
||||
@ObservationIgnored private var lastSeen: String?
|
||||
@ObservationIgnored private let files: FileServicing
|
||||
|
||||
public init(files: FileServicing) {
|
||||
self.files = files
|
||||
}
|
||||
|
||||
public var isEdited: Bool { text != received }
|
||||
|
||||
public var characterCount: Int { text.count }
|
||||
|
||||
/// Project open: the file's current text is the baseline and never shows.
|
||||
public func loadBaseline() async {
|
||||
lastSeen = await read()
|
||||
}
|
||||
|
||||
/// The file changed: returns true when it holds new text, which then waits in `pending`.
|
||||
@discardableResult
|
||||
public func fileChanged() async -> Bool {
|
||||
guard let contents = await read() else { return false }
|
||||
let isNew = CleanCopy.isNew(contents, lastSeen: lastSeen)
|
||||
lastSeen = contents
|
||||
if isNew { pending = contents }
|
||||
return isNew
|
||||
}
|
||||
|
||||
/// Moves the pending text into the popup that is about to open.
|
||||
public func takePending() {
|
||||
guard let pending else { return }
|
||||
text = pending
|
||||
received = pending
|
||||
self.pending = nil
|
||||
}
|
||||
|
||||
/// New text while the popup is open replaces it only when the user has not edited it.
|
||||
public func refreshOpenPopup() {
|
||||
if !isEdited { takePending() }
|
||||
pending = nil
|
||||
}
|
||||
|
||||
private func read() async -> String? {
|
||||
try? await files.read(Self.fileName)
|
||||
}
|
||||
}
|
||||
@@ -90,6 +90,8 @@ public enum Overlay: Equatable {
|
||||
case shortcuts
|
||||
/// ⌘N: the project note.
|
||||
case note
|
||||
/// Text the agent wrote to `.clean-copy`, to edit and copy.
|
||||
case cleanCopy
|
||||
case diff(DiffOverlayRequest)
|
||||
/// ⇧⌘O: the other recent projects. The bare window's launcher is not an overlay.
|
||||
case launcher
|
||||
|
||||
@@ -15,6 +15,39 @@ extension WorkspaceModel {
|
||||
default:
|
||||
break
|
||||
}
|
||||
if new == .none, cleanCopy?.pending != nil {
|
||||
Task { @MainActor [weak self] in self?.showCleanCopy() }
|
||||
}
|
||||
}
|
||||
|
||||
/// `.clean-copy` changed on disk: new text opens the popup, or waits for the open overlay to close.
|
||||
func cleanCopyChanged() async {
|
||||
guard let cleanCopy else { return }
|
||||
let current = generation
|
||||
guard await cleanCopy.fileChanged(), generation == current, self.cleanCopy === cleanCopy else { return }
|
||||
showCleanCopy()
|
||||
}
|
||||
|
||||
/// Shows the pending clean-copy text when nothing else floats over the workspace.
|
||||
func showCleanCopy() {
|
||||
guard let cleanCopy, cleanCopy.pending != nil else { return }
|
||||
switch overlay.current {
|
||||
case .none:
|
||||
cleanCopy.takePending()
|
||||
overlay.present(.cleanCopy)
|
||||
case .cleanCopy:
|
||||
cleanCopy.refreshOpenPopup()
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
/// Copies the popup's text as plain text, closes it and confirms with a toast.
|
||||
func copyCleanCopy() {
|
||||
guard let cleanCopy, overlay.current == .cleanCopy else { return }
|
||||
app.system.copy(cleanCopy.text)
|
||||
overlay.dismiss()
|
||||
toasts.show("Copied", detail: "\(cleanCopy.characterCount) characters")
|
||||
}
|
||||
|
||||
/// The recent projects other than this one, for ⇧⌘O.
|
||||
|
||||
@@ -69,6 +69,7 @@ extension WorkspaceModel {
|
||||
let note = NoteStore(notes: services.notes)
|
||||
note.notify = notify
|
||||
self.note = note
|
||||
cleanCopy = CleanCopyStore(files: services.files)
|
||||
terminal = TerminalStore(projectRoot: root.path)
|
||||
wireTerminal()
|
||||
launcher = LauncherList(recents: recentProjects, excluding: root.path)
|
||||
@@ -100,11 +101,12 @@ extension WorkspaceModel {
|
||||
recentProjects = await app.recentProjects.list()
|
||||
guard generation == current else { return }
|
||||
launcher = LauncherList(recents: recentProjects, excluding: root.path)
|
||||
let sourceControl = sourceControl, explorer = explorer, note = note, editor = editor
|
||||
let sourceControl = sourceControl, explorer = explorer, note = note, editor = editor, cleanCopy = cleanCopy
|
||||
async let git: Void = sourceControl?.refresh() ?? ()
|
||||
async let tree: Void = explorer?.reload() ?? ()
|
||||
async let loaded: Void = note?.load() ?? ()
|
||||
_ = await (git, tree, loaded)
|
||||
async let baseline: Void = cleanCopy?.loadBaseline() ?? ()
|
||||
_ = await (git, tree, loaded, baseline)
|
||||
await editor?.restore(restoreSession: config.session.restoreOnLaunch)
|
||||
guard generation == current else { return }
|
||||
startWatcher(root)
|
||||
|
||||
@@ -18,6 +18,7 @@ extension WorkspaceModel {
|
||||
case .project: scheduleRefresh([.git, .tree, .symbols, .openFiles, .note])
|
||||
case .git: scheduleRefresh(.git)
|
||||
case .config: await reloadConfig()
|
||||
case .cleanCopy: await cleanCopyChanged()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -84,6 +84,7 @@ public final class WorkspaceModel {
|
||||
public internal(set) var editor: EditorStore?
|
||||
public internal(set) var terminal: TerminalStore
|
||||
public internal(set) var note: NoteStore?
|
||||
public internal(set) var cleanCopy: CleanCopyStore?
|
||||
|
||||
@ObservationIgnored public let app: AppServices
|
||||
@ObservationIgnored public internal(set) var services: WorkspaceServices?
|
||||
@@ -186,6 +187,7 @@ public final class WorkspaceModel {
|
||||
focusRequest = FocusRequest(.commitMessage)
|
||||
|
||||
case .passNote: passNote()
|
||||
case .copyCleanCopy: copyCleanCopy()
|
||||
|
||||
case .openFolder:
|
||||
switchProject {
|
||||
|
||||
Reference in New Issue
Block a user