chore: swiftformat drift + xcode 27 settings leftovers
Format-only pass over spikes, the gate prototype, and three app
files (brace style, wrapping — no semantic change), plus the 4-line
pbxproj alignment finishing the Xcode 27 settings pass (test-target
deployment target parity with 8e70f69).
[Generated by AI]
This commit is contained in:
1 parent
8e70f69945
commit
d722820fc8
17 files changed
+1910
-1791
No files matched your search
@@ -5,80 +5,88 @@
|
||||
import Foundation
|
||||
|
||||
struct Block: Identifiable, Equatable {
|
||||
var id = UUID()
|
||||
var raw: String // markdown source incl. marker; kind is derived, never stored
|
||||
var id = UUID()
|
||||
var raw: String // markdown source incl. marker; kind is derived, never stored
|
||||
}
|
||||
|
||||
nonisolated enum BlockModel {
|
||||
enum Kind { case heading1, heading2, paragraph, quote, hr }
|
||||
enum Kind { case heading1, heading2, paragraph, quote, hr }
|
||||
|
||||
/// Blank-line split — identical strategy to Spike B's readingBlocks, so the
|
||||
/// spikes differ by substrate, not by parse. ponytail: no AST, no lists/setext (corpus has none, #51).
|
||||
static func parse(_ md: String) -> [Block] {
|
||||
md.components(separatedBy: "\n\n")
|
||||
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||
.filter { !$0.isEmpty }
|
||||
.map { Block(raw: $0) }
|
||||
}
|
||||
/// Blank-line split — identical strategy to Spike B's readingBlocks, so the
|
||||
/// spikes differ by substrate, not by parse. ponytail: no AST, no lists/setext (corpus has none, #51).
|
||||
static func parse(_ md: String) -> [Block] {
|
||||
md.components(separatedBy: "\n\n")
|
||||
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
|
||||
.filter { !$0.isEmpty }
|
||||
.map { Block(raw: $0) }
|
||||
}
|
||||
|
||||
static func kind(of raw: String) -> Kind {
|
||||
if raw.hasPrefix("## ") { return .heading2 }
|
||||
if raw.hasPrefix("# ") { return .heading1 }
|
||||
if raw.hasPrefix("> ") { return .quote }
|
||||
if raw.count <= 6, !raw.isEmpty, raw.allSatisfy({ $0 == "*" || $0 == "-" || $0 == "_" || $0.isWhitespace }) { return .hr }
|
||||
return .paragraph
|
||||
}
|
||||
static func kind(of raw: String) -> Kind {
|
||||
if raw.hasPrefix("## ") {
|
||||
return .heading2
|
||||
}
|
||||
if raw.hasPrefix("# ") {
|
||||
return .heading1
|
||||
}
|
||||
if raw.hasPrefix("> ") {
|
||||
return .quote
|
||||
}
|
||||
if raw.count <= 6, !raw.isEmpty, raw.allSatisfy({ $0 == "*" || $0 == "-" || $0 == "_" || $0.isWhitespace }) {
|
||||
return .hr
|
||||
}
|
||||
return .paragraph
|
||||
}
|
||||
|
||||
/// Rendered text: marker stripped, inline markdown parsed (inline-only — block
|
||||
/// structure is ours, exactly like Spike B's reading mode).
|
||||
static func rendered(_ raw: String) -> AttributedString {
|
||||
var p = raw
|
||||
let k = kind(of: raw)
|
||||
switch k {
|
||||
case .heading1: p = String(p.dropFirst(2))
|
||||
case .heading2: p = String(p.dropFirst(3))
|
||||
case .quote: p = String(p.dropFirst(2))
|
||||
case .hr: return AttributedString("⁂")
|
||||
case .paragraph: break
|
||||
}
|
||||
var opts = AttributedString.MarkdownParsingOptions()
|
||||
opts.interpretedSyntax = .inlineOnlyPreservingWhitespace
|
||||
return (try? AttributedString(markdown: p, options: opts)) ?? AttributedString(p)
|
||||
}
|
||||
/// Rendered text: marker stripped, inline markdown parsed (inline-only — block
|
||||
/// structure is ours, exactly like Spike B's reading mode).
|
||||
static func rendered(_ raw: String) -> AttributedString {
|
||||
var p = raw
|
||||
let k = kind(of: raw)
|
||||
switch k {
|
||||
case .heading1: p = String(p.dropFirst(2))
|
||||
case .heading2: p = String(p.dropFirst(3))
|
||||
case .quote: p = String(p.dropFirst(2))
|
||||
case .hr: return AttributedString("⁂")
|
||||
case .paragraph: break
|
||||
}
|
||||
var opts = AttributedString.MarkdownParsingOptions()
|
||||
opts.interpretedSyntax = .inlineOnlyPreservingWhitespace
|
||||
return (try? AttributedString(markdown: p, options: opts)) ?? AttributedString(p)
|
||||
}
|
||||
|
||||
/// Marker-stripped plain text (anchor search runs over this).
|
||||
static func plainText(_ raw: String) -> String {
|
||||
switch kind(of: raw) {
|
||||
case .heading1: return String(raw.dropFirst(2))
|
||||
case .heading2: return String(raw.dropFirst(3))
|
||||
case .quote: return String(raw.dropFirst(2))
|
||||
case .hr: return ""
|
||||
case .paragraph: return raw
|
||||
}
|
||||
}
|
||||
/// Marker-stripped plain text (anchor search runs over this).
|
||||
static func plainText(_ raw: String) -> String {
|
||||
switch kind(of: raw) {
|
||||
case .heading1: String(raw.dropFirst(2))
|
||||
case .heading2: String(raw.dropFirst(3))
|
||||
case .quote: String(raw.dropFirst(2))
|
||||
case .hr: ""
|
||||
case .paragraph: raw
|
||||
}
|
||||
}
|
||||
|
||||
/// Within-block UTF-16 range of `quote` (word-tokenized, whitespace-tolerant — same
|
||||
/// strategy as Spike B's anchorRange, scoped to one block). nil = stale.
|
||||
static func anchorRange(of quote: String, in raw: String) -> NSRange? {
|
||||
let text = plainText(raw)
|
||||
let textWords = words(text), quoteWords = words(quote)
|
||||
guard !quoteWords.isEmpty, textWords.count >= quoteWords.count else { return nil }
|
||||
for start in 0 ... textWords.count - quoteWords.count {
|
||||
if (start ..< start + quoteWords.count).allSatisfy({ textWords[$0].norm == quoteWords[$0 - start].norm }) {
|
||||
let from = textWords[start].range.lowerBound
|
||||
let to = textWords[start + quoteWords.count - 1].range.upperBound
|
||||
return NSRange(location: text.utf16.distance(from: text.startIndex, to: from),
|
||||
length: text.utf16.distance(from: from, to: to))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
/// Within-block UTF-16 range of `quote` (word-tokenized, whitespace-tolerant — same
|
||||
/// strategy as Spike B's anchorRange, scoped to one block). nil = stale.
|
||||
static func anchorRange(of quote: String, in raw: String) -> NSRange? {
|
||||
let text = plainText(raw)
|
||||
let textWords = words(text), quoteWords = words(quote)
|
||||
guard !quoteWords.isEmpty, textWords.count >= quoteWords.count else { return nil }
|
||||
for start in 0 ... textWords.count - quoteWords.count {
|
||||
if (start ..< start + quoteWords.count).allSatisfy({ textWords[$0].norm == quoteWords[$0 - start].norm }) {
|
||||
let from = textWords[start].range.lowerBound
|
||||
let to = textWords[start + quoteWords.count - 1].range.upperBound
|
||||
return NSRange(location: text.utf16.distance(from: text.startIndex, to: from),
|
||||
length: text.utf16.distance(from: from, to: to))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
private struct Word { let norm: String; let range: Range<String.Index> }
|
||||
private static func words(_ s: String) -> [Word] {
|
||||
s.split(whereSeparator: \.isWhitespace).map { sub in
|
||||
Word(norm: sub.lowercased().trimmingCharacters(in: .punctuationCharacters),
|
||||
range: sub.startIndex ..< sub.endIndex)
|
||||
}
|
||||
}
|
||||
private struct Word { let norm: String; let range: Range<String.Index> }
|
||||
private static func words(_ s: String) -> [Word] {
|
||||
s.split(whereSeparator: \.isWhitespace).map { sub in
|
||||
Word(norm: sub.lowercased().trimmingCharacters(in: .punctuationCharacters),
|
||||
range: sub.startIndex ..< sub.endIndex)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user