twostraws / Sourceful

A syntax highlighting source editor for iOS and macOS using UITextView and NSTextView.

Home Page:https://www.hackingwithswift.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

exceptions thrown when undoing (SwiftUI, macOS)

wtholliday opened this issue · comments

To reproduce (macOS 11.6 (20G165)):

  1. Run attached project
  2. Create a new document
  3. Place insertion point at the end of the document
  4. Add two characters
  5. Undo 2x
  6. Observe: *** -[NSBigMutableString substringWithRange:]: Range {14, 1} out of bounds; string length 14
  7. Redo.
  8. Observe: __redoCommonDoSingle:: NSUndoManager 0x600003d12580 is in invalid state, do not invoke this method while undoing

UndoTest.zip

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: audulus@b724bc8

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.