kyle-n / HighlightedTextEditor

A SwiftUI view for dynamically highlighting user input
MIT License
726 stars 69 forks source link

Crash when pressing on the editor with iPad 10th gen, iPad Pro 4th gen #77

Closed mitchymitch closed 9 months ago

mitchymitch commented 9 months ago

DO NOT DELETE THIS TEMPLATE

If you do not fill out this template, your issue will be automatically closed.

Describe the bug When pressing on the editor to try edit the text, it crashes. I tried swapping back out with a TextEditor and the crash doesn't happen.

To Reproduce

fileprivate let RegexLetterChunks = try! NSRegularExpression(pattern: "[a-zA-Z ]+", options: [])
...
   @State var code: String
...
    self.syntaxColorRules = [
            HighlightRule(pattern: RegexLetterChunks, formattingRules: [
                TextFormattingRule(key: .foregroundColor, value: UIColor.cyan),
                TextFormattingRule(key: .underlineStyle) { content, range in
                    guard graph[content] != nil else { return [] }
                    return NSUnderlineStyle.single.rawValue
                }
            ])
        ]
...
            VStack(alignment: .leading) {
                Text(concept.id)
                    .foregroundColor(.primary.opacity(0.9))
                    .fontWeight(.bold)
                    .fontDesign(.serif)
                    .font(.largeTitle)
                    .multilineTextAlignment(.leading)
                Divider()
                    .frame(height: 0.5)
                    .overlay(.primary.opacity(DividerAlpha))
                HighlightedTextEditor(text: $code, highlightRules: syntaxColorRules)
                    .fixedSize(horizontal: false, vertical: false)
            }
...

Expected behavior I am able to edit the text view.

Screenshots

uncaught exception 'NSInvalidArgumentException', reason: '-[Swift.__EmptyArrayStorage integerValue]: unrecognized selector sent to instance 0x7ff8659a4d90'

Environment

Additional context

Error for queryMetaDataSync: 2
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Swift.__EmptyArrayStorage integerValue]: unrecognized selector sent to instance 0x7ff8659a4d90'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007ff80049b761 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007ff800063904 objc_exception_throw + 48
    2   CoreFoundation                      0x00007ff8004b0873 +[NSObject(NSObject) instanceMethodSignatureForSelector:] + 0
    3   CoreFoundation                      0x00007ff80049fef4 ___forwarding___ + 1459
    4   CoreFoundation                      0x00007ff8004a20f8 _CF_forwarding_prep_0 + 120
    5   UIKitCore                           0x00007ff805f8213f __95+[UIAssistantBarButtonItemProvider barButtonItemForAssistantItemStyle:target:forcePlainButton:]_block_invoke_8 + 153
    6   UIKitCore                           0x00007ff804dab353 -[UIBarButtonItemGroup _validateAllItems] + 199
    7   TextInputUI                         0x00007ff81e85e027 -[TUIAssistantButtonBarView validateButtonGroup:] + 156
    8   TextInputUI                         0x00007ff81e85d683 -[TUIAssistantButtonBarView validateButtonGroups] + 215
    9   TextInputUI                         0x00007ff81e80ad0b -[TUISystemInputAssistantView validateIfNeeded] + 102
    10  TextInputUI                         0x00007ff81e80adce -[TUISystemInputAssistantView layoutSubviews] + 74
    11  UIKitCore                           0x00007ff80628d6af -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2138
    12  QuartzCore                          0x00007ff80afab2bd _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 527
    13  UIKitCore                           0x00007ff806277aca -[UIView(Hierarchy) layoutBelowIfNeeded] + 1450
    14  UIKitCore                           0x00007ff8058a4244 __43-[UIInputWindowController setInputViewSet:]_block_invoke_2 + 52
    15  UIKitCore                           0x00007ff8059ae216 -[_UIRemoteKeyboards ignoreLayoutNotifications:] + 18
    16  UIKitCore                           0x00007ff80588e6da -[UIInputWindowController ignoreLayoutNotifications:] + 97
    17  UIKitCore                           0x00007ff8058a41e4 __43-[UIInputWindowController setInputViewSet:]_block_invoke + 220
    18  UIKitCore                           0x00007ff80627f3b4 +[UIView(Animation) performWithoutAnimation:] + 84
    19  UIKitCore                           0x00007ff8058a3bbe -[UIInputWindowController setInputViewSet:] + 1980
    20  UIKitCore                           0x00007ff8058965a5 -[UIInputWindowController performOperations:withAnimationStyle:] + 71
    21  UIKitCore                           0x00007ff8053adcae -[UIKeyboardSceneDelegate setKeyWindowSceneInputViews:animationStyle:] + 3965
    22  UIKitCore                           0x00007ff8053acd05 -[UIKeyboardSceneDelegate setInputViews:animationStyle:] + 194
    23  UIKitCore                           0x00007ff8053ae483 -[UIKeyboardSceneDelegate setInputViews:animated:] + 86
    24  UIKitCore                           0x00007ff8053ae4ea -[UIKeyboardSceneDelegate setInputViews:] + 68
    25  UIKitCore                           0x00007ff8053aa908 __102-[UIKeyboardSceneDelegate _reloadInputViewsForKeyWindowSceneResponder:force:fromBecomeFirstResponder:]_block_invoke.910 + 35
    26  UIKitCore                           0x00007ff80596da55 __65-[UIPeripheralHost(UIKitInternal) queueDelayedTask:forKey:delay:]_block_invoke + 243
    27  libdispatch.dylib                   0x0000000103d159f7 _dispatch_client_callout + 8
    28  libdispatch.dylib                   0x0000000103d18c04 _dispatch_continuation_pop + 812
    29  libdispatch.dylib                   0x0000000103d2ea2d _dispatch_source_invoke + 2228
    30  libdispatch.dylib                   0x0000000103d256bf _dispatch_main_queue_drain + 955
    31  libdispatch.dylib                   0x0000000103d252f6 _dispatch_main_queue_callback_4CF + 31
    32  CoreFoundation                      0x00007ff8003f7dc4 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    33  CoreFoundation                      0x00007ff8003f26ff __CFRunLoopRun + 2463
    34  CoreFoundation                      0x00007ff8003f197d CFRunLoopRunSpecific + 557
    35  GraphicsServices                    0x00007ff80fe9d08f GSEventRunModal + 137
    36  UIKitCore                           0x00007ff805bbb53d -[UIApplication _run] + 972
    37  UIKitCore                           0x00007ff805bbffab UIApplicationMain + 123
    38  SwiftUI                             0x00007ff84ab8aeda __swift_memcpy18_8 + 2510
    39  SwiftUI                             0x00007ff84ab8ad93 __swift_memcpy18_8 + 2183
    40  SwiftUI                             0x00007ff84a82b0d7 objectdestroy.11Tm + 8814
    41  IDE                                 0x00000001036b6d1e $s3IDE6IDEAppV5$mainyyFZ + 30
    42  IDE                                 0x00000001036b6db9 main + 9
    43  dyld                                0x00000001038083e0 start_sim + 10
    44  ???                                 0x0000000111dc1386 0x0 + 4594602886
)
libc++abi: terminating due to uncaught exception of type NSException
kyle-n commented 9 months ago

Hey, thanks for all the detail in this report! Before we proceed, would you mind doing some debugging to narrow down the problem?

It looks like your HighlightedTextEditor has a couple TextFormattingRules attached to it. Try removing those one by one. If removing a TFR stops the crashes, there's probably a bug in that code.

The issue could also be happening because of RegexLetterChunks. Maybe try with a different NSRegularExpression.

The issue could also be from the other items in the VStack or the fixedSize modifier. I'm not sure!

mitchymitch commented 9 months ago

Hey I removed all the attached stuff on the HighlightedTextEditor and that didn't change anything, but it was the underline rule which made it crash. Removing this stopped the crash:

TextFormattingRule(key: .underlineStyle) { content, range in
   guard graph[content] != nil else { return [] }
   return NSUnderlineStyle.single.rawValue
}
kyle-n commented 9 months ago

Nice! Glad to hear that fixed it.

mitchymitch commented 9 months ago

Thanks for looking at this - just curious, did I get the return type right in that NSUnderlineStyle TextFormattingRule above?

kyle-n commented 9 months ago

Under the covers, TextFormattingRules call addAttribute() on one big NSAttributedString. I think NSUnderlineStyle.single.rawValue is a valid value for .underlineStyle. I don't know if [] is valid. It's an older, untyped API so you'd have to check the docs for NSAttributedString.

The code also could've crashed in that graph[content] check.

You probably have to keep narrowing it down to the specific part of that TFR to find the crash.