From 0559f4772ed341f77f5d2e26c0b6727a4f5c3019 Mon Sep 17 00:00:00 2001 From: v0 Date: Sat, 7 Nov 2026 20:33:08 +0000 Subject: [PATCH 1/3] Share AST and source-range parsing and preserve Unicode math positions --- .../MarkdownParser+MathContext.swift | 55 +++++++++++++++- .../MarkdownParser/MarkdownParser.swift | 60 ++++++++++++++---- .../MarkdownTextView+Private.swift | 24 +++++-- .../IncrementalParsingTests.swift | 63 +++++++++++++++++++ .../MarkdownViewTests/PerformanceTests.swift | 15 +++++ .../StreamingFastPathTests.swift | 19 ++++++ 6 files changed, 217 insertions(+), 19 deletions(-) diff --git a/Sources/MarkdownParser/MarkdownParser/MarkdownParser+MathContext.swift b/Sources/MarkdownParser/MarkdownParser/MarkdownParser+MathContext.swift index c8c4265..79ac17a 100644 --- a/Sources/MarkdownParser/MarkdownParser/MarkdownParser+MathContext.swift +++ b/Sources/MarkdownParser/MarkdownParser/MarkdownParser+MathContext.swift @@ -34,6 +34,41 @@ public extension MarkdownParser { let document: String var indexedContent: String? + private struct Replacement { + let original: Range + let indexed: Range + } + private var replacements: [Replacement] = [] + + func originalIndex(for index: String.Index, in source: String, isEnd: Bool) -> String.Index { + let offset = source.utf8.distance(from: source.startIndex, to: index) + var lower = 0 + var upper = replacements.count + while lower < upper { + let middle = lower + (upper - lower) / 2 + if replacements[middle].indexed.lowerBound <= offset { + lower = middle + 1 + } else { + upper = middle + } + } + var originalOffset = offset + if lower > 0 { + let replacement = replacements[lower - 1] + if replacement.indexed.contains(offset) { + originalOffset = isEnd ? replacement.original.upperBound - 1 : replacement.original.lowerBound + } else { + originalOffset += replacement.original.upperBound - replacement.indexed.upperBound + } + } + let bytes = document.utf8 + var position = bytes.index(bytes.startIndex, offsetBy: min(originalOffset, bytes.count)) + while String.Index(position, within: document) == nil, position > bytes.startIndex { + position = bytes.index(before: position) + } + return String.Index(position, within: document)! + } + public fileprivate(set) var contents: [Int: String] = [:] init(preprocessText: String) { @@ -51,11 +86,12 @@ public extension MarkdownParser { let matches = regex.matches( in: document, options: [], - range: NSRange(location: 0, length: document.count) + range: NSRange(location: 0, length: document.utf16.count) ).reversed() if matches.isEmpty { return } var indexer = 0 + var substitutions: [(original: Range, length: Int)] = [] for match in matches where match.numberOfRanges > 1 { var mathContentRange: NSRange? @@ -81,9 +117,22 @@ public extension MarkdownParser { contents[mathIndex] = mathContent let replacement = MarkdownParser.replacementText(for: .math, identifier: String(mathIndex)) + if let originalRange = Range(fullMatchRange, in: self.document) { + let bytes = self.document.utf8 + let start = bytes.distance(from: bytes.startIndex, to: originalRange.lowerBound) + let end = bytes.distance(from: bytes.startIndex, to: originalRange.upperBound) + substitutions.append((start.. [MarkdownInlineNode] { guard let regex = mathPatternWithinBlock else { return [.text(text)] } - let matches = regex.matches(in: text, options: [], range: NSRange(location: 0, length: text.count)) + let matches = regex.matches(in: text, options: [], range: NSRange(location: 0, length: text.utf16.count)) if matches.isEmpty { return [.text(text)] } var result: [MarkdownInlineNode] = [] @@ -248,7 +297,7 @@ extension MarkdownParser { lastEnd = fullMatchRange.location + fullMatchRange.length } - if lastEnd < text.count { + if lastEnd < text.utf16.count { let remainingText = (text as NSString).substring(from: lastEnd) if !remainingText.isEmpty { result.append(.text(remainingText)) diff --git a/Sources/MarkdownParser/MarkdownParser/MarkdownParser.swift b/Sources/MarkdownParser/MarkdownParser/MarkdownParser.swift index 9db1d90..fd35476 100644 --- a/Sources/MarkdownParser/MarkdownParser/MarkdownParser.swift +++ b/Sources/MarkdownParser/MarkdownParser/MarkdownParser.swift @@ -38,20 +38,55 @@ public class MarkdownParser { public let mathContext: [Int: String] } + public struct RangedParseResult { + public let result: ParseResult + /// Ranges in the original source, including math before placeholder substitution. + public let blockRanges: [RootBlockRange] + } + public func parse(_ markdown: String) -> ParseResult { + parseSource(markdown, includingRanges: false).result + } + + public func parseWithBlockRanges(_ markdown: String) -> RangedParseResult { + parseSource(markdown, includingRanges: true) + } + + private func parseSource(_ markdown: String, includingRanges: Bool) -> RangedParseResult { let math = MathContext(preprocessText: markdown) math.process() - let markdown = math.indexedContent ?? markdown - let nodes = withParser { parser in - markdown.withCString { str in - cmark_parser_feed(parser, str, strlen(str)) - return cmark_parser_finish(parser) + let source = math.indexedContent ?? markdown + return withParser { parser in + source.withCString { cmark_parser_feed(parser, $0, source.utf8.count) } + guard let root = cmark_parser_finish(parser) else { + assertionFailure("Unable to finish markdown parsing") + return .init(result: .init(document: [], mathContext: [:]), blockRanges: []) } + defer { cmark_node_free(root) } + let sourceLines = includingRanges ? SourceLineIndex(source) : nil + var blocks: [MarkdownBlockNode] = [] + var ranges: [RootBlockRange] = [] + for child in root.children { + let context = SpecializeContext() + if let node = MarkdownBlockNode(unsafeNode: child) { context.append(node) } + let transformed = finalizeMathBlocks(context.complete(), mathContext: math) + blocks.append(contentsOf: transformed) + if let sourceLines, + let start = sourceLines.index(line: Int(child.pointee.start_line), column: Int(child.pointee.start_column)), + let end = sourceLines.index(line: Int(child.pointee.end_line), column: Int(child.pointee.end_column)) { + ranges.append(.init( + type: child.nodeType, + startIndex: math.originalIndex(for: start, in: source, isEnd: false), + endIndex: math.originalIndex(for: end, in: source, isEnd: true), + outputBlockCount: transformed.count + )) + } + } + return .init( + result: .init(document: blocks, mathContext: math.contents), + blockRanges: ranges + ) } - defer { if let nodes { cmark_node_free(nodes) } } - var blocks = dumpBlocks(root: nodes) - blocks = finalizeMathBlocks(blocks, mathContext: math) - return .init(document: blocks, mathContext: math.contents) } public struct RootBlockRange { @@ -120,7 +155,7 @@ public class MarkdownParser { guard !previousMarkdown.isEmpty else { return nil } guard newMarkdown.count > previousMarkdown.count else { return nil } - let ranges = previousRanges ?? parseBlockRange(previousMarkdown) + let ranges = previousRanges ?? parseWithBlockRanges(previousMarkdown).blockRanges guard !ranges.isEmpty else { return nil } let tailRootBlockCount = preferredTailRootBlockCount( @@ -147,8 +182,9 @@ public class MarkdownParser { let tailMarkdown = String(newMarkdown[stablePrefixStartIndex...]) let mathIdentifierOffset = maxMathIdentifier(in: previousBlocks.prefix(stablePrefixBlockCount)) + 1 - let tailResult = shiftMathIdentifiers(in: parse(tailMarkdown), by: mathIdentifierOffset) - let tailRanges = parseBlockRange(tailMarkdown) + let tail = parseWithBlockRanges(tailMarkdown) + let tailResult = shiftMathIdentifiers(in: tail.result, by: mathIdentifierOffset) + let tailRanges = tail.blockRanges let shiftedTailRanges = shiftRanges( tailRanges, from: tailMarkdown, diff --git a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Private.swift b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Private.swift index 349d16f..97f693c 100644 --- a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Private.swift +++ b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Private.swift @@ -27,6 +27,7 @@ extension MarkdownTextView { case parse(markdown: String, theme: MarkdownTheme, incrementalContext: IncrementalParsingContext?) case parsedFull( result: MarkdownParser.ParseResult, + ranges: [MarkdownParser.RootBlockRange], content: PreprocessedContent, theme: MarkdownTheme, rawMarkdown: String @@ -75,11 +76,13 @@ extension MarkdownTextView { ) } - let result = parser.parse(markdown) + let parsed = parser.parseWithBlockRanges(markdown) + let result = parsed.result // Code highlighting runs on background; math rendering deferred to main let content = PreprocessedContent(parserResult: result, theme: theme, backgroundSafe: true) return .parsedFull( result: result, + ranges: parsed.blockRanges, content: content, theme: theme, rawMarkdown: markdown @@ -175,14 +178,27 @@ extension MarkdownTextView { private func applyRawMarkdownUpdate(_ update: RawMarkdownUpdate) { switch update { case let .ready(content, rawMarkdown): + // The plain-text fast path changes only the final paragraph's end. + // Keep its source map so the next structural token does not reparse + // the entire previous document just to recover block ranges. + if var ranges = lastRootBlockRanges, let last = ranges.last, + last.outputBlockCount == 1, !rawMarkdown.isEmpty { + ranges[ranges.count - 1] = .init( + type: last.type, startIndex: last.startIndex, + endIndex: rawMarkdown.index(before: rawMarkdown.endIndex), + outputBlockCount: last.outputBlockCount + ) + lastRootBlockRanges = ranges + } else { + lastRootBlockRanges = nil + } lastRawMarkdown = rawMarkdown - lastRootBlockRanges = nil use(content, layoutImmediately: false) - case let .parsedFull(result, content, theme, rawMarkdown): + case let .parsedFull(result, ranges, content, theme, rawMarkdown): // Complete math rendering on main thread where UI context is available let finalContent = content.completeMathRendering(parserResult: result, theme: theme) lastRawMarkdown = rawMarkdown - lastRootBlockRanges = nil + lastRootBlockRanges = ranges use(finalContent, layoutImmediately: false) case let .parsedIncremental(result, tailContent, context, theme, rawMarkdown): let completedTailContent = tailContent.completeMathRendering( diff --git a/Tests/MarkdownViewTests/IncrementalParsingTests.swift b/Tests/MarkdownViewTests/IncrementalParsingTests.swift index 615786b..48df370 100644 --- a/Tests/MarkdownViewTests/IncrementalParsingTests.swift +++ b/Tests/MarkdownViewTests/IncrementalParsingTests.swift @@ -5,6 +5,69 @@ final class IncrementalParsingTests: XCTestCase { private let parser = MarkdownParser() + func testCombinedParseMatchesStandaloneParsingAndRanges() { + let fixtures = [ + "", "你好\r\n\r\nCafe\u{301}", + "# Title\n\nText **bold**\n\n```js\nconst x = 1;\n```", + "- Item\n\n ```js\n const x = 1;\n ```\n\nAfter", + "| A | B |\n|---|---|\n| C | D |\n\n> Quote" + ] + for markdown in fixtures { + let combined = parser.parseWithBlockRanges(markdown) + let standalone = parser.parse(markdown) + XCTAssertEqual(combined.result.document, standalone.document) + XCTAssertEqual(combined.result.mathContext, standalone.mathContext) + XCTAssertEqual(combined.blockRanges.reduce(0) { $0 + $1.outputBlockCount }, combined.result.document.count) + let ranges = parser.parseBlockRange(markdown) + XCTAssertEqual(combined.blockRanges.map(\.startIndex), ranges.map(\.startIndex)) + XCTAssertEqual(combined.blockRanges.map(\.endIndex), ranges.map(\.endIndex)) + XCTAssertEqual(combined.blockRanges.map(\.outputBlockCount), ranges.map(\.outputBlockCount)) + } + } + + func testCombinedParseMapsMultilineMathToOriginalUnicodeSource() { + let blocks = ["前言 cafe\u{301}", "$$\nx + y\n$$", "間 $$z$$ 後", "End"] + for newline in ["\n", "\r\n"] { + let expected = blocks.map { $0.replacingOccurrences(of: "\n", with: newline) } + let markdown = expected.joined(separator: newline + newline) + let combined = parser.parseWithBlockRanges(markdown) + XCTAssertEqual(combined.result.document, parser.parse(markdown).document) + XCTAssertEqual(combined.result.mathContext.count, 2) + XCTAssertEqual(combined.blockRanges.map { String(markdown[$0.startIndex...$0.endIndex]) }, expected) + XCTAssertEqual(combined.blockRanges.reduce(0) { $0 + $1.outputBlockCount }, combined.result.document.count) + } + } + + func testMathSourceRangesSurviveRepeatedIncrementalUpdates() throws { + var markdown = "前言\n\n$$\nx + y\n$$\n\nAlpha\n\nBravo\n\nCharlie\n\nDelta" + let initial = parser.parseWithBlockRanges(markdown) + var blocks = initial.result.document + var ranges = initial.blockRanges + for suffix in [" more", "\n\nEcho", " text"] { + let next = markdown + suffix + let incremental = try XCTUnwrap(parser.parseIncremental( + previousMarkdown: markdown, newMarkdown: next, + previousBlocks: blocks, previousRanges: ranges + )) + blocks = Array(blocks.prefix(incremental.stablePrefixBlockCount)) + incremental.tailResult.document + ranges = incremental.blockRanges + let full = parser.parseWithBlockRanges(next) + XCTAssertEqual(blocks, full.result.document) + XCTAssertEqual(ranges.map { String(next[$0.startIndex...$0.endIndex]) }, + full.blockRanges.map { String(next[$0.startIndex...$0.endIndex]) }) + markdown = next + } + } + + func testUnicodeInlineMathPreservesTrailingText() { + let result = parser.parse("你好 $x$ 後文") + XCTAssertEqual(Array(result.mathContext.values), ["x"]) + guard let first = result.document.first, + case let .paragraph(content) = first else { return XCTFail("Expected paragraph") } + XCTAssertEqual(content.first, .text("你好 ")) + XCTAssertEqual(content.last, .text(" 後文")) + } + func testBlockRangesHandleUnicodeAndLineEndings() { for newline in ["\n", "\r\n", "\r"] { let blocks = ["你好", "cafe\u{301}", "終わり"] diff --git a/Tests/MarkdownViewTests/PerformanceTests.swift b/Tests/MarkdownViewTests/PerformanceTests.swift index 4fe3901..f7d15ce 100644 --- a/Tests/MarkdownViewTests/PerformanceTests.swift +++ b/Tests/MarkdownViewTests/PerformanceTests.swift @@ -148,6 +148,21 @@ final class PerformanceTests: XCTestCase { } } + func testCombinedParsingPerformance_1000Blocks() { + let markdown = generateLargeMarkdown(blockCount: 1000) + let parser = MarkdownParser() + measure { _ = parser.parseWithBlockRanges(markdown) } + } + + func testSeparateParsingAndRangesPerformance_1000Blocks() { + let markdown = generateLargeMarkdown(blockCount: 1000) + let parser = MarkdownParser() + measure { + _ = parser.parse(markdown) + _ = parser.parseBlockRange(markdown) + } + } + func testBlockRangeIndexPerformance_1000Blocks() { let markdown = generateLargeMarkdown(blockCount: 1000) let parser = MarkdownParser() diff --git a/Tests/MarkdownViewTests/StreamingFastPathTests.swift b/Tests/MarkdownViewTests/StreamingFastPathTests.swift index e470608..aaed5a6 100644 --- a/Tests/MarkdownViewTests/StreamingFastPathTests.swift +++ b/Tests/MarkdownViewTests/StreamingFastPathTests.swift @@ -56,6 +56,25 @@ final class StreamingFastPathTests: XCTestCase { XCTAssertEqual(view.document.blocks, parser.parse(latest).document) } + func testPlainAppendKeepsRangesForNextStructuralUpdate() throws { + let view = MarkdownTextView() + view.throttleInterval = nil + let initial = "Alpha\n\nBravo\n\nCharlie\n\nDelta\n\nEcho\n\nFoxtrot" + view.setMarkdown(string: initial) + waitForPreprocessing(view) + XCTAssertNotNil(view.lastRootBlockRanges) + let appended = initial + " more" + view.setMarkdown(string: appended) + waitForPreprocessing(view) + let ranges = try XCTUnwrap(view.lastRootBlockRanges) + XCTAssertEqual(ranges.map { String(appended[$0.startIndex...$0.endIndex]) }, + parser.parseWithBlockRanges(appended).blockRanges.map { String(appended[$0.startIndex...$0.endIndex]) }) + let structural = appended + " **bold**" + view.setMarkdown(string: structural) + waitForPreprocessing(view) + XCTAssertEqual(view.document.blocks, parser.parse(structural).document) + } + func testResetDiscardsInFlightResult() { let view = MarkdownTextView() view.throttleInterval = nil From dc22773ddb5cde1a74e0f4419105c43f710573f8 Mon Sep 17 00:00:00 2001 From: v0 Date: Sat, 7 Nov 2026 20:33:17 +0000 Subject: [PATCH 2/3] Reuse unchanged attributed prefixes and CoreText line layouts --- Sources/Litext/LTXLabel/LTXLabel.swift | 16 +- .../LTXLabel/TextLayout/LTXTextLayout.swift | 133 ++++++++++----- .../MarkdownTextBuilder/TextBuilder+Do.swift | 5 +- .../MarkdownTextBuilder/TextBuilder.swift | 36 +++- .../MarkdownTextView+Update.swift | 8 +- .../RenderingLifetimeTests.swift | 20 +++ .../MarkdownViewTests/TiledLayoutTests.swift | 155 ++++++++++++++++++ 7 files changed, 325 insertions(+), 48 deletions(-) diff --git a/Sources/Litext/LTXLabel/LTXLabel.swift b/Sources/Litext/LTXLabel/LTXLabel.swift index 8dbc232..d69175d 100644 --- a/Sources/Litext/LTXLabel/LTXLabel.swift +++ b/Sources/Litext/LTXLabel/LTXLabel.swift @@ -18,11 +18,25 @@ public class LTXLabel: LTXPlatformView, Identifiable { // Rebuilding the layout recreates the CTFramesetter and triggers a // full relayout; skip it when the content is unchanged. guard !attributedText.isEqual(to: oldValue) else { return } - textLayout = LTXTextLayout(attributedString: attributedText) + let nextLayout = LTXTextLayout(attributedString: attributedText) + if let pendingPrefixUTF16Length { + nextLayout.reusePrefix(from: textLayout, utf16Length: pendingPrefixUTF16Length) + } + textLayout = nextLayout handleRevealTextChange() } } + private var pendingPrefixUTF16Length: Int? + + /// Updates content with a candidate unchanged prefix. The layout validates + /// attributes and paragraph boundaries before reusing immutable CoreText lines. + public func setAttributedText(_ text: NSAttributedString, preservingPrefixUTF16Length length: Int) { + pendingPrefixUTF16Length = length + defer { pendingPrefixUTF16Length = nil } + attributedText = text + } + // MARK: - Streaming reveal /// When `true`, characters appended to `attributedText` fade in (left→right as diff --git a/Sources/Litext/LTXLabel/TextLayout/LTXTextLayout.swift b/Sources/Litext/LTXLabel/TextLayout/LTXTextLayout.swift index 5fdba33..bd35a09 100644 --- a/Sources/Litext/LTXLabel/TextLayout/LTXTextLayout.swift +++ b/Sources/Litext/LTXLabel/TextLayout/LTXTextLayout.swift @@ -47,6 +47,15 @@ public nonisolated class LTXTextLayout: NSObject { private var lineOrigins: [CGPoint]? private var lineBands: [LineBand] = [] private var hasOrderedLineBands = true + private struct PrefixSnapshot { + let lines: [CTLine] + let origins: [CGPoint] + let size: CGSize + let anchorLocation: Int + let anchorOrigin: CGPoint + } + private var prefixSnapshot: PrefixSnapshot? + private(set) var reusedLineCount = 0 private var _highlightRegions: [Int: LTXHighlightRegion] private var cachedSuggestedSize: CGSize? private var cachedSuggestedSizeConstraint: CGSize? @@ -91,7 +100,34 @@ public nonisolated class LTXTextLayout: NSObject { deinit {} + func reusePrefix(from previous: LTXTextLayout, utf16Length length: Int) { + guard length > 0, length <= attributedString.length, + length <= previous.attributedString.length, + previous.containerSize.width > 0, + let oldLines = previous.lines, let oldOrigins = previous.lineOrigins else { return } + let prefixRange = NSRange(location: 0, length: length) + guard attributedString.attributedSubstring(from: prefixRange).isEqual( + to: previous.attributedString.attributedSubstring(from: prefixRange) + ) else { return } + let source = attributedString.string as NSString + let anchor = source.paragraphRange(for: NSRange(location: length - 1, length: 0)) + // Re-layout one complete, unchanged paragraph at the join. Its baseline + // anchors paragraph spacing without independently stacking block heights. + guard NSMaxRange(anchor) <= length, + let anchorIndex = oldLines.firstIndex(where: { + CTLineGetStringRange($0).location == anchor.location + }), anchorIndex > 0 else { return } + prefixSnapshot = PrefixSnapshot( + lines: Array(oldLines[.. previous.minY || band.maxY > previous.maxY { - hasOrderedLineBands = false - } - lineBands.append(band) - } + lineOrigins = origins + } + guard let lines, let lineOrigins else { return } + lineBands.reserveCapacity(lines.count) + for (index, line) in lines.enumerated() { + var ascent: CGFloat = 0 + var descent: CGFloat = 0 + var leading: CGFloat = 0 + CTLineGetTypographicBounds(line, &ascent, &descent, &leading) + let band = LineBand(minY: lineOrigins[index].y - descent, maxY: lineOrigins[index].y + ascent + leading) + if let previous = lineBands.last, + band.minY > previous.minY || band.maxY > previous.maxY { + hasOrderedLineBands = false } + lineBands.append(band) } } + private func generateTailLayout(path: CGPath) -> Bool { + guard let prefix = prefixSnapshot, prefix.size.width == containerSize.width, + containerSize.height > 0 else { return false } + let frame = CTFramesetterCreateFrame( + framesetter, CFRange(location: prefix.anchorLocation, length: 0), path, nil + ) + let visible = CTFrameGetVisibleStringRange(frame) + // Finite/truncated containers retain the full-frame implementation. + guard visible.location + visible.length == attributedString.length, + let tailLines = CTFrameGetLines(frame) as? [CTLine], !tailLines.isEmpty, + CTLineGetStringRange(tailLines[0]).location == prefix.anchorLocation else { return false } + var origins = [CGPoint](repeating: .zero, count: tailLines.count) + CTFrameGetLineOrigins(frame, CFRange(location: 0, length: 0), &origins) + let heightDelta = containerSize.height - prefix.size.height + let translation = prefix.anchorOrigin.y + heightDelta - origins[0].y + for index in origins.indices { + origins[index].y += translation + var ascent: CGFloat = 0 + var descent: CGFloat = 0 + CTLineGetTypographicBounds(tailLines[index], &ascent, &descent, nil) + guard origins[index].y - descent >= 0, + origins[index].y + ascent <= containerSize.height else { return false } + } + lines = prefix.lines + tailLines + lineOrigins = prefix.origins.map { CGPoint(x: $0.x, y: $0.y + heightDelta) } + origins + reusedLineCount = prefix.lines.count + return true + } + @MainActor private func extractHighlightRegions() { guard hasHighlightAttributes else { return } enumerateLines { line, _, lineOrigin in diff --git a/Sources/MarkdownView/MarkdownTextBuilder/TextBuilder+Do.swift b/Sources/MarkdownView/MarkdownTextBuilder/TextBuilder+Do.swift index 190e41c..ea458b9 100644 --- a/Sources/MarkdownView/MarkdownTextBuilder/TextBuilder+Do.swift +++ b/Sources/MarkdownView/MarkdownTextBuilder/TextBuilder+Do.swift @@ -280,9 +280,10 @@ extension TextBuilder { view: MarkdownTextView, viewProvider: ReusableViewProvider, changes: [ASTDiff.Change], - cachedSegments: [NSAttributedString] + cachedSegments: [NSAttributedString], + cachedDocument: NSAttributedString? = nil ) -> BuildResult { configuredBuilder(view: view, viewProvider: viewProvider) - .buildIncremental(changes: changes, cachedSegments: cachedSegments) + .buildIncremental(changes: changes, cachedSegments: cachedSegments, cachedDocument: cachedDocument) } } diff --git a/Sources/MarkdownView/MarkdownTextBuilder/TextBuilder.swift b/Sources/MarkdownView/MarkdownTextBuilder/TextBuilder.swift index 02ccf5b..272b1fa 100644 --- a/Sources/MarkdownView/MarkdownTextBuilder/TextBuilder.swift +++ b/Sources/MarkdownView/MarkdownTextBuilder/TextBuilder.swift @@ -20,7 +20,7 @@ final class TextBuilder { private let viewProvider: ReusableViewProvider private var theme: MarkdownTheme = .default private var layoutWidth: CGFloat = 0 - private let text: NSMutableAttributedString = .init() + private var text: NSMutableAttributedString = .init() private let context: MarkdownTextView.PreprocessedContent private var bulletDrawing: BulletDrawingCallback? @@ -105,6 +105,7 @@ final class TextBuilder { let subviews: [PlatformView] /// Per-block attributed strings, parallel to the input block array. let blockSegments: [NSAttributedString] + var preservedPrefixUTF16Length: Int = 0 } private var previouslyBuilt = false @@ -133,11 +134,31 @@ final class TextBuilder { /// Build only specific block indices, reusing cached segments for unchanged blocks. func buildIncremental( changes: [ASTDiff.Change], - cachedSegments: [NSAttributedString] + cachedSegments: [NSAttributedString], + cachedDocument: NSAttributedString? = nil ) -> BuildResult { assert(!previouslyBuilt, "TextBuilder can only be built once.") previouslyBuilt = true + var prefixCount = 0 + var prefixLength = 0 + if let cachedDocument { + for change in changes { + guard case let .keep(oldIndex, newIndex) = change, + oldIndex == prefixCount, newIndex == prefixCount, + oldIndex < cachedSegments.count else { break } + prefixCount += 1 + prefixLength += cachedSegments[oldIndex].length + } + if prefixLength <= cachedDocument.length { + text = cachedDocument.mutableCopy() as! NSMutableAttributedString + text.deleteCharacters(in: NSRange(location: prefixLength, length: text.length - prefixLength)) + } else { + prefixCount = 0 + prefixLength = 0 + } + } + var subviewCollector = [PlatformView]() var segments = [NSAttributedString]() segments.reserveCapacity(nodes.count) @@ -150,7 +171,7 @@ final class TextBuilder { if oldIndex < cachedSegments.count { let segment = cachedSegments[oldIndex] segments.append(segment) - text.append(segment) + if newIndex >= prefixCount { text.append(segment) } // Also collect any subviews from the cached segment collectSubviews(from: segment, into: &subviewCollector) } else { @@ -181,8 +202,13 @@ final class TextBuilder { } } - text.fixAttributes(in: .init(location: 0, length: text.length)) - return .init(document: text, subviews: subviewCollector, blockSegments: segments) + text.fixAttributes(in: .init(location: prefixLength, length: text.length - prefixLength)) + return .init( + document: text, + subviews: subviewCollector, + blockSegments: segments, + preservedPrefixUTF16Length: prefixLength + ) } /// Extract context views (CodeView, TableView) embedded in an attributed string. diff --git a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Update.swift b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Update.swift index 6ff68d4..41faafe 100644 --- a/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Update.swift +++ b/Sources/MarkdownView/MarkdownTextView/MarkdownTextView+Update.swift @@ -82,7 +82,8 @@ extension MarkdownTextView { view: self, viewProvider: viewProvider, changes: incrementalRenderContext.changes, - cachedSegments: incrementalRenderContext.cachedSegments + cachedSegments: incrementalRenderContext.cachedSegments, + cachedDocument: lastBuildResult?.document ) } else { artifacts = TextBuilder.build( @@ -91,7 +92,10 @@ extension MarkdownTextView { ) } - textView.attributedText = artifacts.document + textView.setAttributedText( + artifacts.document, + preservingPrefixUTF16Length: artifacts.preservedPrefixUTF16Length + ) contextViews = artifacts.subviews lastRenderedBlocks = document.blocks lastBuildResult = artifacts diff --git a/Tests/MarkdownViewTests/RenderingLifetimeTests.swift b/Tests/MarkdownViewTests/RenderingLifetimeTests.swift index ff8e4d4..fa279cf 100644 --- a/Tests/MarkdownViewTests/RenderingLifetimeTests.swift +++ b/Tests/MarkdownViewTests/RenderingLifetimeTests.swift @@ -53,6 +53,26 @@ final class RenderingLifetimeTests: XCTestCase { } } + func testIncrementalBuilderPreservesPrefixAndOriginalSnapshot() { + let previous = MarkdownTextView.PreprocessedContent(markdown: "Alpha\n\nBravo\n\nCharlie", theme: .default) + let provider = ReusableViewProvider() + let original = TextBuilder(nodes: previous.blocks, context: previous, viewProvider: provider).build() + let originalSnapshot = NSAttributedString(attributedString: original.document) + for markdown in ["Alpha\n\nBravo\n\nChanged tail", "Alpha\n\nBravo", "Alpha\n\nInserted\n\nCharlie"] { + let next = MarkdownTextView.PreprocessedContent(markdown: markdown, theme: .default) + let incremental = TextBuilder(nodes: next.blocks, context: next, viewProvider: provider).buildIncremental( + changes: ASTDiff.diff(old: previous.blocks, new: next.blocks), + cachedSegments: original.blockSegments, + cachedDocument: original.document + ) + let full = TextBuilder(nodes: next.blocks, context: next, viewProvider: ReusableViewProvider()).build() + XCTAssertTrue(incremental.document.isEqual(to: full.document)) + XCTAssertTrue(original.document.isEqual(to: originalSnapshot)) + XCTAssertGreaterThan(incremental.preservedPrefixUTF16Length, 0) + XCTAssertTrue(incremental.blockSegments[0] === original.blockSegments[0]) + } + } + func testTextBuilderReleaseInsideSynchronousTaskLocalScope() { let context = MarkdownTextView.PreprocessedContent(markdown: "Hello", theme: .default) let viewProvider = ReusableViewProvider() diff --git a/Tests/MarkdownViewTests/TiledLayoutTests.swift b/Tests/MarkdownViewTests/TiledLayoutTests.swift index 4b0d931..3f3a3ad 100644 --- a/Tests/MarkdownViewTests/TiledLayoutTests.swift +++ b/Tests/MarkdownViewTests/TiledLayoutTests.swift @@ -3,6 +3,11 @@ import CoreText import Foundation import XCTest @testable import Litext +#if canImport(UIKit) +import UIKit +#elseif canImport(AppKit) +import AppKit +#endif final class TiledLayoutTests: XCTestCase { private let size = CGSize(width: 320, height: 6000) @@ -64,6 +69,156 @@ final class TiledLayoutTests: XCTestCase { XCTAssertEqual(try pixels(full), try pixels(tiled)) } + func testReusedPrefixMatchesFullLayoutAndPixels() throws { + let previous = makeLayout() + let changed = previous.attributedString.mutableCopy() as! NSMutableAttributedString + let lastParagraph = (changed.string as NSString).paragraphRange(for: NSRange(location: changed.length - 1, length: 0)) + changed.replaceCharacters(in: lastParagraph, with: "Changed tail with 日本語 and cafe\u{301}\nExtra line\n") + let incremental = LTXTextLayout(attributedString: changed) + incremental.reusePrefix(from: previous, utf16Length: lastParagraph.location) + incremental.containerSize = size + let full = LTXTextLayout(attributedString: changed) + full.containerSize = size + XCTAssertGreaterThan(incremental.reusedLineCount, 0) + XCTAssertEqual(incremental.lineStringRanges(), full.lineStringRanges()) + assertEquivalentGeometry(incremental, full) + let expectedPixels = try makeContext() + full.draw(in: expectedPixels) + let actualPixels = try makeContext() + incremental.draw(in: actualPixels) + XCTAssertEqual(try pixels(actualPixels), try pixels(expectedPixels)) + let band = CGRect(x: 0, y: 3200, width: size.width, height: 180) + let fullTile = try makeContext() + fullTile.clip(to: band) + full.draw(in: fullTile, visibleRect: band) + let incrementalTile = try makeContext() + incrementalTile.clip(to: band) + incremental.draw(in: incrementalTile, visibleRect: band) + XCTAssertEqual(try pixels(fullTile), try pixels(incrementalTile)) + } + + @MainActor + func testReusePreservesParagraphSpacingAndLinkRegions() { + let source = makeLayout().attributedString.mutableCopy() as! NSMutableAttributedString + let style = NSMutableParagraphStyle() + style.paragraphSpacing = 7 + style.paragraphSpacingBefore = 3 + style.lineSpacing = 2 + style.firstLineHeadIndent = 12 + source.addAttribute(.paragraphStyle, value: style, range: NSRange(location: 0, length: source.length)) + source.addAttribute(.link, value: "https://example.com", range: NSRange(location: 8, length: 10)) + let previous = LTXTextLayout(attributedString: source) + previous.containerSize = CGSize(width: 320, height: 12000) + let changed = source.mutableCopy() as! NSMutableAttributedString + changed.append(NSAttributedString(string: "More linked text\n", attributes: [ + .paragraphStyle: style, .link: "https://example.com/tail" + ])) + let incremental = LTXTextLayout(attributedString: changed) + incremental.reusePrefix(from: previous, utf16Length: source.length) + incremental.containerSize = previous.containerSize + let full = LTXTextLayout(attributedString: changed) + full.containerSize = previous.containerSize + XCTAssertGreaterThan(incremental.reusedLineCount, 0) + assertEquivalentGeometry(incremental, full) + incremental.updateHighlightRegions() + full.updateHighlightRegions() + let actualRegions = incremental.highlightRegions.sorted { $0.stringRange.location < $1.stringRange.location } + let expectedRegions = full.highlightRegions.sorted { $0.stringRange.location < $1.stringRange.location } + XCTAssertEqual(actualRegions.map(\.stringRange), expectedRegions.map(\.stringRange)) + for (actual, expected) in zip(actualRegions, expectedRegions) { + XCTAssertEqual(actual.rects, expected.rects) + } + } + + func testReuseFallsBackForWidthChangesAndClippedContainers() { + let previous = makeLayout() + let changed = previous.attributedString.mutableCopy() as! NSMutableAttributedString + changed.append(NSAttributedString(string: "More text")) + for dimensions in [CGSize(width: 160, height: size.height), CGSize(width: size.width, height: 200)] { + let incremental = LTXTextLayout(attributedString: changed) + incremental.reusePrefix(from: previous, utf16Length: previous.attributedString.length) + incremental.containerSize = dimensions + let full = LTXTextLayout(attributedString: changed) + full.containerSize = dimensions + XCTAssertEqual(incremental.reusedLineCount, 0) + assertEquivalentGeometry(incremental, full) + } + } + + func testReuseRejectsChangedPrefixAttributes() { + let previous = makeLayout() + let changed = previous.attributedString.mutableCopy() as! NSMutableAttributedString + changed.addAttribute(NSAttributedString.Key(kCTFontAttributeName as String), + value: CTFontCreateWithName("Helvetica" as CFString, 30, nil), + range: NSRange(location: 0, length: 5)) + let incremental = LTXTextLayout(attributedString: changed) + incremental.reusePrefix(from: previous, utf16Length: changed.length) + incremental.containerSize = size + let full = LTXTextLayout(attributedString: changed) + full.containerSize = size + XCTAssertEqual(incremental.reusedLineCount, 0) + assertEquivalentGeometry(incremental, full) + } + + func testReusedPrefixTracksContainerHeightChanges() { + let previous = makeLayout() + let changed = previous.attributedString.mutableCopy() as! NSMutableAttributedString + changed.append(NSAttributedString(string: "New paragraph\n")) + let incremental = LTXTextLayout(attributedString: changed) + incremental.reusePrefix(from: previous, utf16Length: previous.attributedString.length) + incremental.containerSize = CGSize(width: size.width, height: size.height + 100) + let full = LTXTextLayout(attributedString: changed) + full.containerSize = incremental.containerSize + XCTAssertGreaterThan(incremental.reusedLineCount, 0) + assertEquivalentGeometry(incremental, full) + } + + private func assertEquivalentGeometry(_ actual: LTXTextLayout, _ expected: LTXTextLayout, + file: StaticString = #filePath, line: UInt = #line) { + XCTAssertEqual(actual.lineStringRanges(), expected.lineStringRanges(), file: file, line: line) + let actualRects = actual.lineRects() + let expectedRects = expected.lineRects() + XCTAssertEqual(actualRects.count, expectedRects.count, file: file, line: line) + for (a, b) in zip(actualRects, expectedRects) { + XCTAssertEqual(a.minX, b.minX, accuracy: 0.001, file: file, line: line) + XCTAssertEqual(a.minY, b.minY, accuracy: 0.001, file: file, line: line) + XCTAssertEqual(a.width, b.width, accuracy: 0.001, file: file, line: line) + XCTAssertEqual(a.height, b.height, accuracy: 0.001, file: file, line: line) + XCTAssertEqual(actual.nearestTextIndex(at: CGPoint(x: a.midX, y: a.midY)), + expected.nearestTextIndex(at: CGPoint(x: b.midX, y: b.midY)), file: file, line: line) + } + let range = NSRange(location: 0, length: actual.attributedString.length) + let actualSelection = actual.rects(for: range) + let expectedSelection = expected.rects(for: range) + XCTAssertEqual(actualSelection.count, expectedSelection.count, file: file, line: line) + for (a, b) in zip(actualSelection, expectedSelection) { + XCTAssertEqual(a.minY, b.minY, accuracy: 0.001, file: file, line: line) + XCTAssertEqual(a.width, b.width, accuracy: 0.001, file: file, line: line) + } + } + + func testReusedPrefixLayoutPerformance() { + let previous = makeLayout() + let changed = previous.attributedString.mutableCopy() as! NSMutableAttributedString + changed.append(NSAttributedString(string: "New paragraph\n")) + measure { + let layout = LTXTextLayout(attributedString: changed) + layout.reusePrefix(from: previous, utf16Length: previous.attributedString.length) + layout.containerSize = size + XCTAssertGreaterThan(layout.reusedLineCount, 0) + } + } + + func testFullLayoutPerformance() { + let previous = makeLayout() + let changed = previous.attributedString.mutableCopy() as! NSMutableAttributedString + changed.append(NSAttributedString(string: "New paragraph\n")) + measure { + let layout = LTXTextLayout(attributedString: changed) + layout.containerSize = size + } + } + func testLowerDocumentTileDrawingPerformance() throws { let layout = makeLayout() let context = try makeContext() From 85413dd9beea8340b60686cb5809b6597fec54fe Mon Sep 17 00:00:00 2001 From: v0 Date: Sun, 8 Nov 2026 00:44:02 +0000 Subject: [PATCH 3/3] Expose RootBlockRange initializer for cross-module streaming updates --- .../MarkdownParser/MarkdownParser.swift | 7 +++++++ .../PublicParserAPITests.swift | 20 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 Tests/MarkdownViewTests/PublicParserAPITests.swift diff --git a/Sources/MarkdownParser/MarkdownParser/MarkdownParser.swift b/Sources/MarkdownParser/MarkdownParser/MarkdownParser.swift index fd35476..0f7c923 100644 --- a/Sources/MarkdownParser/MarkdownParser/MarkdownParser.swift +++ b/Sources/MarkdownParser/MarkdownParser/MarkdownParser.swift @@ -94,6 +94,13 @@ public class MarkdownParser { public let startIndex: String.Index public let endIndex: String.Index public let outputBlockCount: Int + + public init(type: MarkdownNodeType, startIndex: String.Index, endIndex: String.Index, outputBlockCount: Int) { + self.type = type + self.startIndex = startIndex + self.endIndex = endIndex + self.outputBlockCount = outputBlockCount + } } public struct IncrementalParseResult { diff --git a/Tests/MarkdownViewTests/PublicParserAPITests.swift b/Tests/MarkdownViewTests/PublicParserAPITests.swift new file mode 100644 index 0000000..b5484f6 --- /dev/null +++ b/Tests/MarkdownViewTests/PublicParserAPITests.swift @@ -0,0 +1,20 @@ +import MarkdownParser +import XCTest + +final class PublicParserAPITests: XCTestCase { + func testRootBlockRangeCanBeConstructedOutsideParserModule() throws { + let markdown = "Original paragraph" + let parsed = MarkdownParser().parseWithBlockRanges(markdown) + let original = try XCTUnwrap(parsed.blockRanges.first) + let reconstructed = MarkdownParser.RootBlockRange( + type: original.type, + startIndex: original.startIndex, + endIndex: original.endIndex, + outputBlockCount: original.outputBlockCount + ) + XCTAssertEqual(reconstructed.type, original.type) + XCTAssertEqual(reconstructed.startIndex, original.startIndex) + XCTAssertEqual(reconstructed.endIndex, original.endIndex) + XCTAssertEqual(reconstructed.outputBlockCount, original.outputBlockCount) + } +}