fix(editor): audit fixes — fence staleness, font caching, lint shrink (#77)
- HIGH: fence delimiter edits now restyle the whole document (delimiter count fingerprint in the coordinator; count moved = full re-attribute, caret preserved) — typing/deleting a ``` flipped every line until the match before - derived fonts + measureWidth cached as nonisolated(unsafe) static let (were NSFontManager.convert per line per pass) - h2/h3/h4 line height calibrated from their own face's metrics - 7+ hashes fall to body (CommonMark cap); tab-indented heading marker dims the hash, not the tab - intraword _ spans unclaim on reject so the fallback char-dim takes them - shrink: fenceState reuses contentLines+classify (isDelim deleted), marker regexes as literals (helper + 2 lint disables deleted), charBefore/After via NSString, dead colors + forwarders deleted - vacuous retrofit test replaced with a real one + fenceDelimCount test
This commit is contained in:
1 parent
fefdd9a6fa
commit
1f7557a2ed
4 files changed
+102
-99
No files matched your search
@@ -38,10 +38,10 @@ nonisolated enum ProsePresenter {
|
||||
classify(line, inFence: false)
|
||||
}
|
||||
|
||||
/// Delimiter lines are fences even inside a fence (they close it) — so the
|
||||
/// delim test runs before the inFence shortcut and `applyStyles` toggles
|
||||
/// on `kind == .fence` instead of re-deriving the prefix.
|
||||
static func classify(_ line: String, inFence: Bool) -> LineKind {
|
||||
if inFence {
|
||||
return .fenceInterior
|
||||
}
|
||||
let trimmed = line.drop(while: { $0 == " " || $0 == "\t" })
|
||||
if trimmed.isEmpty {
|
||||
return .blank
|
||||
@@ -49,8 +49,11 @@ nonisolated enum ProsePresenter {
|
||||
if trimmed.hasPrefix("```") || trimmed.hasPrefix("~~~") {
|
||||
return .fence
|
||||
}
|
||||
if inFence {
|
||||
return .fenceInterior
|
||||
}
|
||||
let hashes = trimmed.prefix(while: { $0 == "#" }).count
|
||||
if hashes > 0 {
|
||||
if hashes > 0, hashes <= 6 { // CommonMark caps at 6; 7+ is body
|
||||
let after = trimmed.dropFirst(hashes)
|
||||
if after.hasPrefix(" ") || after.isEmpty {
|
||||
switch hashes {
|
||||
@@ -67,7 +70,7 @@ nonisolated enum ProsePresenter {
|
||||
if trimmed.allSatisfy({ $0 == "*" || $0 == " " || $0 == "-" || $0 == "_" }) {
|
||||
return .syntaxOnly
|
||||
}
|
||||
if listMarkerRegex.firstMatch(in: line, range: NSRange(location: 0, length: (line as NSString).length)) != nil {
|
||||
if line.firstMatch(of: listMarkerPattern) != nil {
|
||||
return .listMarker
|
||||
}
|
||||
return .body
|
||||
@@ -85,10 +88,27 @@ nonisolated enum ProsePresenter {
|
||||
|
||||
/// Attribute one paragraph (editing-time re-apply). Cheap: the paragraph's
|
||||
/// lines plus a fence-state scan of the prefix.
|
||||
/// NOTE fence state crosses paragraphs: the coordinator (PostEditorView)
|
||||
/// compares the delimiter count per edit and routes to a full-document
|
||||
/// `attribute` pass when it moved — typing/deleting a ``` flips every line
|
||||
/// until the matching delimiter.
|
||||
static func reattribute(_ out: NSMutableAttributedString, paragraphRange: NSRange) {
|
||||
applyStyles(out, range: paragraphRange)
|
||||
}
|
||||
|
||||
/// Number of fence delimiter lines (``` or ~~~ prefixes) — the cheap
|
||||
/// fingerprint the coordinator compares per edit.
|
||||
static func fenceDelimCount(_ text: String) -> Int {
|
||||
var count = 0
|
||||
for line in text.split(separator: "\n", omittingEmptySubsequences: false) {
|
||||
let trimmed = line.drop(while: { $0 == " " || $0 == "\t" })
|
||||
if trimmed.hasPrefix("```") || trimmed.hasPrefix("~~~") {
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// MARK: Styling pass
|
||||
|
||||
private static func applyStyles(_ out: NSMutableAttributedString, range: NSRange) {
|
||||
@@ -99,10 +119,8 @@ nonisolated enum ProsePresenter {
|
||||
inFence = fenceState(nsText, upTo: range.location)
|
||||
}
|
||||
for line in contentLines(nsText, in: range) {
|
||||
let trimmed = line.text.drop(while: { $0 == " " || $0 == "\t" })
|
||||
let isDelim = !line.text.isEmpty && !trimmed.isEmpty && (trimmed.hasPrefix("```") || trimmed.hasPrefix("~~~"))
|
||||
let kind = isDelim ? LineKind.fence : classify(line.text, inFence: inFence)
|
||||
if isDelim {
|
||||
let kind = classify(line.text, inFence: inFence)
|
||||
if kind == .fence {
|
||||
inFence.toggle()
|
||||
}
|
||||
styleLine(out, line: line, kind: kind)
|
||||
@@ -145,26 +163,10 @@ nonisolated enum ProsePresenter {
|
||||
/// Count unclosed fences before `end` (``` or ~~~ delim lines toggle).
|
||||
private static func fenceState(_ nsText: NSString, upTo end: Int) -> Bool {
|
||||
var inFence = false
|
||||
var loc = 0
|
||||
while loc < end {
|
||||
let full = nsText.lineRange(for: NSRange(location: loc, length: 0))
|
||||
var len = full.length
|
||||
while len > 0 {
|
||||
let char = nsText.character(at: full.location + len - 1)
|
||||
if char == 0x0A || char == 0x0D {
|
||||
len -= 1
|
||||
} else {
|
||||
break
|
||||
}
|
||||
for line in contentLines(nsText, in: NSRange(location: 0, length: min(end, nsText.length))) {
|
||||
if classify(line.text, inFence: inFence) == .fence {
|
||||
inFence.toggle()
|
||||
}
|
||||
if len > 0 {
|
||||
let line = (nsText.substring(with: NSRange(location: full.location, length: len)) as String)
|
||||
.drop(while: { $0 == " " || $0 == "\t" })
|
||||
if line.hasPrefix("```") || line.hasPrefix("~~~") {
|
||||
inFence.toggle()
|
||||
}
|
||||
}
|
||||
loc = NSMaxRange(full)
|
||||
}
|
||||
return inFence
|
||||
}
|
||||
@@ -175,24 +177,26 @@ nonisolated enum ProsePresenter {
|
||||
let dim = ProseTypography.codeAttributes // codeFont + syntaxColor
|
||||
switch kind {
|
||||
case .heading1, .heading2, .heading3, .heading4Plus:
|
||||
let lead = line.text.prefix(while: { $0 == " " }).utf16.count
|
||||
let lead = line.text.prefix(while: { $0 == " " || $0 == "\t" }).utf16.count
|
||||
let hashes = line.text.dropFirst(lead).prefix(while: { $0 == "#" }).utf16.count
|
||||
let marker = NSRange(location: line.content.location + lead, length: min(hashes + 1, line.content.length - lead))
|
||||
if marker.length > 0 {
|
||||
out.addAttributes(dim, range: marker)
|
||||
}
|
||||
case .quote:
|
||||
if let marker = markerRange(quoteMarkerRegex, in: line.text), marker.length > 0 {
|
||||
if let marker = markerRangeUTF16(quoteMarkerPattern, in: line.text), marker.length > 0 {
|
||||
out.addAttributes(dim, range: shifted(marker, into: line.content))
|
||||
}
|
||||
case .listMarker:
|
||||
if let marker = markerRange(listMarkerRegex, in: line.text), marker.length > 0 {
|
||||
if let marker = markerRangeUTF16(listMarkerPattern, in: line.text), marker.length > 0 {
|
||||
out.addAttributes(dim, range: shifted(marker, into: line.content))
|
||||
}
|
||||
case .syntaxOnly, .fence:
|
||||
case .syntaxOnly:
|
||||
if line.content.length > 0 {
|
||||
out.addAttributes(dim, range: line.content)
|
||||
}
|
||||
case .fence:
|
||||
break // attributes(for: .fence) already carries codeFont + syntaxColor
|
||||
case .fenceInterior, .blank, .body:
|
||||
break
|
||||
}
|
||||
@@ -325,14 +329,6 @@ nonisolated enum ProsePresenter {
|
||||
|
||||
// MARK: Base attributes
|
||||
|
||||
static var bodyFont: NSFont {
|
||||
ProseTypography.bodyFont
|
||||
}
|
||||
|
||||
static var bodyAttributes: [NSAttributedString.Key: Any] {
|
||||
ProseTypography.bodyAttributes
|
||||
}
|
||||
|
||||
static func attributes(for kind: LineKind) -> [NSAttributedString.Key: Any] {
|
||||
switch kind {
|
||||
case .heading1:
|
||||
@@ -408,16 +404,15 @@ nonisolated enum ProsePresenter {
|
||||
|
||||
// MARK: Pattern helpers
|
||||
|
||||
/// Marker dimming for quote / list lines — returns the marker's NSRange in
|
||||
/// `line.text`, or nil.
|
||||
// Static literal patterns — compile by construction.
|
||||
// swiftlint:disable:next force_try
|
||||
private static let listMarkerRegex = try! NSRegularExpression(pattern: #"^\s*(?:[-*+]|\d{1,9}[.)])(?:\s+|$)"#)
|
||||
// swiftlint:disable:next force_try
|
||||
private static let quoteMarkerRegex = try! NSRegularExpression(pattern: #"^(?:\s*>(?:\s|$))*"#)
|
||||
// Marker patterns (regex literals, like the inline ones below).
|
||||
private nonisolated(unsafe) static let listMarkerPattern = /^\s*(?:[-*+]|\d{1,9}[.)])(?:\s+|$)/
|
||||
private nonisolated(unsafe) static let quoteMarkerPattern = /^(?:\s*>(?:\s|$))*/
|
||||
|
||||
private static func markerRange(_ regex: NSRegularExpression, in lineText: String) -> NSRange? {
|
||||
regex.firstMatch(in: lineText, range: NSRange(location: 0, length: (lineText as NSString).length))?.range
|
||||
/// UTF-16 NSRange of the marker in `lineText`, or nil.
|
||||
private static func markerRangeUTF16(_ pattern: Regex<Substring>, in lineText: String) -> NSRange? {
|
||||
guard let match = lineText.firstMatch(of: pattern) else { return nil }
|
||||
let utf16 = match.range.utf16Range(in: lineText)
|
||||
return NSRange(location: utf16.lowerBound, length: utf16.count)
|
||||
}
|
||||
|
||||
/// Mark-boundary helpers (no lookbehind in swift-regex): the Character
|
||||
|
||||
Reference in new issue
Block a user