diff --git a/Package.swift b/Package.swift index 2c98541d7..ab6e1227a 100644 --- a/Package.swift +++ b/Package.swift @@ -15,7 +15,8 @@ let package = Package( ], targets: [ .target(name: "Runestone", dependencies: [ - "TreeSitter" + "TreeSitter", + "TreeSitterSupport" ], resources: [ .copy("PrivacyInfo.xcprivacy"), .process("TextView/Appearance/Theme.xcassets") @@ -27,12 +28,14 @@ let package = Package( publicHeadersPath: "include", cSettings: [.headerSearchPath("src"), .define("_DARWIN_C_SOURCE")] ), + .target(name: "TreeSitterSupport", dependencies: ["TreeSitter"]), .target(name: "TestTreeSitterLanguages", cSettings: [ .headerSearchPath("private"), .unsafeFlags(["-w"]) ]), .testTarget(name: "RunestoneTests", dependencies: [ "Runestone", + "TreeSitterSupport", "TestTreeSitterLanguages" ]) ], diff --git a/Sources/Runestone/Library/NSString+Helpers.swift b/Sources/Runestone/Library/NSString+Helpers.swift index 142eaaa0d..d356aeb0a 100644 --- a/Sources/Runestone/Library/NSString+Helpers.swift +++ b/Sources/Runestone/Library/NSString+Helpers.swift @@ -24,6 +24,7 @@ extension NSString { if didGetBytes { return UnsafePointer(buffer) } else { + buffer.deallocate() return nil } } diff --git a/Sources/Runestone/TextView/Core/LayoutManager.swift b/Sources/Runestone/TextView/Core/LayoutManager.swift index 8719b86f0..65ab6621e 100644 --- a/Sources/Runestone/TextView/Core/LayoutManager.swift +++ b/Sources/Runestone/TextView/Core/LayoutManager.swift @@ -254,9 +254,37 @@ final class LayoutManager { } } /// The text covered by the fragments from the most recent viewport layout. - private(set) var visibleTextRange: NSRange? + var visibleTextRange: NSRange? { + visibleTextRanges.reduce(nil as NSRange?) { result, range in + result.map { NSUnionRange($0, range) } ?? range + } + } /// Individual visible ranges avoid including hidden columns between long unwrapped lines. - private(set) var visibleTextRanges: [NSRange] = [] + var visibleTextRanges: [NSRange] { + if let cachedVisibleTextRanges { + return cachedVisibleTextRanges + } + let ranges = visibleTextFragments.flatMap { snapshot in + let localRanges = snapshot.horizontalRange.map { horizontalRange in + LineFragmentRenderer.visibleCharacterRanges(in: horizontalRange, lineFragment: snapshot.fragment) + } ?? [snapshot.fragment.visibleRange] + return localRanges.map { localRange in + NSRange(location: snapshot.lineLocation + localRange.location, length: localRange.length) + } + } + cachedVisibleTextRanges = ranges + return ranges + } + + // Freeze the completed layout's drawing objects and coordinates. Controllers + // can receive new fragments before the next layout; spelling must not mix them. + private struct VisibleTextFragment { + let fragment: LineFragment + let lineLocation: Int + let horizontalRange: ClosedRange? + } + private var visibleTextFragments: [VisibleTextFragment] = [] + private var cachedVisibleTextRanges: [NSRange]? // MARK: - Views let gutterContainerView = GutterContainerView() @@ -576,8 +604,8 @@ extension LayoutManager { private func layoutLinesInViewport() { // Immediately bail out from generating lines in a viewport of zero size. guard viewport.size.width > 0 && viewport.size.height > 0 else { - visibleTextRange = nil - visibleTextRanges = [] + visibleTextFragments = [] + cachedVisibleTextRanges = [] return } let highlightDestinationSynchronously = needsSynchronousDestinationHighlight @@ -598,8 +626,7 @@ extension LayoutManager { var nextLine = lineManager.line(containingYOffset: insetViewport.minY) var appearedLineIDs: Set = [] var appearedLineFragmentIDs: Set = [] - var visibleRange: NSRange? - var visibleRanges: [NSRange] = [] + var textFragments: [VisibleTextFragment] = [] var maxY = insetViewport.minY while let line = nextLine, maxY < insetViewport.maxY, constrainingLineWidth > 0 { appearedLineIDs.insert(line.id) @@ -632,11 +659,10 @@ extension LayoutManager { let lineFragment = lineFragmentController.lineFragment var lineFragmentFrame: CGRect = .zero appearedLineFragmentIDs.insert(lineFragment.id) - for localRange in visibleCharacterRanges(in: lineFragmentController) { - let documentRange = NSRange(location: line.location + localRange.location, length: localRange.length) - visibleRange = visibleRange.map { NSUnionRange($0, documentRange) } ?? documentRange - visibleRanges.append(documentRange) - } + let horizontalRange: ClosedRange? = isLineWrappingEnabled ? nil + : (viewport.minX - leadingLineSpacing - 32) ... (viewport.maxX - leadingLineSpacing + 32) + textFragments.append(VisibleTextFragment(fragment: lineFragment, lineLocation: line.location, + horizontalRange: horizontalRange)) updateSpelling(on: lineFragmentController, lineLocation: line.location) lineFragmentController.highlightedRangeFragments = highlightService.highlightedRangeFragments(for: lineFragment, inLineWithID: line.id) @@ -665,8 +691,8 @@ extension LayoutManager { linesContainerView.frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height) // Update the visible lines and line fragments. Clean up everything that is not in the viewport anymore. visibleLineIDs = appearedLineIDs - visibleTextRange = visibleRange - visibleTextRanges = visibleRanges + visibleTextFragments = textFragments + cachedVisibleTextRanges = nil let disappearedLineIDs = oldVisibleLineIDs.subtracting(appearedLineIDs) let disappearedLineFragmentIDs = oldVisibleLineFragmentIDs.subtracting(appearedLineFragmentIDs) for disappearedLineID in disappearedLineIDs { @@ -863,16 +889,6 @@ extension LayoutManager { // MARK: - Spelling private extension LayoutManager { - private func visibleCharacterRanges(in controller: LineFragmentController) -> [NSRange] { - if isLineWrappingEnabled { - return [controller.lineFragment.visibleRange] - } - // Include partially visible glyphs and a small margin; the spelling service expands to word boundaries. - let lowerBound = viewport.minX - leadingLineSpacing - 32 - let upperBound = viewport.maxX - leadingLineSpacing + 32 - return controller.visibleCharacterRanges(in: lowerBound ... upperBound) - } - private func updateSpellingOnVisibleLines() { for lineID in visibleLineIDs { guard let lineController = lineControllerStorage[lineID] else { diff --git a/Sources/Runestone/TextView/Core/TextInputView.swift b/Sources/Runestone/TextView/Core/TextInputView.swift index a78fe2272..10d5b6d08 100644 --- a/Sources/Runestone/TextView/Core/TextInputView.swift +++ b/Sources/Runestone/TextView/Core/TextInputView.swift @@ -2260,7 +2260,9 @@ extension TextInputView { } private func checkVisibleSpellingIfNeeded() { - let languages = resolvedSpellingLanguages + // Language discovery can contact system services. Disabled spelling only + // needs to clear an existing request, without discovering its languages. + let languages = isSpellCheckingEnabled ? resolvedSpellingLanguages : [] guard isSpellCheckingEnabled, !languages.isEmpty, !layoutManager.visibleTextRanges.isEmpty else { if lastSpellingRequest != nil { diff --git a/Sources/Runestone/TextView/LineController/LineFragmentRenderer.swift b/Sources/Runestone/TextView/LineController/LineFragmentRenderer.swift index 3b4ac53c4..0eb485178 100644 --- a/Sources/Runestone/TextView/LineController/LineFragmentRenderer.swift +++ b/Sources/Runestone/TextView/LineController/LineFragmentRenderer.swift @@ -43,6 +43,10 @@ final class LineFragmentRenderer { /// A horizontally scrolled viewport can cover disjoint logical ranges in bidirectional text. func visibleCharacterRanges(in horizontalRange: ClosedRange) -> [NSRange] { + Self.visibleCharacterRanges(in: horizontalRange, lineFragment: lineFragment) + } + + static func visibleCharacterRanges(in horizontalRange: ClosedRange, lineFragment: LineFragment) -> [NSRange] { let runs = CTLineGetGlyphRuns(lineFragment.line) as? [CTRun] ?? [] return runs.compactMap { run in guard let bounds = horizontalBounds(of: run), bounds.overlaps(horizontalRange) else { @@ -77,7 +81,7 @@ final class LineFragmentRenderer { let stringRange = CTRunGetStringRange(run) let runRange = NSRange(location: stringRange.location, length: stringRange.length) let intersection = NSIntersectionRange(visibleRange, runRange) - guard intersection.length > 0, let bounds = horizontalBounds(of: run) else { + guard intersection.length > 0, let bounds = Self.horizontalBounds(of: run) else { return nil } let runMinX = bounds.lowerBound @@ -134,7 +138,7 @@ private extension LineFragmentRenderer { } } - private func horizontalBounds(of run: CTRun) -> ClosedRange? { + private static func horizontalBounds(of run: CTRun) -> ClosedRange? { let count = CTRunGetGlyphCount(run) guard count > 0 else { return nil diff --git a/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterHighlightRequest.swift b/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterHighlightRequest.swift new file mode 100644 index 000000000..a153107c7 --- /dev/null +++ b/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterHighlightRequest.swift @@ -0,0 +1,61 @@ +import Foundation + +/// Owns document-sized resources while work is pending or awaiting main-thread +/// application. Queued closures hold this request weakly so cancellation can +/// release a completed snapshot without waiting for the main queue to drain. +final class TreeSitterHighlightRequest { + struct Completed { + let snapshot: TreeSitterHighlightSnapshot + let captures: [TreeSitterCapture] + } + + private let lock = NSLock() + private let resourceDisposer: TreeSitterHighlightResourceDisposer + private var pendingSnapshot: TreeSitterHighlightSnapshot? + private var completed: Completed? + private var isCancelled = false + + init(snapshot: TreeSitterHighlightSnapshot, resourceDisposer: TreeSitterHighlightResourceDisposer = .shared) { + pendingSnapshot = snapshot + self.resourceDisposer = resourceDisposer + } + + func takeSnapshot() -> TreeSitterHighlightSnapshot? { + lock.lock() + defer { lock.unlock() } + let snapshot = pendingSnapshot + pendingSnapshot = nil + return snapshot + } + + func complete(snapshot: TreeSitterHighlightSnapshot, captures: [TreeSitterCapture]) { + lock.lock() + defer { lock.unlock() } + guard !isCancelled else { + return + } + completed = Completed(snapshot: snapshot, captures: captures) + } + + func takeCompleted() -> Completed? { + lock.lock() + defer { lock.unlock() } + let result = completed + completed = nil + return result + } + + func cancel() { + lock.lock() + isCancelled = true + guard pendingSnapshot != nil || completed != nil else { + lock.unlock() + return + } + let resources = TreeSitterHighlightResources(snapshot: pendingSnapshot, completed: completed) + pendingSnapshot = nil + completed = nil + lock.unlock() + resourceDisposer.dispose(resources) + } +} diff --git a/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterHighlightResourceDisposer.swift b/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterHighlightResourceDisposer.swift new file mode 100644 index 000000000..f61ac595e --- /dev/null +++ b/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterHighlightResourceDisposer.swift @@ -0,0 +1,63 @@ +import Foundation + +/// The wrapper owns canceled resources exclusively. Clearing its fields releases +/// them on the selected executor even if the wrapper itself still has a caller. +final class TreeSitterHighlightResources { + private var snapshot: TreeSitterHighlightSnapshot? + private var completed: TreeSitterHighlightRequest.Completed? + + init(snapshot: TreeSitterHighlightSnapshot?, completed: TreeSitterHighlightRequest.Completed?) { + self.snapshot = snapshot + self.completed = completed + } + + func clear() { + snapshot = nil + completed = nil + } +} + +/// At most one canceled payload may await or undergo background destruction. +/// Bursts apply synchronous backpressure instead of retaining a queue of old +/// documents. This queue never owns an executing query's snapshot. +final class TreeSitterHighlightResourceDisposer { + static let shared = TreeSitterHighlightResourceDisposer(queue: { + let queue = OperationQueue() + queue.name = "Runestone.HighlightResourceDisposal" + queue.qualityOfService = .userInitiated + queue.maxConcurrentOperationCount = 1 + return queue + }()) + + private let queue: OperationQueue + private let lock = NSLock() + private var isDisposing = false + + // The executor must remain active. Production owns a dedicated private queue; + // injection lets lifetime tests suspend and drain their own isolated queue. + init(queue: OperationQueue) { + self.queue = queue + } + + func dispose(_ resources: TreeSitterHighlightResources) { + lock.lock() + let canDisposeAsynchronously = !isDisposing + if canDisposeAsynchronously { isDisposing = true } + lock.unlock() + + guard canDisposeAsynchronously else { + resources.clear() + return + } + queue.addOperation { [self, resources] in + autoreleasepool { + resources.clear() + } + // Release the slot only after the heavy fields have been cleared; + // the operation closure can outlive this point without retaining them. + lock.lock() + isDisposing = false + lock.unlock() + } + } +} diff --git a/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterLanguageLayer.swift b/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterLanguageLayer.swift index c2bae2196..cefb31e4c 100644 --- a/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterLanguageLayer.swift +++ b/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterLanguageLayer.swift @@ -196,7 +196,7 @@ private extension TreeSitterLanguageLayer { injectionsQueryCursor.execute() let captures = injectionsQueryCursor.validCaptures(in: stringView) let injectedLanguages = injectedLanguages(from: captures) - let capturedIDs = injectedLanguages.map(\.id) + let capturedIDs = Set(injectedLanguages.map(\.id)) let currentIDs = childLanguageLayerStore.allIDs for id in currentIDs { if !capturedIDs.contains(id) { diff --git a/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterSyntaxHighlighter.swift b/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterSyntaxHighlighter.swift index 4b8f54147..bd2bf878d 100644 --- a/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterSyntaxHighlighter.swift +++ b/Sources/Runestone/TextView/SyntaxHighlighting/Internal/TreeSitter/TreeSitterSyntaxHighlighter.swift @@ -27,6 +27,7 @@ final class TreeSitterSyntaxHighlighter: LineSyntaxHighlighter { private let languageMode: TreeSitterInternalLanguageMode private let operationQueue: OperationQueue private var currentOperation: Operation? + private var currentRequest: TreeSitterHighlightRequest? init(stringView: StringView, languageMode: TreeSitterInternalLanguageMode, operationQueue: OperationQueue) { self.stringView = stringView @@ -34,6 +35,10 @@ final class TreeSitterSyntaxHighlighter: LineSyntaxHighlighter { self.operationQueue = operationQueue } + deinit { + cancel() + } + func syntaxHighlight(_ input: LineSyntaxHighlighterInput) { cancel() let captures = languageMode.captures(in: input.byteRange) @@ -43,55 +48,60 @@ final class TreeSitterSyntaxHighlighter: LineSyntaxHighlighter { func syntaxHighlight(_ input: LineSyntaxHighlighterInput, completion: @escaping AsyncCallback) { cancel() - let snapshot = languageMode.makeHighlightSnapshot() + let request = TreeSitterHighlightRequest(snapshot: languageMode.makeHighlightSnapshot()) let operation = BlockOperation() - operation.addExecutionBlock { [weak operation, weak self] in - guard let operation = operation, let self = self else { - DispatchQueue.main.async { - completion(.failure(TreeSitterSyntaxHighlighterError.operationDeallocated)) - } - return - } - guard !operation.isCancelled else { - DispatchQueue.main.async { - completion(.failure(TreeSitterSyntaxHighlighterError.cancelled)) - } + operation.addExecutionBlock { [weak operation, weak request] in + guard let operation = operation, let request = request, !operation.isCancelled, + let snapshot = request.takeSnapshot() else { return } let captures = snapshot.captures(in: input.byteRange) { operation.isCancelled } if !operation.isCancelled { - DispatchQueue.main.async { - defer { - if self.currentOperation === operation { self.currentOperation = nil } - } - if self.currentOperation === operation && !operation.isCancelled && input.isValid() { - let tokens = self.tokens(for: captures, localTo: input.byteRange) - guard self.currentOperation === operation && !operation.isCancelled && input.isValid() else { - completion(.failure(TreeSitterSyntaxHighlighterError.cancelled)) - return - } - let result = withExtendedLifetime(snapshot) { - self.setAttributes(for: tokens, on: input.attributedString) - } - completion(.success(result)) - } else { - completion(.failure(TreeSitterSyntaxHighlighterError.cancelled)) - } + request.complete(snapshot: snapshot, captures: captures) + } + } + // Operation completion also runs when canceled before its execution + // block starts, so every request still delivers one completion callback. + operation.completionBlock = { [weak operation, weak request, weak self] in + if operation?.isCancelled != false { request?.cancel() } + DispatchQueue.main.async { [weak operation, weak request, weak self] in + guard let self = self, let operation = operation else { + completion(.failure(TreeSitterSyntaxHighlighterError.operationDeallocated)) + return } - } else { - DispatchQueue.main.async { + defer { if self.currentOperation === operation { self.currentOperation = nil } + if self.currentRequest === request { self.currentRequest = nil } + request?.cancel() + } + guard let request = request, self.currentRequest === request, + self.currentOperation === operation, !operation.isCancelled, input.isValid(), + let completed = request.takeCompleted() else { completion(.failure(TreeSitterSyntaxHighlighterError.cancelled)) + return + } + withExtendedLifetime(completed.snapshot) { + let tokens = self.tokens(for: completed.captures, localTo: input.byteRange) + guard self.currentRequest === request, self.currentOperation === operation, + !operation.isCancelled, input.isValid() else { + completion(.failure(TreeSitterSyntaxHighlighterError.cancelled)) + return + } + let result = self.setAttributes(for: tokens, on: input.attributedString) + completion(.success(result)) } } } currentOperation = operation + currentRequest = request operationQueue.addOperation(operation) } func cancel() { currentOperation?.cancel() currentOperation = nil + currentRequest?.cancel() + currentRequest = nil } } diff --git a/Sources/Runestone/TreeSitter/TreeSitterTree.swift b/Sources/Runestone/TreeSitter/TreeSitterTree.swift index 4f2ea0347..8ed2121ff 100644 --- a/Sources/Runestone/TreeSitter/TreeSitterTree.swift +++ b/Sources/Runestone/TreeSitter/TreeSitterTree.swift @@ -1,5 +1,6 @@ import Foundation import TreeSitter +import TreeSitterSupport final class TreeSitterTree { let pointer: OpaquePointer @@ -26,6 +27,11 @@ final class TreeSitterTree { } func rangesChanged(comparingTo otherTree: TreeSitterTree) -> [TreeSitterTextRange] { + // A bounded proof can avoid expanding shared repetition subtrees after + // syntax-preserving edits. Every uncertain case uses the stock diff. + if runestone_trees_have_same_structure(pointer, otherTree.pointer) { + return [] + } var count = CUnsignedInt(0) let ptr = ts_tree_get_changed_ranges(pointer, otherTree.pointer, &count) defer { free(ptr) } diff --git a/Sources/TreeSitterSupport/TreeSitterSupport.c b/Sources/TreeSitterSupport/TreeSitterSupport.c new file mode 100644 index 000000000..f4f3b4b7e --- /dev/null +++ b/Sources/TreeSitterSupport/TreeSitterSupport.c @@ -0,0 +1,94 @@ +#include "include/TreeSitterSupport.h" + +// This proof deliberately uses the private structures of the pinned Tree-sitter +// submodule. Keep it conservative when updating that dependency. It neither +// changes trees nor replaces Tree-sitter's algorithm for structural changes. +#include "../../tree-sitter/lib/src/tree.h" + +typedef struct { + Subtree old_tree; + Subtree new_tree; +} TreePair; + +static bool points_equal(TSPoint left, TSPoint right) { + return left.row == right.row && left.column == right.column; +} + +static bool lengths_equal(Length left, Length right) { + return left.bytes == right.bytes && points_equal(left.extent, right.extent); +} + +bool runestone_trees_have_same_structure(const TSTree *old_tree, const TSTree *new_tree) { + if (!old_tree || !new_tree || old_tree->language != new_tree->language || + old_tree->included_range_count != new_tree->included_range_count) { + return false; + } + + // Included-range changes are independently reported by the stock algorithm, + // even when the resulting syntax trees would otherwise look identical. + for (unsigned i = 0; i < old_tree->included_range_count; i++) { + TSRange old_range = old_tree->included_ranges[i]; + TSRange new_range = new_tree->included_ranges[i]; + if (old_range.start_byte != new_range.start_byte || old_range.end_byte != new_range.end_byte || + !points_equal(old_range.start_point, new_range.start_point) || + !points_equal(old_range.end_point, new_range.end_point)) { + return false; + } + } + + // Recovery and external scanner state have additional comparison semantics. + // Leave them entirely to Tree-sitter, including when a subtree is shared. + if (ts_subtree_error_cost(old_tree->root) || ts_subtree_error_cost(new_tree->root) || + ts_subtree_has_external_tokens(old_tree->root) || ts_subtree_has_external_tokens(new_tree->root)) { + return false; + } + + // Compare raw syntax nodes instead of expanding the visible cursor state. + // Shared immutable repetition subtrees can be skipped altogether. Some + // parsers rebuild repetition nodes, so also allow a bounded linear proof. + // Different topology, deep trees, or excessive work simply use the fallback. + enum { stack_capacity = 256, comparison_limit = 4000000 }; + TreePair pending[stack_capacity]; + unsigned pending_count = 1; + unsigned comparisons = 0; + pending[0] = (TreePair){old_tree->root, new_tree->root}; + while (pending_count > 0) { + if (++comparisons > comparison_limit) { + return false; + } + TreePair pair = pending[--pending_count]; + Subtree old = pair.old_tree; + Subtree new = pair.new_tree; + uint32_t child_count = ts_subtree_child_count(old); + if (ts_subtree_symbol(old) != ts_subtree_symbol(new) || + ts_subtree_production_id(old) != ts_subtree_production_id(new) || + ts_subtree_parse_state(old) != ts_subtree_parse_state(new) || + ts_subtree_extra(old) != ts_subtree_extra(new) || + ts_subtree_visible(old) != ts_subtree_visible(new) || + ts_subtree_named(old) != ts_subtree_named(new) || + ts_subtree_missing(old) || ts_subtree_missing(new) || + ts_subtree_is_error(old) || ts_subtree_is_error(new) || + child_count != ts_subtree_child_count(new) || + !lengths_equal(ts_subtree_padding(old), ts_subtree_padding(new)) || + !lengths_equal(ts_subtree_size(old), ts_subtree_size(new))) { + return false; + } + // Identical parent production IDs preserve the alias context of every + // child. With identical geometry, shared immutable children need no scan. + if (!old.data.is_inline && !new.data.is_inline && old.ptr == new.ptr) { + continue; + } + if (child_count > stack_capacity - pending_count) { + return false; + } + for (uint32_t i = 0; i < child_count; i++) { + pending[pending_count++] = (TreePair){ts_subtree_children(old)[i], ts_subtree_children(new)[i]}; + } + } + + // The edited old tree can retain has_changes markers even when every token, + // alias and coordinate matches the new tree. The ordinary comparator then + // descends to equivalent leaves and produces no range. The proof above does + // the same without expanding shared hidden repetition subtrees into tokens. + return true; +} diff --git a/Sources/TreeSitterSupport/include/TreeSitterSupport.h b/Sources/TreeSitterSupport/include/TreeSitterSupport.h new file mode 100644 index 000000000..5b0fa3a4a --- /dev/null +++ b/Sources/TreeSitterSupport/include/TreeSitterSupport.h @@ -0,0 +1,11 @@ +#ifndef RUNESTONE_TREE_SITTER_SUPPORT_H +#define RUNESTONE_TREE_SITTER_SUPPORT_H + +#include +#include + +/// A conservative proof of identical syntax structure, not a comparison of text. +/// False means callers must use Tree-sitter's ordinary changed-range algorithm. +bool runestone_trees_have_same_structure(const TSTree *old_tree, const TSTree *new_tree); + +#endif diff --git a/Tests/RunestoneTests/LargeFileCorrectnessTests.swift b/Tests/RunestoneTests/LargeFileCorrectnessTests.swift index 7ba39348d..5cae6389b 100644 --- a/Tests/RunestoneTests/LargeFileCorrectnessTests.swift +++ b/Tests/RunestoneTests/LargeFileCorrectnessTests.swift @@ -73,7 +73,8 @@ final class LargeFileCorrectnessTests: XCTestCase { try compareWithFresh(view, input: input, expected: expected, location: location, language: language) } - print("LARGE_FILE_CORRECTNESS checkpoint=\(name) long=\(longLines) wrapping=\(wrapping) utf16=\(input.string.length)") + print("LARGE_FILE_CORRECTNESS checkpoint=\(name) long=\(longLines) wrapping=\(wrapping) utf16=\(input.string.length)" + + " viewportWidth=\(view.bounds.width) viewportHeight=\(view.bounds.height)") } if asciiEligible { @@ -429,7 +430,7 @@ private extension LargeFileCorrectnessTests { } private func makeView(wrapping: Bool) -> TextView { - let view = TextView(frame: CGRect(x: 0, y: 0, width: 390, height: 300)) + let view = TextView(frame: LargeFileTestViewport.frame(defaultSize: CGSize(width: 390, height: 300))) view.contentInsetAdjustmentBehavior = .never view.isLineWrappingEnabled = wrapping view.isAutomaticScrollEnabled = false diff --git a/Tests/RunestoneTests/LargeFilePerformanceTestCase.swift b/Tests/RunestoneTests/LargeFilePerformanceTestCase.swift index 5c6312acd..8dbf39dc2 100644 --- a/Tests/RunestoneTests/LargeFilePerformanceTestCase.swift +++ b/Tests/RunestoneTests/LargeFilePerformanceTestCase.swift @@ -14,7 +14,7 @@ class LargeFilePerformanceTestCase: XCTestCase { let lineKind = unbroken ? "long-unbroken" : (longLines ? "long" : "short") let prefix = purePlainText ? "pure-ascii-" : "" let scenario = "\(prefix)\(lineKind)-\(highlighted ? "json" : "plain")-\(wrapping ? "wrapped" : "unwrapped")" - let view = TextView(frame: CGRect(x: 0, y: 0, width: 390, height: 700)) + let view = TextView(frame: LargeFileTestViewport.frame(defaultSize: CGSize(width: 390, height: 700))) view.contentInsetAdjustmentBehavior = .never view.isLineWrappingEnabled = wrapping view.isAutomaticScrollEnabled = false @@ -45,7 +45,8 @@ class LargeFilePerformanceTestCase: XCTestCase { let loadMilliseconds = milliseconds(since: loadStart) report(scenario: scenario, phase: "load", samples: [loadMilliseconds], extra: ["utf8Bytes": fixture.utf8.count, "logicalLines": input.lineManager.lineCount, - "maximumLineUTF8Bytes": maximumLineUTF8Bytes(in: fixture)]) + "maximumLineUTF8Bytes": maximumLineUTF8Bytes(in: fixture), + "viewportWidth": Double(view.bounds.width), "viewportHeight": Double(view.bounds.height)]) XCTAssertGreaterThanOrEqual(fixture.utf8.count, 25 * 1_024 * 1_024) XCTAssertEqual(input.string.length, fixture.utf16.count) diff --git a/Tests/RunestoneTests/LargeFileTestViewport.swift b/Tests/RunestoneTests/LargeFileTestViewport.swift new file mode 100644 index 000000000..fbf858aa8 --- /dev/null +++ b/Tests/RunestoneTests/LargeFileTestViewport.swift @@ -0,0 +1,11 @@ +import UIKit + +/// Keep the original benchmark geometry by default. Opt in separately to screen +/// dimensions when checking tablet-sized layout; those timings are a new series. +@MainActor +enum LargeFileTestViewport { + static func frame(defaultSize: CGSize) -> CGRect { + let useScreen = ProcessInfo.processInfo.environment["RUNESTONE_PERFORMANCE_SCREEN_VIEWPORT"] == "1" + return CGRect(origin: .zero, size: useScreen ? UIScreen.main.bounds.size : defaultSize) + } +} diff --git a/Tests/RunestoneTests/LargeFileTraversalTests.swift b/Tests/RunestoneTests/LargeFileTraversalTests.swift index 1124904e0..04755762f 100644 --- a/Tests/RunestoneTests/LargeFileTraversalTests.swift +++ b/Tests/RunestoneTests/LargeFileTraversalTests.swift @@ -22,7 +22,7 @@ final class LargeFileTraversalTests: XCTestCase { "Set RUNESTONE_PERFORMANCE=1 in the test runner to run distributed traversal.") let fixture = makeFixture(lineLength: longLines ? 60_000 : 128) let scenario = "\(longLines ? "long" : "short")-json-\(wrapping ? "wrapped" : "unwrapped")" - let view = TextView(frame: CGRect(x: 0, y: 0, width: 390, height: 700)) + let view = TextView(frame: LargeFileTestViewport.frame(defaultSize: CGSize(width: 390, height: 700))) view.contentInsetAdjustmentBehavior = .never view.isLineWrappingEnabled = wrapping view.isAutomaticScrollEnabled = false @@ -206,6 +206,8 @@ final class LargeFileTraversalTests: XCTestCase { result["offsetY"] = Double(view.contentOffset.y) result["contentWidth"] = Double(view.contentSize.width) result["contentHeight"] = Double(view.contentSize.height) + result["viewportWidth"] = Double(view.bounds.width) + result["viewportHeight"] = Double(view.bounds.height) result["visibleFragmentCount"] = visibleFragments(in: view).count if let data = try? JSONSerialization.data(withJSONObject: result, options: [.sortedKeys]), let json = String(data: data, encoding: .utf8) { diff --git a/Tests/RunestoneTests/LineFragmentSpellingTests.swift b/Tests/RunestoneTests/LineFragmentSpellingTests.swift index 2817328a3..c218e9629 100644 --- a/Tests/RunestoneTests/LineFragmentSpellingTests.swift +++ b/Tests/RunestoneTests/LineFragmentSpellingTests.swift @@ -50,6 +50,52 @@ final class LineFragmentSpellingTests: XCTestCase { XCTAssertEqual(segment.upperBound - segment.lowerBound, width(of: "אבגד"), accuracy: 0.5) } + @MainActor + func testVisibleSpellingRangesFollowCompletedLayoutAcrossEditsScrollReflowAndFolding() { + let text = String(repeating: "office אבגד 😀 words ", count: 40) + "\nsecond visible paragraph" + let fixture = SpellingLayoutFixture(text: text) + fixture.layoutNow() + // Do not read the ranges before editing. The eventual request still + // belongs to the completed layout, including its old document offsets. + let beforeEdit = fixture.eagerRanges() + XCTAssertFalse(beforeEdit.isEmpty) + let edit = TextEditHelper(stringView: fixture.stringView, lineManager: fixture.lineManager, lineEndings: .lf) + .replaceText(in: NSRange(location: 0, length: 0), with: "inserted paragraph\n") + fixture.layout.redisplayLines(withIDs: Set(edit.lineChangeSet.editedLines.map(\.id))) + XCTAssertEqual(fixture.layout.visibleTextRanges, beforeEdit) + + fixture.layoutNow() + XCTAssertEqual(fixture.layout.visibleTextRanges, fixture.eagerRanges()) + let leftRanges = fixture.layout.visibleTextRanges + fixture.layout.viewport.origin.x = 300 + fixture.layoutNow() + XCTAssertEqual(fixture.layout.visibleTextRanges, fixture.eagerRanges()) + XCTAssertNotEqual(fixture.layout.visibleTextRanges, leftRanges, + "Spelling must follow the newly exposed columns rather than a previous horizontal viewport.") + + fixture.layout.isLineWrappingEnabled = true + fixture.layout.scrollViewWidth = 130 + fixture.layout.viewport = CGRect(x: 0, y: 0, width: 130, height: 90) + fixture.layoutNow() + XCTAssertEqual(fixture.layout.visibleTextRanges, fixture.eagerRanges()) + let hiddenLine = fixture.lineManager.line(atRow: 1) + let hiddenRange = NSRange(location: hiddenLine.location, length: hiddenLine.data.totalLength) + fixture.lineManager.setCollapsedLineRanges([1...1]) + fixture.layoutNow() + XCTAssertEqual(fixture.layout.visibleTextRanges, fixture.eagerRanges()) + XCTAssertTrue(fixture.layout.visibleTextRanges.allSatisfy { NSIntersectionRange($0, hiddenRange).length == 0 }, + "Folded text must not remain in a previous layout's spelling request.") + + fixture.layout.viewport = .zero + fixture.layoutNow() + XCTAssertTrue(fixture.layout.visibleTextRanges.isEmpty) + XCTAssertNil(fixture.layout.visibleTextRange) + fixture.layout.viewport = CGRect(x: 0, y: 0, width: 130, height: 90) + fixture.layoutNow() + XCTAssertEqual(fixture.layout.visibleTextRanges, fixture.eagerRanges()) + XCTAssertFalse(fixture.layout.visibleTextRanges.isEmpty) + } + private func renderer(string: String, range: NSRange) -> LineFragmentRenderer { let attributedString = NSAttributedString(string: string, attributes: [.font: UIFont.systemFont(ofSize: 20)]) let typesetter = CTTypesetterCreateWithAttributedString(attributedString) @@ -71,3 +117,75 @@ final class LineFragmentSpellingTests: XCTestCase { return CGFloat(CTLineGetTypographicBounds(CTLineCreateWithAttributedString(attributedString), nil, nil, nil)) } } + +@MainActor +private final class SpellingLayoutFixture { + let stringView: StringView + let lineManager: LineManager + let layout: LayoutManager + private let parent = UIView(frame: CGRect(x: 0, y: 0, width: 180, height: 90)) + + init(text: String) { + let stringView = StringView(string: text) + let lineManager = LineManager(stringView: stringView) + lineManager.rebuild() + let configuration = InvisibleCharacterConfiguration() + let highlights = HighlightService(lineManager: lineManager) + let factory = LineControllerFactory(stringView: stringView, highlightService: highlights, + invisibleCharacterConfiguration: configuration) + let storage = LineControllerStorage(stringView: stringView, lineControllerFactory: factory) + let gutter = GutterWidthService(lineManager: lineManager) + let contentSize = ContentSizeService(lineManager: lineManager, lineControllerStorage: storage, + gutterWidthService: gutter, invisibleCharacterConfiguration: configuration) + let caret = CaretRectService(stringView: stringView, lineManager: lineManager, + lineControllerStorage: storage, gutterWidthService: gutter) + let selection = SelectionRectService(lineManager: lineManager, contentSizeService: contentSize, + gutterWidthService: gutter, caretRectService: caret) + self.stringView = stringView + self.lineManager = lineManager + self.layout = LayoutManager(lineManager: lineManager, languageMode: PlainTextInternalLanguageMode(), + stringView: stringView, lineControllerStorage: storage, contentSizeService: contentSize, + gutterWidthService: gutter, caretRectService: caret, + selectionRectService: selection, highlightService: highlights, + invisibleCharacterConfiguration: configuration) + layout.textInputView = parent + layout.isLineWrappingEnabled = false + layout.scrollViewWidth = parent.bounds.width + layout.viewport = parent.bounds + contentSize.scrollViewWidth = parent.bounds.width + } + + func layoutNow() { + layout.setNeedsLayout() + layout.layoutIfNeeded() + } + + /// Read the actual displayed drawing objects, independently of the saved + /// spelling ranges, using the same directional Core Text mapping as before. + func eagerRanges() -> [NSRange] { + displayedFragments(in: parent).sorted { left, right in + if left.documentLineLocation != right.documentLineLocation { + return left.documentLineLocation < right.documentLineLocation + } + return (left.renderer?.lineFragment.index ?? 0) < (right.renderer?.lineFragment.index ?? 0) + }.flatMap { view -> [NSRange] in + guard let renderer = view.renderer else { + return [] + } + let fragment = renderer.lineFragment + let ranges = layout.isLineWrappingEnabled ? [fragment.visibleRange] + : LineFragmentRenderer.visibleCharacterRanges( + in: (layout.viewport.minX - 32) ... (layout.viewport.maxX + 32), lineFragment: fragment) + return ranges.map { NSRange(location: view.documentLineLocation + $0.location, length: $0.length) } + } + } + + private func displayedFragments(in view: UIView) -> [LineFragmentView] { + view.subviews.flatMap { child -> [LineFragmentView] in + if let fragment = child as? LineFragmentView { + return [fragment] + } + return displayedFragments(in: child) + } + } +} diff --git a/Tests/RunestoneTests/TreeSitterChangedRangeTests.swift b/Tests/RunestoneTests/TreeSitterChangedRangeTests.swift new file mode 100644 index 000000000..32cae3fa8 --- /dev/null +++ b/Tests/RunestoneTests/TreeSitterChangedRangeTests.swift @@ -0,0 +1,181 @@ +@testable import Runestone +import TestTreeSitterLanguages +import TreeSitter +import TreeSitterSupport +import XCTest + +final class TreeSitterChangedRangeTests: XCTestCase { + func testSharedJSONRepetitionsKeepStockChangedRangesForTokenAndStructuralEdits() throws { + let text = "[\n" + (0..<2_000).map { " \"value\($0)\"" }.joined(separator: ",\n") + "\n]" + for replacement in ["value1000x", "Grøn 😀", "value\n1000", "value1000\", 42, \"added", ""] { + try compareEdit(text, language: tree_sitter_json(), replacing: "value1000", with: replacement) + } + // Exercise the accelerated path as well as differential correctness. A + // shared balanced repetition keeps ordinary character typing bounded. + let pair = try editedTrees(text, language: tree_sitter_json(), replacing: "value1000", with: "value1000x") + XCTAssertTrue(runestone_trees_have_same_structure(pair.old.pointer, pair.new.pointer)) + XCTAssertTrue(pair.old.rangesChanged(comparingTo: pair.new).isEmpty) + } + + func testJavaScriptAliasesUnicodeAndRecoveryKeepStockChangedRanges() throws { + let text = "const object = { target: \"café 😀\", count: 42 };\nobject.target;" + for (needle, replacement) in [ + ("target", "renamed"), + ("😀", "🧑‍💻"), + ("42", "true"), + ("42", "{ value: 42 }"), + ("café 😀", "unterminated\" +"), + ("object.target;", "") + ] { + try compareEdit(text, language: tree_sitter_javascript(), replacing: needle, with: replacement) + } + } + + func testExternalScannerHTMLAndBoundaryEditsKeepStockChangedRanges() throws { + let html = "

café 😀

" + for (needle, replacement) in [("42", "43"), ("\r\n

😀

") + ] + for fixture in fixtures { + try runMutationSequence(fixture.text, language: fixture.language) + } + } + + private func runMutationSequence(_ original: String, language: OpaquePointer) throws { + let stringView = StringView(string: original) + let lineManager = LineManager(stringView: stringView) + lineManager.rebuild() + let parser = TreeSitterParser(encoding: .preferredUTF16) + parser.language = language + let delegate = MockTreeSitterParserDelegate() + delegate.string = stringView.string + parser.delegate = delegate + var tree = try XCTUnwrap(parser.parse()) + let editor = TextEditHelper(stringView: stringView, lineManager: lineManager, lineEndings: .lf) + var randomState: UInt64 = 0x8D31_74A2 + for step in 0..<300 { + randomState = randomState &* 6_364_136_223_846_793_005 &+ 1 + let mutation = mutation(at: step, in: stringView.string, original: original, randomState: randomState) + let change = editor.replaceText(in: mutation.range, with: mutation.text).textChange + tree.apply(TreeSitterInputEdit( + startByte: change.byteRange.location, oldEndByte: change.byteRange.upperBound, + newEndByte: change.byteRange.location + change.bytesAdded, + startPoint: TreeSitterTextPoint(change.startLinePosition), + oldEndPoint: TreeSitterTextPoint(change.oldEndLinePosition), + newEndPoint: TreeSitterTextPoint(change.newEndLinePosition))) + delegate.string = stringView.string + let new = try XCTUnwrap(parser.parse(oldTree: tree)) + assertSameChangedRanges(tree, new, context: "Mutation \(step) of \(original)") + tree = new + } + } + + private func mutation(at step: Int, in text: NSString, original: String, randomState: UInt64) + -> (range: NSRange, text: String) { + let entireText = NSRange(location: 0, length: text.length) + let randomCharacter = text.length == 0 ? NSRange(location: 0, length: 0) + : text.customRangeOfComposedCharacterSequence(at: Int(randomState % UInt64(text.length))) + switch step % 12 { + case 0, 8: + return (entireText, original) + case 1, 9: + return (text.range(of: "seed"), step % 12 == 1 ? "café 😀" : "Grøn 🧑‍💻") + case 2: + return (text.range(of: "1"), "123") + case 3: + return (NSRange(location: text.length, length: 0), "\r\n") + case 4: + return (randomCharacter, "") + case 5: + return (NSRange(location: randomCharacter.location, length: 0), "\"") + case 6: + let first = text.length == 0 ? entireText : text.customRangeOfComposedCharacterSequence(at: 0) + return (first, "") + case 7: + return (entireText, "") + case 10: + return (randomCharacter, "{{") + default: + return (NSRange(location: randomCharacter.location, length: 0), "\n") + } + } + + private func compareEdit(_ text: String, language: OpaquePointer, replacing needle: String, + with replacement: String, atEnd: Bool = false, + file: StaticString = #filePath, line: UInt = #line) throws { + let pair = try editedTrees(text, language: language, replacing: needle, with: replacement, atEnd: atEnd) + assertSameChangedRanges(pair.old, pair.new, file: file, line: line) + } + + private func editedTrees(_ text: String, language: OpaquePointer, replacing needle: String, + with replacement: String, atEnd: Bool = false) throws + -> (old: TreeSitterTree, new: TreeSitterTree) { + let stringView = StringView(string: text) + let lineManager = LineManager(stringView: stringView) + lineManager.rebuild() + let parser = TreeSitterParser(encoding: .preferredUTF16) + parser.language = language + let delegate = MockTreeSitterParserDelegate() + delegate.string = stringView.string + parser.delegate = delegate + let old = try XCTUnwrap(parser.parse()) + let range = needle.isEmpty + ? NSRange(location: atEnd ? stringView.string.length : 0, length: 0) + : stringView.string.range(of: needle) + XCTAssertNotEqual(range.location, NSNotFound) + let editor = TextEditHelper(stringView: stringView, lineManager: lineManager, lineEndings: .lf) + let change = editor.replaceText(in: range, with: replacement).textChange + old.apply(TreeSitterInputEdit( + startByte: change.byteRange.location, oldEndByte: change.byteRange.upperBound, + newEndByte: change.byteRange.location + change.bytesAdded, + startPoint: TreeSitterTextPoint(change.startLinePosition), + oldEndPoint: TreeSitterTextPoint(change.oldEndLinePosition), + newEndPoint: TreeSitterTextPoint(change.newEndLinePosition))) + delegate.string = stringView.string + return (old, try XCTUnwrap(parser.parse(oldTree: old))) + } + + private func assertSameChangedRanges(_ old: TreeSitterTree, _ new: TreeSitterTree, context: String = "", + file: StaticString = #filePath, line: UInt = #line) { + var count: UInt32 = 0 + let stock = ts_tree_get_changed_ranges(old.pointer, new.pointer, &count) + defer { free(stock) } + let expected = UnsafeBufferPointer(start: stock, count: Int(count)).map { range in + [range.start_byte, range.end_byte, range.start_point.row, range.start_point.column, + range.end_point.row, range.end_point.column] + } + let actual = old.rangesChanged(comparingTo: new).map { range in + [UInt32(range.startByte.value), UInt32(range.endByte.value), range.startPoint.row, range.startPoint.column, + range.endPoint.row, range.endPoint.column] + } + XCTAssertEqual(actual, expected, context, file: file, line: line) + } +} diff --git a/Tests/RunestoneTests/TreeSitterHighlightRequestTests.swift b/Tests/RunestoneTests/TreeSitterHighlightRequestTests.swift new file mode 100644 index 000000000..dc445a6f9 --- /dev/null +++ b/Tests/RunestoneTests/TreeSitterHighlightRequestTests.swift @@ -0,0 +1,105 @@ +@testable import Runestone +import TestTreeSitterLanguages +import XCTest + +final class TreeSitterHighlightRequestTests: XCTestCase { + func testCancellationReleasesAQueuedDocumentSnapshotBeforeWorkStarts() throws { + let disposalQueue = OperationQueue() + let disposer = TreeSitterHighlightResourceDisposer(queue: disposalQueue) + weak var weakSnapshot: TreeSitterHighlightSnapshot? + let request = try autoreleasepool { + let snapshot = try makeSnapshot() + weakSnapshot = snapshot + return TreeSitterHighlightRequest(snapshot: snapshot, resourceDisposer: disposer) + } + XCTAssertNotNil(weakSnapshot) + request.cancel() + disposalQueue.waitUntilAllOperationsAreFinished() + XCTAssertNil(weakSnapshot, "Canceling queued work must promptly release its document-sized snapshot.") + XCTAssertNil(request.takeSnapshot()) + } + + func testCancellationReleasesCompletedCapturesBeforeMainThreadApplication() throws { + let disposalQueue = OperationQueue() + let disposer = TreeSitterHighlightResourceDisposer(queue: disposalQueue) + weak var weakSnapshot: TreeSitterHighlightSnapshot? + let request = try autoreleasepool { + let snapshot = try makeSnapshot() + weakSnapshot = snapshot + let request = TreeSitterHighlightRequest(snapshot: snapshot, resourceDisposer: disposer) + let workerSnapshot = try XCTUnwrap(request.takeSnapshot()) + let captures = workerSnapshot.captures(in: ByteRange(location: 0, length: 100)) + XCTAssertFalse(captures.isEmpty) + request.complete(snapshot: workerSnapshot, captures: captures) + return request + } + XCTAssertNotNil(weakSnapshot, "Completed captures must retain the trees backing their unsafe node references.") + request.cancel() + disposalQueue.waitUntilAllOperationsAreFinished() + XCTAssertNil(weakSnapshot, "Canceled main-queue applications must not retain an obsolete full document.") + XCTAssertNil(request.takeCompleted()) + } + + func testCancellationDuringQueryRejectsLateCompletionWithoutReleasingActiveWorkerSnapshot() throws { + let disposalQueue = OperationQueue() + let disposer = TreeSitterHighlightResourceDisposer(queue: disposalQueue) + weak var weakSnapshot: TreeSitterHighlightSnapshot? + let request = try autoreleasepool { + let snapshot = try makeSnapshot() + weakSnapshot = snapshot + return TreeSitterHighlightRequest(snapshot: snapshot, resourceDisposer: disposer) + } + try autoreleasepool { + let workerSnapshot = try XCTUnwrap(request.takeSnapshot()) + request.cancel() + let captures = workerSnapshot.captures(in: ByteRange(location: 0, length: 100)) + XCTAssertFalse(captures.isEmpty, "An executing query must keep its tree alive until it returns.") + request.complete(snapshot: workerSnapshot, captures: captures) + XCTAssertNil(request.takeCompleted(), "Cancellation must reject results published by an already-running worker.") + } + XCTAssertNil(weakSnapshot) + } + + func testCancellationRetainsAtMostOnePayloadWhileBackgroundDisposalIsBusy() throws { + let disposalQueue = OperationQueue() + disposalQueue.isSuspended = true + defer { + disposalQueue.isSuspended = false + disposalQueue.waitUntilAllOperationsAreFinished() + } + let disposer = TreeSitterHighlightResourceDisposer(queue: disposalQueue) + weak var firstSnapshot: TreeSitterHighlightSnapshot? + let first = try autoreleasepool { + let snapshot = try makeSnapshot() + firstSnapshot = snapshot + return TreeSitterHighlightRequest(snapshot: snapshot, resourceDisposer: disposer) + } + first.cancel() + XCTAssertNotNil(firstSnapshot, "One canceled payload may await background destruction.") + + weak var secondSnapshot: TreeSitterHighlightSnapshot? + let second = try autoreleasepool { + let snapshot = try makeSnapshot() + secondSnapshot = snapshot + let request = TreeSitterHighlightRequest(snapshot: snapshot, resourceDisposer: disposer) + let workerSnapshot = try XCTUnwrap(request.takeSnapshot()) + request.complete(snapshot: workerSnapshot, captures: workerSnapshot.captures(in: ByteRange(location: 0, length: 100))) + return request + } + second.cancel() + XCTAssertNil(secondSnapshot, "A busy disposer must release another completed payload synchronously.") + XCTAssertNotNil(firstSnapshot) + disposalQueue.isSuspended = false + disposalQueue.waitUntilAllOperationsAreFinished() + XCTAssertNil(firstSnapshot, "Background cleanup must release its payload without a main-queue callback.") + } + + private func makeSnapshot() throws -> TreeSitterHighlightSnapshot { + let parser = TreeSitterParser(encoding: .preferredUTF16) + parser.language = tree_sitter_json() + let text = "[1, 2, 3]" + let tree = try XCTUnwrap(parser.parse(text as NSString)) + let query = try TreeSitterQuery(source: "(number) @number", language: tree_sitter_json()) + return TreeSitterHighlightSnapshot(layers: [.init(tree: tree, query: query)], stringView: StringView(string: text)) + } +} diff --git a/Tests/RunestoneTests/TreeSitterSyntaxHighlightingTests.swift b/Tests/RunestoneTests/TreeSitterSyntaxHighlightingTests.swift index 7133d407c..90a33a4a8 100644 --- a/Tests/RunestoneTests/TreeSitterSyntaxHighlightingTests.swift +++ b/Tests/RunestoneTests/TreeSitterSyntaxHighlightingTests.swift @@ -4,6 +4,60 @@ import UIKit import XCTest final class TreeSitterSyntaxHighlightingTests: XCTestCase { + func testIncrementalEditsPreserveSurvivingInjectionsAndRemoveDeletedRegions() { + let javaScript = TreeSitterLanguage(tree_sitter_javascript(), highlightsQuery: .init(string: "(number) @number")) + let html = TreeSitterLanguage(tree_sitter_html(), highlightsQuery: .init(string: "(tag_name) @type"), + injectionsQuery: .init(string: """ + ((script_element (raw_text) @injection.content) (#set! injection.language "javascript")) + """)) + let stringView = StringView(string: """ + + + + """) + let lineManager = LineManager(stringView: stringView) + lineManager.rebuild() + let parser = TreeSitterParser(encoding: .preferredUTF16) + let delegate = MockTreeSitterParserDelegate() + parser.delegate = delegate + let provider = JavaScriptLanguageProvider(language: javaScript) + let layer = TreeSitterLanguageLayer(language: html.internalLanguage, languageProvider: provider, + parser: parser, stringView: stringView, lineManager: lineManager) + layer.parse(stringView.string, predicateStringView: stringView) + let editor = TextEditHelper(stringView: stringView, lineManager: lineManager, lineEndings: .lf) + + func numbers() -> [String] { + layer.captures(in: ByteRange(utf16Range: NSRange(location: 0, length: stringView.string.length))) + .filter { $0.name == "number" } + .map { stringView.string.substring(with: NSRange($0.byteRange)) } + } + + func replace(_ text: String, with replacement: String) { + let range = stringView.string.range(of: text) + XCTAssertNotEqual(range.location, NSNotFound) + guard range.location != NSNotFound else { + return + } + let change = editor.replaceText(in: range, with: replacement).textChange + delegate.string = stringView.string + _ = layer.apply(TreeSitterInputEdit( + startByte: change.byteRange.location, + oldEndByte: change.byteRange.upperBound, + newEndByte: change.byteRange.location + change.bytesAdded, + startPoint: TreeSitterTextPoint(change.startLinePosition), + oldEndPoint: TreeSitterTextPoint(change.oldEndLinePosition), + newEndPoint: TreeSitterTextPoint(change.newEndLinePosition))) + } + + XCTAssertEqual(numbers(), ["1", "2", "3"]) + replace("", with: "

outside 99

") + XCTAssertEqual(numbers(), ["1", "3"], "Deleted injections must not leave captures in ordinary HTML.") + replace("

outside 99

", with: "") + XCTAssertEqual(numbers(), ["1", "4", "3"], "New and surviving injections must all remain queryable.") + replace("last = 3", with: "last = 30") + XCTAssertEqual(numbers(), ["1", "4", "30"], "Editing one injection must preserve its siblings.") + } + func testHighlightEmbeddedJavaScriptWithUnicodeAndTextPredicates() throws { let javaScript = TreeSitterLanguage( tree_sitter_javascript(), @@ -37,6 +91,43 @@ final class TreeSitterSyntaxHighlightingTests: XCTestCase { assertColor(theme.textColor, in: source.range(of: ";"), of: highlighted) } + @MainActor + func testCancellationBeforeWorkerStartsStillCompletesTheRequest() { + let context = makeAsyncContext(text: "let target = 42;") + context.queue.isSuspended = true + defer { context.queue.isSuspended = false } + let input = makeInput("let target = 42;") + let rejected = expectation(description: "Canceled queued request completes") + rejected.assertForOverFulfill = true + context.highlighter.syntaxHighlight(input) { result in + if case .success = result { XCTFail("Canceled queued work must not succeed.") } + rejected.fulfill() + } + context.highlighter.cancel() + context.queue.isSuspended = false + wait(for: [rejected], timeout: 5) + } + + @MainActor + func testCompletedRequestDoesNotRetainHighlighterUntilMainQueueDrains() { + var context: AsyncContext? = makeAsyncContext(text: "let target = 42;") + let queue = context!.queue + weak var weakHighlighter = context!.highlighter + let input = makeInput("let target = 42;") + let original = NSAttributedString(attributedString: input.attributedString) + let rejected = expectation(description: "Deallocated highlighter still completes request") + context!.highlighter.syntaxHighlight(input) { result in + if case .success = result { XCTFail("A deallocated highlighter must not apply its pending result.") } + rejected.fulfill() + } + // Keep the main queue blocked until the worker has produced its result. + queue.waitUntilAllOperationsAreFinished() + context = nil + XCTAssertNil(weakHighlighter, "Queued result delivery must not retain the highlighter and its full document.") + wait(for: [rejected], timeout: 5) + XCTAssertTrue(input.attributedString.isEqual(to: original)) + } + @MainActor func testInvalidatedInputCannotApplyCompletedBackgroundCaptures() throws { let context = makeAsyncContext(text: "let target = 42;")