native: breaks held for a composition are built however it ends
Fable's delta review on #180, F6. Breaks held while text is marked were built only through `unmarkText` (and `insertText` ending one). The text view now remembers it is holding some, and lays out again once nothing is marked on resigning first responder, on any text change and on any selection change: a composition the input method cancels, or one ended by resigning, builds them too. No test fails without this: in a unit test there is no input system, so resigning leaves the marked text in place, and every other ending either goes through `unmarkText` or edits the text, which lays out anyway. testABreakWaitsForACompositionToEnd still covers the held break.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+18
−1
native/apple/Sources/DocsKit/DocsPagedTextView.swift+17 −1native/apple/Sources/DocsKit/DocumentSurface.swift+1 −0
diff --git a/native/apple/Sources/DocsKit/DocsPagedTextView.swift b/native/apple/Sources/DocsKit/DocsPagedTextView.swift| index 8084aa5..acf36e3 100644 |
| --- a/native/apple/Sources/DocsKit/DocsPagedTextView.swift |
| +++ b/native/apple/Sources/DocsKit/DocsPagedTextView.swift |
| @@ -265,6 +265,7 @@ import UIKit |
| override func resignFirstResponder() -> Bool { |
| let resigned = super.resignFirstResponder() |
| if resigned { reportEditing(false) } |
| + releaseHeldBreaks() |
| return resigned |
| } |
| /// Told on the next turn: a responder change can happen inside a |
| @@ -336,6 +337,7 @@ import UIKit |
| pages.edited(edited, delta: delta) |
| if !pages.regenerating { |
| lastEdit = .now |
| + releaseHeldBreaks() |
| if edited.location < screenTop { editedAboveScreen = true } |
| // The anchor moves with the text. |
| if var kept = anchor, edited.location < kept.offset { |
| @@ -495,9 +497,23 @@ import UIKit |
| /// (Fable on #180). They wait for the composition to end, which lays |
| /// out again (`DocsTextView`). |
| private func applyBreaks(through limit: Int = .max) { |
| - guard markedTextRange == nil else { return } |
| + guard markedTextRange == nil else { |
| + if pages.hasChanges { breaksHeld = true } |
| + return |
| + } |
| pages.applyChanges(through: limit) |
| } |
| + /// Breaks are waiting for a composition to end. |
| + private var breaksHeld = false |
| + /// A composition can end without `unmarkText` (the text view resigning, |
| + /// the input method cancelling): on those, and on any text or selection |
| + /// change, held breaks are built once nothing is marked (Fable's delta |
| + /// review, F6). |
| + func releaseHeldBreaks() { |
| + guard breaksHeld, markedTextRange == nil else { return } |
| + breaksHeld = false |
| + setNeedsLayout() |
| + } |
| /// Whether the idle measuring is running (tests). |
| var isMeasuringWhileIdle: Bool { idle != nil } |
diff --git a/native/apple/Sources/DocsKit/DocumentSurface.swift b/native/apple/Sources/DocsKit/DocumentSurface.swift| index b49c8f6..84c7c3e 100644 |
| --- a/native/apple/Sources/DocsKit/DocumentSurface.swift |
| +++ b/native/apple/Sources/DocsKit/DocumentSurface.swift |
| @@ -558,6 +558,7 @@ extension DocumentSurface: UITextViewDelegate { |
| } |
| func textViewDidChangeSelection(_ textView: UITextView) { |
| guard let text = textView as? DocsTextView else { return } |
| + text.releaseHeldBreaks() |
| selectionDidChange(text) |
| } |
| } |