exceptions thrown when undoing (SwiftUI, macOS)
To reproduce (macOS 11.6 (20G165)):
- Run attached project
- Create a new document
- Place insertion point at the end of the document
- Add two characters
- Undo 2x
- Observe:
*** -[NSBigMutableString substringWithRange:]: Range {14, 1} out of bounds; string length 14 - Redo.
- Observe:
__redoCommonDoSingle:: NSUndoManager 0x600003d12580 is in invalid state, do not invoke this method while undoing
Given the stack trace, this seems like it might be an apple bug:
0 CoreFoundation 0x00007fff205ab1db __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff202e4d92 objc_exception_throw + 48
2 Foundation 0x00007fff2128c91d -[NSString substringWithRange:] + 223
3 AppKit 0x00007fff22f66881 -[NSTextStorage(NSUndo) _undoRedoAttributedSubstringFromRange:] + 145
4 AppKit 0x00007fff23514fd7 -[NSUndoTyping undoRedo:] + 142
5 Foundation 0x00007fff21390e26 -[_NSUndoStack popAndInvoke] + 187
6 Foundation 0x00007fff21390b9d -[NSUndoManager undoNestedGroup] + 296
7 AppKit 0x00007fff22f592bb -[NSApplication(NSResponder) sendAction:to:from:] + 288
8 AppKit 0x00007fff2305bcc9 -[NSMenuItem _corePerformAction] + 413
This change works around the behavior: https://github.com/audulus/Sourceful/commit/b724bc8e235e72618b0c30df4a2f87af29ae91d4
Undo still works, but each undo will change a single character, instead of the typical smarter NSTextView behavior.
Single character undo seems like a pretty serious breakage. Is there really no other approach here?
@twostraws I'm not sure. There's a somewhat similar bug with SwiftUI's TextEditor which I've filed with Apple. Once they fix that, the situation here might improve.