Files
museion/Museion/PostsModelOutline.swift
T
AlexKucera 012f91727f
CI / ci (push) Canceled after 0s
refactor(postsmodel): split domain extensions (#146)
PostsModel.swift had outgrown its lint ceilings — 1127 lines against the
1000 file-length error, class body 361 against 350; both pre-dated the
week and #143 deepened them while naming this split as the follow-up.
The six domain extensions after the class move to their own files,
domain-grouped: PostsModelOutline.swift (mixed-content guard #111 +
Convert to Post #114), PostsModelPasses.swift (pass orchestration #24 +
Blind Read #122), PostsModelCards.swift (stored cards #23 +
apply/skip/done #25 + promote #26, with their support types). The
watcher section (startWatching + rescan) moves to a file-scope
extension so the class body drops to 323 — #111's precedent: same-file
extensions keep private state file-scoped while type_body_length only
counts the class body.

Pure moves, mechanically verified line-for-line: the only edits are the
18 private->internal widenings the cross-file moves force (13
private(set) setters + rescan/dismissExternal/wireSession/
isGuardedOutline/openUnguarded, each doc-noted) and the two
wrapped-condition guards unwrapped per the #142 learning, clause order
preserved. The error ceilings and both opening_brace warnings are gone;
the residual default WARNING-tier notices (708/400 file, 323/250
type-body) match the repo's standing posture — the ticket's own
~700-line landing zone makes the 400 tier unreachable.

Full suite 574/574 by xcresult executed count (573 unit + 1 UI smoke).

Closes #146
2026-11-03 12:44:13 +01:00

150 lines
5.7 KiB
Swift

//
// PostsModelOutline.swift
// Museion
//
// #146: the outline-domain extensions of PostsModel — the mixed-content
// guard (#111, S7) and Convert to Post (#114, S4) — moved from
// PostsModel.swift (file-length ceiling), code unchanged save the #114
// guard unwrap (#142 posture).
//
import Foundation
// MARK: Mixed-content outline guard (#111, S7)
extension PostsModel {
/// A `type: outline` piece that parses with dropped prose — the guard's
/// target. The old session already ended by the time this runs; its
/// flush was prose-mode, so it couldn't drop anything.
/// Type: the scan's word when the list knows it (archive pane opens,
/// app-start selection), the frontmatter's word during the rescan lag
/// (the same window lastDraftWasOutline covers) — ONE disk read derives
/// both. No watched home yet (pre-bind) = post: nothing to guard.
/// #146: internal — `select` (PostsModel.swift) asks the guard.
func isGuardedOutline(_ id: String, home: URL) -> Bool {
guard let dir = PostStore.folder(of: id, home: home),
let text = try? String(contentsOf: dir.appendingPathComponent("post.md"), encoding: .utf8)
else { return false }
let type: Post.ContentType = if let known = posts.first(where: { $0.id == id })?.type {
known
} else {
Post.ContentType(rawValue: Post.frontmatter(of: text)["type"] ?? "") ?? .post
}
return type == .outline && OutlineTree.parsesWithLoss(PostStore.splitFrontmatter(text).body)
}
/// #111 (S7): guard answers. Prose = safe open in prose mode (the
/// outliner never mounts, nothing drops); Outline = explicit consent,
/// opens the outliner (saves normalize/drop as today). Cancel = back
/// out (no session); the piece stays guarded on the next open.
func openGuardedOutlineInProse(home: URL) {
guard let id = outlineGuardPieceID else { return }
outlineGuardPieceID = nil
proseOverridePostID = id // routing: prose until the next open
openUnguarded(id, home: home)
}
func openGuardedOutlineAsOutline(home: URL) {
guard let id = outlineGuardPieceID else { return }
outlineGuardPieceID = nil
openUnguarded(id, home: home)
}
/// #111 (S7): the ask's Cancel — the held open dies (no session opens,
/// nothing stays selected over the empty editor).
func cancelOutlineGuard() {
outlineGuardPieceID = nil
if session == nil {
selectedPostID = nil
}
}
/// #111 (S7): the open with the guard passed (either answer or a clean
/// parse) — the plain select body. No re-check: the answer is the
/// explicit choice the ruling requires.
/// #146: internal — `select` (PostsModel.swift) routes its tail through
/// this one open body.
func openUnguarded(_ id: String, home: URL) {
dismissExternal()
if let session, session.postID != id {
session.teardown(flushFirst: true)
}
session = id == session?.postID ? session : EditorSession(postID: id, home: home)
draft = nil
pendingProject = nil
lastDraftWasOutline = false
outlineGuardPieceID = nil // the open displaces any held ask
selectedPostID = id
wireSession()
refreshCards(home: home) // #23: the newly opened piece's cards, now
}
}
// MARK: Convert to Post (#114, S4)
/// The staged ask: which piece, and whether the headings ladder would
/// drop prose lines (mixed content — the sheet warns, verbatim keeps
/// everything).
struct PendingConversion {
let id: String
let lossy: Bool
}
extension PostsModel {
/// #114 (S4): row-menu entry — stage the ask. Outline pieces only.
/// The open session's text is the truth when the piece is open — disk
/// lags the debounce, and a stale-clean read would drop the warning
/// for prose typed since the last flush (#114 audit).
func stageConversion(_ post: Post, home: URL) {
guard post.type == .outline else { return }
let body: String = if let session, session.postID == post.id {
session.text
} else {
PostStore.readBody(id: post.id, home: home) ?? ""
}
pendingConversion = PendingConversion(id: post.id, lossy: OutlineTree.parsesWithLoss(body))
}
/// #114 (S4): the ask's Cancel (binding dismissal).
func cancelConversion() {
pendingConversion = nil
}
/// #114 (S4): the answer. Flush-first when the piece is open (the
/// transform reads disk), flip `type` + transform the body once, then
/// reopen the session routed to prose — proseOverridePostID covers the
/// rescan lag (lastDraftWasOutline's machinery, reversed). A piece that
/// isn't open just converts; nothing else moves. One-way in v1.
func convertToPost(headingsLadder: Bool, home: URL) {
guard let pending = pendingConversion else { return }
pendingConversion = nil
guard flushOrAbort(pending.id) else { return }
do {
// The sheet's warning covered the STAGED body. If the
// answer-time body went lossy behind the ask (foreign write,
// late edit) and the writer was never warned, Headings would
// drop unconsented prose — abort, nothing converted (#114 audit).
if headingsLadder, !pending.lossy, let body = PostStore.readBody(id: pending.id, home: home), OutlineTree.parsesWithLoss(body) {
lastError = "The outline changed since the sheet opened — nothing was converted."
return
}
try PostStore.convert(id: pending.id, headingsLadder: headingsLadder, home: home)
if session?.postID == pending.id {
session?.teardown(flushFirst: false) // flushOrAbort already wrote
session = nil
proseOverridePostID = pending.id
openUnguarded(pending.id, home: home)
}
if outlineGuardPieceID == pending.id {
// Multi-window: the held #111 ask targets an outline that no
// longer is — dismiss it as a cancel (the ask's own teardown).
cancelOutlineGuard()
}
lastError = nil
Task { await rescan() }
} catch {
lastError = error.localizedDescription
}
}
}