before the great git rework
This commit is contained in:
@@ -1,66 +0,0 @@
|
||||
import Foundation
|
||||
import HelderServices
|
||||
import Observation
|
||||
|
||||
@Observable
|
||||
@MainActor
|
||||
public final class NoteStore {
|
||||
public static let fileName = NotesService.fileName
|
||||
|
||||
public var text = ""
|
||||
/// What is on disk, so a save without edits does not touch the file and wake the watcher.
|
||||
public private(set) var savedText = ""
|
||||
|
||||
/// Raises a toast; the model wires it to `ToastStore`.
|
||||
@ObservationIgnored public var notify: ((String, String?) -> Void)?
|
||||
@ObservationIgnored private let notes: NotesServicing
|
||||
|
||||
public init(notes: NotesServicing) {
|
||||
self.notes = notes
|
||||
}
|
||||
|
||||
public var isDirty: Bool { text != savedText }
|
||||
|
||||
public var characterCount: Int { text.count }
|
||||
|
||||
public func load() async {
|
||||
let loaded = await notes.read()
|
||||
text = loaded
|
||||
savedText = loaded
|
||||
}
|
||||
|
||||
/// Picks up an outside edit of the note file; unsaved typing always wins.
|
||||
public func reloadIfClean() async {
|
||||
guard !isDirty else { return }
|
||||
let loaded = await notes.read()
|
||||
guard !isDirty, loaded != savedText else { return }
|
||||
text = loaded
|
||||
savedText = loaded
|
||||
}
|
||||
|
||||
/// Saves when the text changed; called on window blur, on close and when the overlay closes.
|
||||
/// Returns false when the write failed.
|
||||
@discardableResult
|
||||
public func save() async -> Bool {
|
||||
guard isDirty else { return true }
|
||||
let snapshot = text
|
||||
// Marked saved before the write, so a second blur during the write does not write again.
|
||||
let previous = savedText
|
||||
savedText = snapshot
|
||||
do {
|
||||
try await notes.write(snapshot)
|
||||
return true
|
||||
} catch {
|
||||
if savedText == snapshot { savedText = previous }
|
||||
Log.error("notes", "save failed: \(error.localizedDescription)")
|
||||
notify?("Save failed", Self.fileName)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/// The trimmed note for ⌘P, or nil when it is empty.
|
||||
public var passableText: String? {
|
||||
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
return trimmed.isEmpty ? nil : trimmed
|
||||
}
|
||||
}
|
||||
@@ -88,8 +88,6 @@ public enum Overlay: Equatable {
|
||||
case search(seed: String)
|
||||
/// `?`: the keyboard reference.
|
||||
case shortcuts
|
||||
/// ⌘N: the project note.
|
||||
case note
|
||||
/// Text the agent wrote to `.clean-copy`, to edit and copy.
|
||||
case cleanCopy
|
||||
case diff(DiffOverlayRequest)
|
||||
@@ -110,7 +108,7 @@ public enum Overlay: Equatable {
|
||||
public final class OverlayStore {
|
||||
public private(set) var current = Overlay.none
|
||||
/// Called with the old and new overlay on every change, however it happened, so closing
|
||||
/// behaviour (the note saves, the Diff restores the mode) runs even when another overlay takes over.
|
||||
/// behaviour (the Diff restores the mode) runs even when another overlay takes over.
|
||||
@ObservationIgnored public var onChange: ((_ old: Overlay, _ new: Overlay) -> Void)?
|
||||
|
||||
public init() {}
|
||||
|
||||
@@ -8,8 +8,6 @@ extension WorkspaceModel {
|
||||
|
||||
func overlayChanged(from old: Overlay, to new: Overlay) {
|
||||
switch old {
|
||||
case .note:
|
||||
if let note { track { await note.save() } }
|
||||
case .diff:
|
||||
editor?.closeDiff()
|
||||
default:
|
||||
@@ -103,7 +101,7 @@ extension WorkspaceModel {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Editor and the note
|
||||
// MARK: Editor
|
||||
|
||||
func passSelection(_ info: TextSelectionInfo?) {
|
||||
guard let editor, let path = editor.activePath else { return }
|
||||
@@ -115,14 +113,6 @@ extension WorkspaceModel {
|
||||
}
|
||||
}
|
||||
|
||||
/// ⌘P with the note open: the whole note goes to the agent, and the note closes so the text is seen landing.
|
||||
func passNote() {
|
||||
guard let note, let text = note.passableText else { return }
|
||||
terminal.insertIntoAgent(text: text)
|
||||
overlay.dismiss()
|
||||
toasts.show("Note passed to agent", detail: NoteStore.fileName)
|
||||
}
|
||||
|
||||
func editorSelectionFirstLine() -> String {
|
||||
guard let editor, let path = editor.activePath, let selection = editor.selection, selection.path == path else { return "" }
|
||||
let lines = editor.text(for: path).split(separator: "\n", omittingEmptySubsequences: false)
|
||||
|
||||
@@ -17,11 +17,10 @@ extension WorkspaceModel {
|
||||
isReady = true
|
||||
}
|
||||
|
||||
/// Asks about every unsaved file, then writes the note and the session.
|
||||
/// Asks about every unsaved file, then writes the session.
|
||||
/// False when the user cancelled: the window or project must stay.
|
||||
public func prepareToClose() async -> Bool {
|
||||
if let editor, !(await editor.resolveUnsaved()) { return false }
|
||||
await note?.save()
|
||||
await editor?.persist()
|
||||
return true
|
||||
}
|
||||
@@ -66,9 +65,6 @@ extension WorkspaceModel {
|
||||
wire(editor, symbols: services.symbols)
|
||||
self.editor = editor
|
||||
|
||||
let note = NoteStore(notes: services.notes)
|
||||
note.notify = notify
|
||||
self.note = note
|
||||
cleanCopy = CleanCopyStore(files: services.files)
|
||||
terminal = TerminalStore(projectRoot: root.path)
|
||||
wireTerminal()
|
||||
@@ -101,12 +97,11 @@ 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, cleanCopy = cleanCopy
|
||||
let sourceControl = sourceControl, explorer = explorer, editor = editor, cleanCopy = cleanCopy
|
||||
async let git: Void = sourceControl?.refresh() ?? ()
|
||||
async let tree: Void = explorer?.reload() ?? ()
|
||||
async let loaded: Void = note?.load() ?? ()
|
||||
async let baseline: Void = cleanCopy?.loadBaseline() ?? ()
|
||||
_ = await (git, tree, loaded, baseline)
|
||||
_ = await (git, tree, baseline)
|
||||
await editor?.restore(restoreSession: config.session.restoreOnLaunch)
|
||||
guard generation == current else { return }
|
||||
startWatcher(root)
|
||||
@@ -115,7 +110,7 @@ extension WorkspaceModel {
|
||||
}
|
||||
|
||||
/// Stops the watchers and polling, waits for the work still running against this project,
|
||||
/// then stops reading aloud, saves the note and the session and ends the terminals.
|
||||
/// then stops reading aloud, saves the session and ends the terminals.
|
||||
func closeProject() async {
|
||||
watcher?.stop()
|
||||
watcher = nil
|
||||
@@ -134,7 +129,6 @@ extension WorkspaceModel {
|
||||
for task in running { await task.value }
|
||||
readAloud.stop()
|
||||
await editor?.flushAutoSaves()
|
||||
await note?.save()
|
||||
await editor?.persist()
|
||||
terminal.terminateAll()
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ extension WorkspaceModel {
|
||||
/// Reacts to a disk change from the watcher: project and git events gather into one debounced refresh.
|
||||
public func handle(_ event: WatchEvent) async {
|
||||
switch event {
|
||||
case .project: scheduleRefresh([.git, .tree, .symbols, .openFiles, .note])
|
||||
case .project: scheduleRefresh([.git, .tree, .symbols, .openFiles])
|
||||
case .git: scheduleRefresh(.git)
|
||||
case .config: await reloadConfig()
|
||||
case .cleanCopy: await cleanCopyChanged()
|
||||
@@ -61,7 +61,7 @@ extension WorkspaceModel {
|
||||
|
||||
/// Reads the stores once up front, so a pass that outlives a project switch only touches the old project.
|
||||
func run(_ scope: RefreshScope) async {
|
||||
let sourceControl = sourceControl, explorer = explorer, editor = editor, note = note
|
||||
let sourceControl = sourceControl, explorer = explorer, editor = editor
|
||||
let current = generation
|
||||
if scope.contains(.symbols) {
|
||||
await services?.symbols.invalidate()
|
||||
@@ -70,8 +70,7 @@ extension WorkspaceModel {
|
||||
async let git: Void = scope.contains(.git) ? (sourceControl?.refresh() ?? ()) : ()
|
||||
async let tree: Void = scope.contains(.tree) ? (explorer?.reload() ?? ()) : ()
|
||||
async let files: Void = scope.contains(.openFiles) ? (editor?.filesChanged(nil) ?? ()) : ()
|
||||
async let notes: Void = scope.contains(.note) ? (note?.reloadIfClean() ?? ()) : ()
|
||||
_ = await (git, tree, files, notes)
|
||||
_ = await (git, tree, files)
|
||||
}
|
||||
|
||||
/// Restarts the countdown on every change, so a burst of writes loads the class list once.
|
||||
|
||||
@@ -41,8 +41,6 @@ public struct RefreshScope: OptionSet, Sendable {
|
||||
public static let symbols = RefreshScope(rawValue: 4)
|
||||
/// The open file and every unsaved buffer pick up the disk text; unsaved edits are kept.
|
||||
public static let openFiles = RefreshScope(rawValue: 8)
|
||||
/// The project note picks up an outside edit when it has no unsaved typing.
|
||||
public static let note = RefreshScope(rawValue: 16)
|
||||
}
|
||||
|
||||
/// A list row whose on-screen frame the ⌘→ menu anchors to.
|
||||
@@ -84,7 +82,6 @@ public final class WorkspaceModel {
|
||||
public internal(set) var explorer: ExplorerStore?
|
||||
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
|
||||
@@ -122,7 +119,7 @@ public final class WorkspaceModel {
|
||||
if let root = self.root { attach(root) }
|
||||
}
|
||||
|
||||
/// Loads the recents and, with a project, its config, git, tree, note and session; then starts the watchers.
|
||||
/// Loads the recents and, with a project, its config, git, tree and session; then starts the watchers.
|
||||
public func start() async {
|
||||
guard !hasStarted else { return }
|
||||
hasStarted = true
|
||||
@@ -136,7 +133,6 @@ public final class WorkspaceModel {
|
||||
public func perform(_ command: Command) {
|
||||
switch command {
|
||||
case let .showSearch(seed): overlay.present(.search(seed: seed.isEmpty ? editorSelectionFirstLine() : seed))
|
||||
case .showNote: overlay.present(.note)
|
||||
case .showShortcuts: overlay.present(.shortcuts)
|
||||
case .showRecentProjects: showRecentProjects()
|
||||
case let .showRecentFiles(index): overlay.present(.recentFiles(initialSelection: index))
|
||||
@@ -188,7 +184,6 @@ public final class WorkspaceModel {
|
||||
layout.focus(.sourceControl)
|
||||
focusRequest = FocusRequest(.commitMessage)
|
||||
|
||||
case .passNote: passNote()
|
||||
case .copyCleanCopy: copyCleanCopy()
|
||||
|
||||
case .stopReading: readAloud.stop()
|
||||
@@ -251,11 +246,9 @@ public final class WorkspaceModel {
|
||||
rowFrames[key] = frame
|
||||
}
|
||||
|
||||
/// Saves the note when the window loses focus.
|
||||
/// Remembers the window lost focus, so coming back can flash branch · project.
|
||||
public func windowDidResignKey() {
|
||||
resignedKey = true
|
||||
guard let note else { return }
|
||||
track { await note.save() }
|
||||
}
|
||||
|
||||
/// Coming back to a window flashes branch · project, so the right window is obvious; launch never does.
|
||||
|
||||
@@ -9,17 +9,15 @@ public struct WorkspaceServices {
|
||||
public var search: SearchServicing
|
||||
public var symbols: SymbolServicing
|
||||
public var config: ConfigServicing
|
||||
public var notes: NotesServicing
|
||||
public var recentFiles: RecentFilesStoring
|
||||
|
||||
public init(git: GitServicing, files: FileServicing, search: SearchServicing, symbols: SymbolServicing,
|
||||
config: ConfigServicing, notes: NotesServicing, recentFiles: RecentFilesStoring) {
|
||||
config: ConfigServicing, recentFiles: RecentFilesStoring) {
|
||||
self.git = git
|
||||
self.files = files
|
||||
self.search = search
|
||||
self.symbols = symbols
|
||||
self.config = config
|
||||
self.notes = notes
|
||||
self.recentFiles = recentFiles
|
||||
}
|
||||
|
||||
@@ -32,7 +30,6 @@ public struct WorkspaceServices {
|
||||
search: search,
|
||||
symbols: SymbolService(root: root, config: config),
|
||||
config: config,
|
||||
notes: NotesService(root: root),
|
||||
recentFiles: RecentFilesStore(root: root)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user